java中级_apache_提供的JDBC框架_DML语句使用

来源:互联网 发布:淘宝里金小姐正品代购 编辑:程序博客网 时间:2024/05/22 03:05
package com.bjsxt.lc.dbutil;


import java.sql.SQLException;
import java.util.Date;


import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;


import com.bjsxt.lc.util.MyUtil;
//测试dbUtil
/**
 * QueryRunner的基本使用
 * @author Administrator
 *
 */
public class DBUtil {
private QueryRunner qr = new QueryRunner();
//测试使用dbutil
@Test
public void test01() throws SQLException{
qr.update(MyUtil.getConn(), "insert into emp(eid,ename) values(?,?)",3,"哈哈");
}
//测试添加日期型数据
/**
create table emp2(
eid int(4) primary key,
ename varchar(20) not null,
brithday date not null
)
*/
@Test
//对于mysql来说,在存储到数据库时,就是字符串,所以只有在mysql中能利用字符串直接存储
public void test02() throws SQLException{
qr.update(MyUtil.getConn(), "insert into emp2(eid,ename,brithday) values(?,?,?)",3,"哈哈","1970-1-1");
qr.update(MyUtil.getConn(), "insert into emp2(eid,ename,brithday) values(?,?,?)",2,"哈哈",new Date());
}
//测试删除
@Test
public void test03() throws SQLException{
qr.update(MyUtil.getConn(),"delete from emp where eid = ?" , 3);
}
//测试跟新
@Test
public void test04() throws SQLException{
qr.update(MyUtil.getConn(), "update emp2 set brithday = ? where eid = ?",new Date(),1);
}
//测试batch方法
@Test
public void test05() throws SQLException{
Object[][] params = new Object[10][];
for(int i=0;i<10;i++){
params[i] = new Object[]{i+4,"test"+(i+1),new Date()};
}
qr.batch(MyUtil.getConn(), "insert into emp2(eid,ename,brithday) values(?,?,?);",params);
}

}
0 0
原创粉丝点击