注册jdbc驱动程序的三种方式.docx

上传人:牧羊曲112 文档编号:3623292 上传时间:2023-03-14 格式:DOCX 页数:3 大小:36.68KB
返回 下载 相关 举报
注册jdbc驱动程序的三种方式.docx_第1页
第1页 / 共3页
注册jdbc驱动程序的三种方式.docx_第2页
第2页 / 共3页
注册jdbc驱动程序的三种方式.docx_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《注册jdbc驱动程序的三种方式.docx》由会员分享,可在线阅读,更多相关《注册jdbc驱动程序的三种方式.docx(3页珍藏版)》请在三一办公上搜索。

1、注册jdbc驱动程序的三种方式注册jdbc驱动程序的三种方式 1、比较常用 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 t

2、ablename); 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.printStack

3、Trace; 2、通过系统的属性设置 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

4、 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; 3、看起来比较直观的一种方式,注册相应的db的jdbc驱动,3在编译时需要导入对应的lib try new com

5、.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;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号