直接注册驱动:
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(); }