dbutils入门

dbutils入门

DButils隶属于apache commons,对于一些基本的jdbc操作进行了封装,比之orm要小巧不小,当然功能上弱化很多。

简单demo看看dbutils使用(增删改查):

 

Java代码  

  1. public class DB {   
  2.     private String dirverClassName = "com.mysql.jdbc.Driver";   
  3.     private String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";   
  4.     private String user = "root";   
  5.     private String password = "admin";   
  6.     static Connection conn = null;   
  7.     QueryRunner runner = null;   
  8.   
  9.     private void getConnection() {   
  10.         try {   
  11.             Class.forName(dirverClassName);   
  12.         } catch (ClassNotFoundException e) {   
  13.             e.printStackTrace();   
  14.         }   
  15.         try {   
  16.             conn = DriverManager.getConnection(url, user, password);   
  17.             runner = new QueryRunner();   
  18.         } catch (SQLException e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22.   
  23.     private void insert() throws SQLException {   
  24.         int n = runner.update(conn, "insert into aaa(term) values('你大爷')");   
  25.         System.out.println("插入" + n + "条数据!");   
  26.     }   
  27.   
  28.     private void find() throws SQLException {   
  29.         List<Word> list = (List<Word>) runner.query(conn,   
  30.                 "select id,term from aaa", new BeanListHandler(Word.class));   
  31.         for (Word user : list) {   
  32.             System.out.println(user);   
  33.         }   
  34.     }   
  35.   
  36.     private void delete() throws Exception {   
  37.         runner.update(conn, "delete from aaa where id = ?", 10);   
  38.     }   
  39.   
  40.     public void test() throws Exception {   
  41.         getConnection();   
  42.         // insert();   
  43.         find();   
  44.         DbUtils.closeQuietly(conn);   
  45.     }   
  46.   
  47.     public static void main(String[] args) throws Exception {   
  48.         DB db = new DB();   
  49.         db.test();   
  50.     }   
  51. }  

public class DB {

private String dirverClassName = "com.mysql.jdbc.Driver";

private String url ="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";

private String user = "root";

private String password = "admin";

static Connection conn = null;

QueryRunner runner = null;

 

private void getConnection() {

try {

  Class.forName(dirverClassName);

} catch (ClassNotFoundException e) {

  e.printStackTrace();

}

try {

  conn = DriverManager.getConnection(url, user, password);

  runner = new QueryRunner();

} catch (SQLException e) {

  e.printStackTrace();

}

}

 

private void insert() throws SQLException {

int n = runner.update(conn, "insert into aaa(term) values('你大爷')");

System.out.println("插入" + n + "条数据!");

}

 

private void find() throws SQLException {

List<Word> list = (List<Word>) runner.query(conn,

  "select id,term from aaa", new BeanListHandler(Word.class));

for (Word user : list) {

  System.out.println(user);

}

}

 

private void delete() throws Exception {

runner.update(conn, "delete from aaa where id = ?", 10);

}

 

public void test() throws Exception {

getConnection();

// insert();

find();

DbUtils.closeQuietly(conn);

}

 

public static void main(String[] args) throws Exception {

DB db = new DB();

db.test();

}

}

  

 

 

 

aaa表结构:

 

Java代码  

  1. DROP TABLE IF EXISTS `test`.`aaa`;   
  2. CREATE TABLE  `test`.`aaa` (   
  3.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   
  4.   `term` text NOT NULL,   
  5.   PRIMARY KEY (`id`)   
  6. ) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8;  

DROP TABLE IF EXISTS`test`.`aaa`;

CREATE TABLE  `test`.`aaa` (

  `id` int(10) unsigned NOT NULLAUTO_INCREMENT,

  `term` text NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDBAUTO_INCREMENT=47 DEFAULT CHARSET=utf8;

  

 

除dbutils.jar外还需要mysql-java的驱动包。

廖世勇
原文地址:https://www.cnblogs.com/liaoshiyong/p/3150974.html