String sql="update person set name=? where id=?";

package com.tfy.itheima.impl.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;


import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;


import com.tfy.itheima.domain.Person;
import com.tfy.itheima.jdbc.util.DbcpUtil;




public class QueryRunnerTest {
private QueryRunner qr=new QueryRunner(DbcpUtil.getDataSource());
@Test
public void addPerson(){
try {
String sql="insert into person (id,name,birthday) values(?,?,?)";
// Object []param={1,"tfy","1990-09-09"};
Object []param={2,"谭方勇",new Date()};



qr.update(sql, param);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

}
@Test
public void update(){
try {
String sql="update person set name=? where id=?";
Object params[]={"ty",2};

qr.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
}
}



@Test
public void queryOne(){
try {
String sql="select * from  person  where id=?";
Object params[]={2};

Person p = qr.query(sql,new BeanHandler<Person>(Person.class), params);
System.out.println(p);

} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void queryAll(){
try{
String sql="select * from person";
List<Person> ps = qr.query(sql, new BeanListHandler<Person>(Person.class));
for(Person p:ps){
System.out.println(p);
}

}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@ Test
public void delete(){
try{
String sql="delete from person  where id=?";
qr.update(sql,2);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
public void bath(){
try{
String sql="insert into person (id,name,birthday) values (?,?,?)";
Object [][]params=new Object[100][];
for(int i=0;i<10;i++){
params[i]=new Object[]{i+2,"linlin"+(i+2),new Date()};
}
//System.out.println("QueryRunnerTest.bath()");
//long time=System.currentTimeMillis();
qr.batch(sql, params);
//System.out.println("执行时间为:"+(System.currentTimeMillis()-time));
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
public void clobtxtTest(){
try{
String sql="insert into t1 (id,content) values(?,?)";
File file=new File("C:/a.txt");
Reader r=new FileReader(file);
char []ch=new char[(int)file.length()];

r.read(ch);
r.close();


Clob c=new SerialClob(ch);
Object []params={1,c};//Clob

qr.update(sql, params);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
public void BlobTest(){
try{
String sql="insert into t2 (id,content) values(?,?) ";
InputStream  in= new FileInputStream("c:/118f.jpg");
byte []b=new byte[in.available()];


in.read(b);
in.close();

SerialBlob sb=new SerialBlob(b);
Object []params={1,sb};
qr.update(sql, params);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值