DBUtils使用详解二

一,使用遵从以下步骤:

1.加载JDBC驱动程序类,并用DriverManager来得到一个数据库连接conn。
2.实例化 QueryRunner,得到实例化对象qRunner。
3. qRunner.update()方法,执行增改删的sql命令,
    qRunner.query()方法,得到结果集。

二,实战

1,连接类ConnectDb:importJava.sql.DriverManager;

[html]  view plain  copy
  1. import java.sql.SQLException;  
  2. import java.sql.Connection;   
  3.   
  4. public class ConnectDb {  
  5.     private static String driveClassName = "com.mysql.jdbc.Driver";  
  6.     private static String url = "jdbc:mysql://192.168.1.161:3306/test?useUnicode=true&characterEncoding=utf8";   
  7.       
  8.     private static String user = "root";  
  9.     private static String password = "e-playnow";  
  10.       
  11.     public static Connection Connect(){  
  12.         Connection conn = null;  
  13.           
  14.         //load driver  
  15.         try {  
  16.             Class.forName(driveClassName);  
  17.         } catch (ClassNotFoundException  e) {  
  18.             System.out.println("load driver failed!");  
  19.             e.printStackTrace();  
  20.         }  
  21.           
  22.         //connect db  
  23.         try {  
  24.             conn = DriverManager.getConnection(url, user, password);  
  25.         } catch (SQLException e) {  
  26.             System.out.println("connect failed!");  
  27.             e.printStackTrace();  
  28.         }         
  29.           
  30.         return conn;  
  31.     }  
  32. }  

数据库表:

[html]  view plain  copy
  1. CREATE TABLE `user` (  
  2.   `id` int(11) NOT NULL auto_increment,  
  3.   `name` varchar(50) NOT NULL,  
  4.   `age` tinyint(10) NOT NULL,  
  5.   PRIMARY KEY  (`id`)  
  6. ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8  

Bean:

[html]  view plain  copy
  1. package Beans;  
  2.   
  3. public class UserBean {  
  4.     private int id;   
  5.     private String name;   
  6.     private int age;  
  7.       
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.     public void setId(int id) {  
  12.         this.id = id;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public int getAge() {  
  21.         return age;  
  22.     }  
  23.     public void setAge(int age) {  
  24.         this.age = age;  
  25.     }  
  26. }  

2,Demo:

[html]  view plain  copy
  1. import java.sql.Connection;  
  2. import java.sql.SQLException;  
  3. import java.util.List;  
  4. import org.apache.commons.dbutils.DbUtils;  
  5. import org.apache.commons.dbutils.QueryRunner;  
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;  
  7. import Beans.UserBean;  
  8.   
  9. public class main {  
  10.   
  11.     public static void main(String[] args) throws SQLException {  
  12.         insert_test();  
  13.         del_test();  
  14.     }  
  15.       
  16.     static void insert_test() throws SQLException{  
  17.         Connection conn = ConnectDb.Connect();  
  18.           
  19.         //创建SQL执行工具   
  20.         QueryRunner qRunner = new QueryRunner();   
  21.           
  22.         //执行SQL插入   
  23.         int n = qRunner.update(conn, "insert into user(name,age) values('xxx',22)");   
  24.         System.out.println("成功插入" + n + "条数据!");   
  25.           
  26.         //关闭数据库连接   
  27.         DbUtils.closeQuietly(conn);       
  28.     }   
  29.       
  30.     static void select_test() throws SQLException{  
  31.         Connection conn = ConnectDb.Connect();  
  32.           
  33.         //创建SQL执行工具   
  34.         QueryRunner qRunner = new QueryRunner();   
  35.           
  36.         @SuppressWarnings("unchecked")  
  37.         List<UserBean> list = (List<UserBean>) qRunner.query(conn, "select id,name,age from user", new BeanListHandler(UserBean.class));   
  38.         //输出查询结果   
  39.         for (UserBean user : list) {   
  40.                 System.out.println(user.getAge());   
  41.         }   
  42.           
  43.         //关闭数据库连接   
  44.         DbUtils.closeQuietly(conn);   
  45.     }   
  46.   
  47.     static void update_test() throws SQLException{  
  48.         Connection conn = ConnectDb.Connect();  
  49.           
  50.         //创建SQL执行工具   
  51.         QueryRunner qRunner = new QueryRunner();   
  52.         //执行SQL插入   
  53.         int n = qRunner.update(conn, "update user set name = 'xxx',age=28");   
  54.         System.out.println("成功更新" + n + "条数据!");   
  55.           
  56.         //关闭数据库连接   
  57.         DbUtils.closeQuietly(conn);   
  58.     }   
  59.       
  60.     static void del_test() throws SQLException{  
  61.         Connection conn = ConnectDb.Connect();  
  62.           
  63.         //创建SQL执行工具   
  64.         QueryRunner qRunner = new QueryRunner();   
  65.         //执行SQL插入   
  66.         int n = qRunner.update(conn, "DELETE from user WHERE name='xxx';");   
  67.         System.out.println("删除成功" + n + "条数据!");   
  68.           
  69.         //关闭数据库连接   
  70.         DbUtils.closeQuietly(conn);   
  71.     }   
  72. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值