• Like
Different waysconnect
Upcoming SlideShare
Loading in...5
×

Different waysconnect

  • 65 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
65
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Connecting to database
  • 2. How many ways to connecting db • 1. using url • 2. using userid,password • 3.using properties
  • 3. Using url • • • • //connecting to the db using url import java.sql.*; class jdbcdemo { • • • • • • public static void main(String[] args) throws Exception { • • • • //Class.forName("oracle.jdbc.driver.OracleDriver"); //Connection conn = DriverManager.getConnection("jdbc:odbc:oracleXE","sai","sai"); //Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","sai","sai" ); // Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "admin");
  • 4. • Statement st= con.createStatement(); • • • • • • } st.executeUpdate("insert into emp values(20,'sai',200)"); System.out.println("Rows inserteded"); st.close(); con.close(); • } • //D:jdbc>set classpath=D:softwaresMySQLnewmysqlconnector-java-5.1.18-bin.jar • //;.;
  • 5. using userid,password • //connecting to the db using user id and password • • • • import java.sql.*; public class Ex2 { public static void main(String arg[]) throws SQLException, ClassNotFoundException { String driverClassName="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/test"; String userName="root"; String password="admin"; // String q="insert into students values(101,'kumar')";//or // String q="insert into students values(102,'raj')"; //load driver class, • • • • • • • • •
  • 6. • • • • • • • • • • • • • • Class.forName(driverClassName); //get the connection Connection con=DriverManager.getConnection(url,userName,password); //get the statement Statement st=con.createStatement(); //execute the query // int count=st.executeUpdate(q); //or int count=st.executeUpdate("insert into students values(103,'ram','sec')"); System.out.println("no of rows "+count); //close the connection con.close(); }//main }//class
  • 7. using properties • • /* give any existing table name at runtime * D:jdbcprogmonday>javac ResultSetMDEx.java • • D:jdbcprogmonday>set classpath=D:softwaresMySQLnewmysql-connector-java-5. 1.18-bin.jar;.; • • • • • D:jdbcprogmonday>java ResultSetMDEx mytable Table Name : mytable id INT name VARCHAR add VARCHAR • • • • • D:jdbcprogmonday> / */ import java.sql.*; import java.util.*; • public class ResultSetMDEx { • • • public static void main(String s[]) throws Exception { Class.forName("com.mysql.jdbc.Driver");
  • 8. • • • • • Properties p= new Properties(); p.put("user", "root"); p.put("password", "admin"); //DriverManager.getConnection(url,userName,password); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test",p); • Statement st = con.createStatement(); • • ResultSet rs=st.executeQuery("select * from "+s[0]); //Table name is taken as an command line arg • ResultSetMetaData rsmd=rs.getMetaData(); • System.out.println("Table Name : "+s[0]); • int colcount=rsmd.getColumnCount(); • • • • for (int i=1;i<=colcount;i++) { System.out.print(rsmd.getColumnName(i)+"t"); System.out.println(rsmd.getColumnTypeName(i)); }//for • • • con.close(); }//main }//class