dbutils入门

dbutils入门

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

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

Java代码

  1. publicclassDB{
  2. privateStringdirverClassName="com.mysql.jdbc.Driver";
  3. privateStringurl="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
  4. privateStringuser="root";
  5. privateStringpassword="admin";
  6. staticConnectionconn=null;
  7. QueryRunnerrunner=null;
  8. privatevoidgetConnection(){
  9. try{
  10. Class.forName(dirverClassName);
  11. }catch(ClassNotFoundExceptione){
  12. e.printStackTrace();
  13. }
  14. try{
  15. conn=DriverManager.getConnection(url,user,password);
  16. runner=newQueryRunner();
  17. }catch(SQLExceptione){
  18. e.printStackTrace();
  19. }
  20. }
  21. privatevoidinsert()throwsSQLException{
  22. intn=runner.update(conn,"insertintoaaa(term)values('你大爷')");
  23. System.out.println("插入"+n+"条数据!");
  24. }
  25. privatevoidfind()throwsSQLException{
  26. List<Word>list=(List<Word>)runner.query(conn,
  27. "selectid,termfromaaa",newBeanListHandler(Word.class));
  28. for(Worduser:list){
  29. System.out.println(user);
  30. }
  31. }
  32. privatevoiddelete()throwsException{
  33. runner.update(conn,"deletefromaaawhereid=?",10);
  34. }
  35. publicvoidtest()throwsException{
  36. getConnection();
  37. //insert();
  38. find();
  39. DbUtils.closeQuietly(conn);
  40. }
  41. publicstaticvoidmain(String[]args)throwsException{
  42. DBdb=newDB();
  43. db.test();
  44. }
  45. }

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. DROPTABLEIFEXISTS`test`.`aaa`;
  2. CREATETABLE`test`.`aaa`(
  3. `id`int(10)unsignedNOTNULLAUTO_INCREMENT,
  4. `term`textNOTNULL,
  5. PRIMARYKEY(`id`)
  6. )ENGINE=InnoDBAUTO_INCREMENT=47DEFAULTCHARSET=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的驱动包。

你可能感兴趣的:(DbUtils)