JDBC 笔记
2017-08-02 20:45
253 查看
JDBC 笔记
JDBC 是什么?JDBC 全称就是 Java database connection ,是 sun 公司定义的一组连接数据库的接口,具体的实现类是由各个数据库厂商提供的,称之为驱动。所以,要通过 JDBC 的 API 连接数据库时,必须要有对应的驱动支持。
下面是 JDBC 的架构图:
其中的 DriverMannager 类是 sun 公司编写的类,专门用来管理不同数据库厂商提供的驱动。
到底怎么通过 JDBC 操纵数据库呢?
首先需要下载你需要使用的数据库厂商提供的 Java 驱动包,导入项目中。然后在项目中直接编写代码就可以了。
@Test public void test() throws Exception{ ResultSet rs = null; Connection con = null; PreparedStatement ps = null; //第一步,加载驱动 Class.forName("com.mysql.jdbc.Driver"); //第二步,获得连接对象 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","adminadminadmin"); //利用 preparedStatement 接口来执行 sql 语句,这里会做一个预处理。通过占位符的方式代替变量,可以有效的解决 sql 注入的问题 ps= con.prepareStatement("select id,username,password from demo1 where id > ?"); ps.setObject(1, 3); //第三步,执行 sql 语句,查询就用 executeQuery() 方法,其他就用 executeUpdate() 方法。 rs = ps.executeQuery(); System.out.println(rs); while(rs.next()){//遍历结果集,得到结果。 System.out.println(rs.getString(1) + "---" + rs.getString(2) + "---" + rs.getString(3)); } }
如何封装 JDBC 代码呢?
数据库的连接是通过 Socket 实现的,网络的连接是非常占用资源的,JDBC 非常需要进行优化。而且,数据库的连接信息都不是直接写在程序中的,而是用资源文件代替。
下面就展示怎么写:封装 JDBCUtils 类
public class JDBCUtils { //将资源文件放进 Properties 对象中,方便读取文件内容 static Properties p = null; static { p = new Properties(); try { p.load(JDBCUtils.class.getResourceAsStream("/db.properties"));//此处读取资源文件存在路径的问题,/ 表示在 src 目录下。 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getMysqlConnection1() { try { Class.forName(p.getProperty("mysqlDriver")); return DriverManager.getConnection(p.getProperty("mysqlURL"),p.getProperty("mysqlUsername"),p.getProperty("mysqlPassword")); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void close(ResultSet rs,PreparedStatement ps,Connection con) { if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("rs"); } } if(ps != null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("ps"); } } if(con != null) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("con"); } } } }
调用 JDBCUtils 类封装的方法
Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { con = JDBCUtils.getMysqlConnection1(); ps = con.prepareStatement("select * from testdate"); rs = ps.executeQuery(); while(rs.next()) { System.out.println(rs.getObject(1) + "->" +rs.getObject(2) + "->" + rs.getObject(3) + "->" + rs.getObject(4)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JDBCUtils.close(rs, ps, con); }
补充一个关于事务处理的实现
事务是通过 Connection 对象来控制的,若其中某个 sql 没有执行成功,就会采用回滚操作 rollback( ) 。
ResultSet rs = null; Connection con = null; PreparedStatement ps1 = null; PreparedStatement ps2 = null; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //获得连接对象 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","adminadminadmin"); con.setAutoCommit(false); //利用 preparedStatement 接口来执行 sql 语句,可以有效解决 sql 注入的问题,这里会做一个预处理,prepareStatement() //方法会进行判断,sql 注入的问题直接被解决了 ps1= con.prepareStatement("insert into demo1(username,password,age) values('zh','123456','80')"); ps1.executeUpdate(); System.out.println("第一个执行成功"); ps2= con.prepareStatement("insert into demo1(username,password,aged) values('zh','123456','80')"); ps2.executeUpdate(); con.commit(); } catch (Exception e) { //e.printStackTrace(); try { System.out.println("回滚"); con.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }finally { if(rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ps1 != null) { try { ps1.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(con != null) { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- JAVA 笔记-JDBC(五)
- JDBC 笔记(4)
- jdbc.api数据库编程实作教材 笔记2
- JDBC笔记总结[申明:来源于网络]
- Mybatis学习笔记(一)【对原生jdbc程序中的问题总结】
- JAVA jdbc(数据库连接池)学习笔记(一)
- 关于JDBC的一些笔记
- JDBC笔记(一)基础
- Spring笔记——Spring+JDBC组合开发
- Spring笔记整理——持久层的支持(jdbc,hibernate)
- Java JDBC 理论笔记
- [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等
- [Spring学习笔记 6 ] Spring JDBC 详解
- JAVA 利用JDBC连接MYSQL数据库以及增删改查的实现【学习笔记】
- Java JDBC 理论笔记
- 课堂笔记--JDBC
- jdbc笔记(二)事务
- JDBC知识点笔记
- 错误笔记:JDBC中Statement和PreparedStatement对于Date类型写入数据库问题
- 【JavaEE学习笔记】JDBC_02_抽取jdbc工具类,事物,调用存储过程