QueryRunner 或 普通PreparedStatement进行数据增删改操作 |
---|
一、简单描述JDBC获取连接Connection后能做什么?
首先 查看相关API
(一).使用类和方法
1、基于 QueryRunner(接口的方法如下两个):
update(connection,sql,object...objs)增删改操作
2、基于PreparedStatement实现类PrepareStatement
调用其方法:
executeUpdate()进行增删改 相当于 QueryRunner的update()方法
3、注意添加时间的转换:java.sql.包与java.util.包下的Date
java.sql.Date d1 = new java.sql.Date(d.getTime());
(二)在详细的解析了JDBC使用C3P0 或 DBCP获取了连接Connection 之后
进一步的需要对数据库进行数据的增删改的操作
其中大多数项目的增删改操作(其实可以封装在DAO接口里)如下:
/* 增删改操作
*
* INSERT UPDATE DELETE
* @param connection: 数据库连接
* @param sql: SQL语句
* @param args: 填充占位符的可变参数
*/
void update(Connection connection,String sql,Object ...args) throws SQLException;
}
二、此文主要使用框架QueryRunner 和 PreparedStatement
进行 ★增删改操作
//正如我们平时使用最流行的 方法进行操作 不多说 上代码
<1>首先导入jar包(驱动和连接池的不算)
commons-dbutils-1.3.jar
<2>封装使用C3P0获取连接和关闭连接 这两个方法封装如下:
public class JDBCUtils {
//数据库连接池值应该被初始化一次放在静态代码块中
private static DataSource datasource=null;
static{
datasource =new ComboPooledDataSource("helloc3p0");
}
public static Connection getConnection() throws SQLException{
return datasource.getConnection();
}
//用完资源后需要关闭方法如下:
/*释放资源:
Connection
Statement
ResultSet
1 尽量晚创建早释放
2 后使用的先关闭*/
public static void release(ResultSet rs,Statement statement,Connection conn){
if(rs!=null){
try{
rs.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(statement!=null){
try{
statement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<3>方式一、基于QueryRunner创建QueryRunner 实例对象 调用update()方法–通用
如下:
public class TestDBUtils {
@Test
public void testUpdate() throws Exception{
Connection conn =JDBCUtils.getConnection();
//创建QueryRunner对象
QueryRunner qr = new QueryRunner();
//调用里面 的方法
int update = qr.update(conn, "delete from student where studentno=?","s011");
//此处测试返回的结果为数据库sql执行变动行数结果判断是否有>0行改动
Assert.assertTrue(update>0);
//关闭连接资源
JDBCUtils.release(conn, null, null);
}
PreparedStatement和Statementr 的区别在查询文中记录
<4>方式二、基于PreparedStatement创建prepareStatementr 实例对象 调用executeUpdate()方法
public class BasicDao1<T> implements Dao<T> {
//增删改
@Override
public int update(String sql, Object... objects) {
Connection connection = null;
PreparedStatement statement=null;
try {
//1.获取连接
connection = JDBCUtils.getConnection();
//2.获取statement
statement = connection.prepareStatement(sql);
//为占位符赋值
for (int i = 0; i < objects.length; i++) {
statement.setObject(i+1, objects[i]);
}
//3.执行sql
int execute = statement.executeUpdate();
//返回数据库执行结果改变行数
return execute;
} catch (SQLException e) {
throw new RuntimeException();
}finally{
JDBCUtils.release(connection, statement, null);
}
}
<5>方式三、基于Statement创建createStatement 实例对象 调用
//最死板方法只是研究学习不适应
public class TestStatement {
/**
* 测试增删改
* @throws Exception
*/
@Test
public void testUpdate() throws Exception{
Connection connection = null;
PreparedStatement statement=null;
try {
//1.获取连接
connection = JDBCUtils.getConnection();
//获取执行命令对象
Statement statement = connection.createStatement();
//执行
Date d = new Date();
//如何将 util包下的Date转换成sql包下的Date
java.sql.Date d1 = new java.sql.Date(d.getTime());
//1.插入
int execute = statement.executeUpdate("INSERT INTO 表名 VALUES(9,'john','男','jjjj@qq.com','"+d1+"')");
//2.修改
// int execute = statement.executeUpdate("update 表名 set sex='女' where id = 5");
//3.删除
// int execute = statement.executeUpdate("delete from 表名 where id = 7");
if (execute>0) {
System.out.println("success!!");
} else {
System.out.println("failure!!");
}
//释放资源
JDBCUtils.release(connection, statement, null);
}