package net.ybx.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.ProxyFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.dbutils.wrappers.SqlNullCheckedResultSet;
import org.apache.commons.dbutils.wrappers.StringTrimmedResultSet;
public class DbUtilsTest {
private static final String DRIVERCLASS = "com.mysql.jdbc.Driver";
private static final String CONNECTURL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8";
private static final String USER = "root";
private static final String PASSWORD = "3769221";
public static Connection conn;
public static void main(String[] args) {
// TODO Auto-generated method stub
DbUtils DB = new DbUtils();
// 加载数据库mysql的jdbc驱动
DB.loadDriver(DRIVERCLASS);
PreparedStatement stmt = null;
QueryRunner qr = new QueryRunner();
try {
conn = DriverManager.getConnection(CONNECTURL, USER, PASSWORD);
// 获得PreparedStatement对象
stmt = conn
.prepareStatement("insert into person(name,age,address) values(?,?,?)");
Person person = new Person();
person.setAddress("上海");
person.setName("南信大");
person.setAge(50);
qr.fillStatementWithBean(stmt, person, new String[] { "name",
"age", "address" });
stmt.execute();
// 删除数据;
qr
.update(conn, "delete from person where id=?",
new Object[] { 4 });
// 查询一个bean数据
person = (Person) qr.query(conn, "select * from person where id=?",
new Object[] { 10 }, new BeanHandler(Person.class));
System.out.println("查询出的数据为:" + person.toString());
// 查询一个List
List
list = (List) qr.query(conn,
"select * from person", new BeanListHandler(Person.class));
// 遍历一个list
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i).toString());
// 返回刚刚插入数据的ID(自动递增的)
Long id = (Long) qr.query(conn, "select last_insert_id()",
new ScalarHandler(1));
System.out.println("最新自动增加的值为:" + id.longValue());
// SqlNullCheckedResult 测试
System.out
.println("-------------------------------一道华丽的分界线-----------------------");
Statement stat = conn.createStatement();
ResultSet rs = stat
.executeQuery("select * from person where id=18");
SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs);
wrapper.setNullString("------N/A------");
wrapper.setNullInt(1000);
rs = ProxyFactory.instance().createResultSet(wrapper);
while (rs.next())
System.out.println(rs.getInt("id") + rs.getString("address")
+ " " + rs.getInt("age"));
System.out
.println("--------------------又是一个华丽的分割线----------------");
rs = stat.executeQuery("select * from person where id=17");
while (rs.next()) {
System.out.println("长度为:"+rs.getString(2).length());
rs = StringTrimmedResultSet.wrap(rs);
System.out.println("现在长度为:"+rs.getString(2).length());
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
DB.rollbackAndCloseQuietly(conn);
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
DbUtils.closeQuietly(stmt);
}
}
}
具体的一些截图如下: