标签:insert manager checked create 集中 font tin har sys
到此为止,说明工作就差不多了,下面就实战一下,进入DBUtils使用详解二。
4、使用遵从以下步骤:
1.加载JDBC驱动程序类,并用DriverManager来得到一个数据库连接conn。
2.实例化 QueryRunner,得到实例化对象qRunner。
3. qRunner.update()方法,执行增改删的sql命令,
qRunner.query()方法,得到结果集。
5,实战
1,连接类ConnectDb:import java.sql.DriverManager;
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 }
2、数据库表
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
3、Bean
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 }
4、Demo
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 }
标签:insert manager checked create 集中 font tin har sys
原文地址:https://www.cnblogs.com/meiliguo/p/9097026.html