データベースドライバの登録の3つの方法
4389 ワード
Class.forName("com.mysql.jdbc.Driver");
//
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test";
Connection con= DriverManager.getConnection(url, "root", "123");
System.out.println(con);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.DriverManager経由registerDriver()登録ドライバ
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//
String url="jdbc:mysql://localhost:3306/test";
//extends Hashtable<Object,Object>
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123");
// , mysql
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con=DriverManager.getConnection(url, info);
System.out.println(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3.新規メーカー実現com.mysql.jdbc.Driverクラス
java.sql.Driver driver=new com.mysql.jdbc.Driver();
// :
try {
//com.mysql.jdbc.Driver Java.sql.Driver
java.sql.Driver driver=new com.mysql.jdbc.Driver();
// URL
String url="jdbc:mysql://localhost:3306/test";
//extends Hashtable<Object,Object>
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123");
// connection
Connection con=driver.connect(url, info);
System.out.println(con.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ドライバ登録に成功した後は、基本的にDriverManager.getConnection(url,user,password)は、データベースの接続を取得します.
try{
Class.forName("com.mysql.jdbc.Driver");//
String url="jdbc:mysql://localhost:3306/databasename";//
Connection conn=DriverManager.getConnection(url,"username","password");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println(" !");
}catch(SQLException e){
e.printStackTrace();
}
//
try{
System.setProperty("jdbc.driver","com.mysql.jdbc.Driver");//
String url="jdbc:mysql://localhost:3306/databasename";//
Connection conn=DriverManager.getConnection(url,"username","password");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
// , db jdbc ,3 lib
try{
new com.mysql.jdbc.Driver();// driver ,
String url="jdbc:mysql://localhost:3306/databasename";//
Connection conn=DriverManager.getConnection(url,"username","password");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}