开发者学堂课程【JDBC 数据库开发进阶:DBUtils 的基本原理】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/32/detail/690
DBUtils 的基本原理
内容介绍:
1.通过代码了解 DBUtils 的基本原理
2.DBUtils 工具类的使用
3.DBUtils 类的使用源码
1.通过代码了解 DBUtils 的基本原理
public class UseDButils {
public static void main(String[] args) {
/*addUser(new User("weijinhao","123456"));
quary("weijinhao");*/
}
public static void addUser(User user) {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
QR qr = new QR(comboPooledDataSource);
String sql = "insert into users values(?,?)";
Object[] para = {user.getUsername(),user.getPassword()};
qr.update(sql, para);
}
public static void quary(String username) {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
QR qr = new QR(comboPooledDataSource);
String sql = "select * from users where username = ?";
Object[] para = {username};
User user = (User) qr.quary(new QRHandler() { //我们在这使用的是匿名内部类
public User handler(ResultSet rs) {
User user = new User();
try {
rs.next();
user.setUsername(rs.getString(1));
user.setPassword(rs.getString(2));
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
sql,para);
System.out.println(user);
}
}
public class QR {
private DataSource ds;
public QR() {
super();
}
public QR(DataSource ds) {
super();
this.ds = ds;
}
/*
* 给出sql和object[]来实现增删改查
*/
public void update(String sql ,Object...objects) {
Connection con = null;
PreparedStatement ps = null;
try{
con = ds.getConnection();
ps = con.prepareStatement(sql);
init(ps,objects); //我们调用该函数来进行prepareStatement的初始化
ps.executeUpdate();
}catch(SQLException s){
s.printStackTrace();
}finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* QRHandler用该对象来进行表数据到对像属性的映射,其实在DBUtils中不用我们自己写该接口的实现类,有写好的可以直接使用
* sql为要查询的语句
public T quary(QRHandler hd, String sql ,Object...objects) {
Connection con = null;
PreparedStatement ps = null;
T user = null;
try{
con = ds.getConnection();
ps = con.prepareStatement(sql);
init(ps,objects);
ResultSet rs = ps.executeQuery();
user = hd.handler(rs); //用该对象来进行表数据到对像属性的映射
catch (SQLException e) {
e.printStackTrace();
}finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
.printStackTrace();
}
return user;
}
}
void init(PreparedStatement ps,Object...objects) {
for(int i = 0;i < objects.length;i++) {
try {
ps.setObject(i + 1, objects[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public interface QRHandler {
T handler(ResultSet rs) ;
}
2.DBUtils 工具类的使用
public class Demo(){
// 数据库添加数据
public void fun1() throws SQLException{
// 给出连接池参数
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
// 给出 sql 模板
String sql = "INSERT INTO t_stu VALUES(?,?,?,?)";
// 给出参数的值
Object[] params = {1003,"zhaoliu",24,"male"};
qr.update(sql,params);
}
// 数据库查询
public void fun2() throws SQLException{
// 给出连接参数
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
// 给出 sql 模板
String sql ="SELECT * FROM t_stu WHERE sid=?";
// 传入参数 Object[] params = {1003}; // 执行query() 方法, 需要给出结果集处理器, 即 ResultSetHandler 的实现类. // ResultHandler 实现类也就是将 ResultSet 结果集转换为 Stu 对象
Stu stu = qr.query(sql,new BeanHandler(Stu.class),params);
System.out.println(stu);
}
}
3.DBUtils 类的使用源码
public class UseDBUtils {
public static void main(String[] args) throws SQLException {
/update();
}
public static void query() throws SQLException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
QueryRunner qr = new QueryRunner(comboPooledDataSource);
String sql = "select * from users where username=?";
BeanHandler rsh = new BeanHandler(User.class);//关于结构集处理器有好几中,还可以自己实现自己的结构集处理器
User user = qr.query(sql, rsh, "zhoufeifei");
System.out.println(user);
}
public static void update() throws SQLException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
QueryRunner qr = new QueryRunner(comboPooledDataSource);
String sql = "insert into users values(?,?)";
Object[] params = {"zhoufeifei" , "123456"};
int i = qr.update(sql, params);
System.out.println(i);
}
}