直接註冊驅動:
java
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()註冊驅動mysql
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類sql
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(); }