注册数据库驱动的三种方式

  1. 直接注册驱动:

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();
}


转载于:https://my.oschina.net/u/1765238/blog/289893

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值