Connecting to database
How many ways to connecting db
• 1. using url
• 2. using userid,password
• 3.using properties
Using url
•
•
•
•

//connecting to the db using url
import java.sql.*;
class jdbcdemo
{

•
•
•
•
•
•

public static void m...
• Statement st= con.createStatement();

•
•
•
•
•
• }

st.executeUpdate("insert into emp values(20,'sai',200)");
System.ou...
using userid,password
•

//connecting to the db using user id and password

•
•
•
•

import java.sql.*;
public class Ex2
{...
•
•
•
•
•
•
•
•
•
•
•
•
•
•

Class.forName(driverClassName);
//get the connection
Connection con=DriverManager.getConnecti...
using properties
•
•

/* give any existing table name at runtime
* D:jdbcprogmonday>javac ResultSetMDEx.java

•
•

D:jdbcp...
•
•
•
•
•

Properties p= new Properties();
p.put("user", "root");
p.put("password", "admin");
//DriverManager.getConnectio...
Different waysconnect
Upcoming SlideShare
Loading in …5
×

Different waysconnect

296 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
296
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Different waysconnect

  1. 1. Connecting to database
  2. 2. How many ways to connecting db • 1. using url • 2. using userid,password • 3.using properties
  3. 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. 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. 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. 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. 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. 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

×