dbutils例子

//使用dbutils1.0版本

import java.util.*;
import java.util.logging.*;
import java.sql.*;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;

public class TestDBUnits {
 
 public static void main(String[]args) throws Exception {
  TestDBUnits test = new TestDBUnits();
  
  for(int i = 0 ; i < 1 ; i++) {   
   test.testQuery1();
   test.testQuery2();
   test.testUpdate();
  }
 }
 
 public void testQuery1(){
  try {
   QueryRunner qr = new QueryRunner() ;
   ResultSetHandler rsh = new ArrayListHandler();   
   String strsql = "select * from test1";   
   ArrayList result = (ArrayList)qr.query(getConnection() ,strsql ,rsh);
   //System.out.print("");
  } catch(Exception ex) {
   ex.printStackTrace(System.out);
  }
 }
 
 public void testQuery2(){
  try {
   QueryRunner qr = new QueryRunner() ;
   ResultSetHandler rsh = new MapListHandler();   
   String strsql = "select * from test1";   
   ArrayList result = (ArrayList)qr.query(getConnection() ,strsql ,rsh);
   for(int i = 0 ; i < result.size() ; i++) {
    Map map = (Map)result.get(i);
    //System.out.println(map);    
   }
   //System.out.print("");
  } catch(Exception ex) {
   ex.printStackTrace(System.out);
  }
 }
 
 public void testUpdate(){
  try {
   QueryRunner qr = new QueryRunner() ;
   ResultSetHandler rsh = new ArrayListHandler();
   String strsql = "insert test1(page ,writable ,content)values('ttt','ttt','faskldfjklasdjklfjasdklj')";
   qr.update(getConnection() ,strsql);
   //System.out.print("");
  } catch(Exception ex) {
   ex.printStackTrace(System.out);
  }
 }
 
 private  Connection getConnection() throws InstantiationException,
   IllegalAccessException, ClassNotFoundException, SQLException {
  
  String strDriver = "org.gjt.mm.mysql.Driver";
  String strUrl = "jdbc:mysql://localhost:3306/test";
  String strUser = "root";
  String strPass = "";
   
  Class.forName(strDriver).newInstance();  
  return DriverManager.getConnection(strUrl, strUser, strPass);
 }
}

posted @ 2004-06-24 10:04  程序员天下  阅读(150)  评论(0编辑  收藏  举报