天天看点

J2ee开发用到的数据库操作类大全

J2ee项目开发过程中用到的数据库操作类

此数据库操作封装类在一个省级项目中使用到,在一个集团公司的科技管理项目中也使用过.

package jing.dbnew;

import java.sql.*;

import java.util.*;

import javax.sql.rowset.CachedRowSet;

import oracle.jdbc.rowset.OracleCachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

public class DBManager {

 public DBManager(){

 }

 public void beginTransaction() {

  try {

   Connection con = ConnectionFactory.getInstance().getConnection();

   con.setAutoCommit(false);

  } catch (Exception e) {

   e.printStackTrace();

  }

 }

 public void rollbackTransactoin() {

  try {

   Connection con = ConnectionFactory.getInstance().getConnection();

   con.rollback();

  } catch (Exception e) {

   e.printStackTrace();

  }

 }

 public void commitTransaction() {

  try {

   Connection con = ConnectionFactory.getInstance().getConnection();

   con.commit();

   con.setAutoCommit(true);

   ConnectionFactory.getInstance().closeConnection();

  } catch (Exception e) {

   e.printStackTrace();

  }

 }

// public void endTransaction() {

//  try {

//   Connection con = ConnectionFactory.getInstance().getConnection();

//   con.commit();

//   con.setAutoCommit(true);

//   ConnectionFactory.getInstance().closeConnection();

//  } catch (Exception e) {

//   e.printStackTrace();

//  }

// } 

 public boolean execute(String sql) throws Exception {

  System.out.println("execute:" + sql);

  boolean re = false;

  Connection con = null;

  PreparedStatement pstmt = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt =con.prepareStatement(sql);

   re = pstmt.execute();

  }catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  } finally {

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return re;

 }

 public int[] executeBatch(String[] sql) throws Exception {

  int[] re=null;;

  Connection con = null;

  Statement stmt = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   con.setAutoCommit(false);

   stmt =con.createStatement();

   stmt.clearBatch();

   for(int i=0;i<sql.length;i++){

    stmt.addBatch(sql[i]);

    System.out.println("executeBatch:" + sql[i]);

   }

   re = stmt.executeBatch();

   con.commit();

  }catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  }

  finally {

   if (stmt != null) {

    stmt.close();

   }

  }

  return re;

 }

 public boolean execute(String sql, String[] value, int[] type)

 throws Exception {

  System.out.println("execute:" + sql);

  boolean re = false;

  Connection con = null;

  PreparedStatement pstmt = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,

     ResultSet.CONCUR_UPDATABLE);

   this.setParameters(pstmt,value,type);  //调用私有方法设置参数

   re = pstmt.execute();

  } catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  }finally {

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return re;

 } 

 public CachedRowSet executeQueryCachedRowSet(String sql) throws Exception {

  System.out.println("executeQueryCachedRowSet:" + sql);

  CachedRowSet crs = null;

  //OracleCachedRowSet crs=null;

  Connection con = null;

  PreparedStatement pstmt = null;

  ResultSet rs = null;

  try {

   con =  ConnectionFactory.getInstance().getConnection();

   pstmt=con.prepareStatement(sql);

   rs = pstmt.executeQuery();

   if (!(rs.isBeforeFirst() == rs.isAfterLast())) {

    crs = new CachedRowSetImpl();

    //crs = new OracleCachedRowSet();

    crs.populate(rs);

   }

  } catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  }finally {

   if (rs != null) {

    rs.close();

   }

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return crs;

 }

 public OracleCachedRowSet executeQueryOracleCachedRowSet(String sql) throws Exception {

  System.out.println("executeQueryOracleCachedRowSet:" + sql);

  OracleCachedRowSet crs=null;

  Connection con = null;

  PreparedStatement pstmt = null;

  ResultSet rs = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt=con.prepareStatement(sql);

   rs = pstmt.executeQuery();

   if (!(rs.isBeforeFirst() == rs.isAfterLast())) {

    crs = new OracleCachedRowSet();

    crs.populate(rs);

   }

  }catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  } finally {

   if (rs != null) {

    rs.close();

   }

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return crs;

 }

 public CachedRowSet executeQueryCachedRowSet(String sql, String[] value,

   int[] type) throws Exception {

  System.out.println("executeQueryRowSet:" + sql);

  CachedRowSet crs = null;

  Connection con = null;

  PreparedStatement pstmt = null;

  ResultSet rs = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,

     ResultSet.CONCUR_UPDATABLE);

   this.setParameters(pstmt,value,type);  //调用私有方法设置参数

   rs = pstmt.executeQuery();

   if (!(rs.isBeforeFirst() == rs.isAfterLast())) {

    crs = new CachedRowSetImpl();

    crs.populate(rs);

   }

  }catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  } finally {

   if (rs != null) {

    rs.close();

   }

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return crs;

 }

 public Map executeQueryMap(String sql) throws Exception {

  System.out.println("executeQueryMap:" + sql);

  Map map = null;

  Connection con = null;

  PreparedStatement pstmt = null;

  ResultSet rs = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt =con.prepareStatement(sql);

   rs = pstmt.executeQuery();

   if (rs.next()) { // 有记录的情况

    map = new HashMap();

    ResultSetMetaData rsm = rs.getMetaData();

    int colnum = rsm.getColumnCount(); // 得到记录的列数

    for (int i = 1; i <= colnum; i++) {

     // rsm.getColumnName(i).toUpperCase()统一列名大写

     // rs.getObject(i)更的值为对象类型

     map

       .put(rsm.getColumnName(i).toUpperCase(), rs

         .getObject(i));

    }

   }

  } catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  }finally {

   if (rs != null) {

    rs.close();

   }

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return map;

 }

 public Map executeQueryMap(String sql, String[] value, int[] type)

   throws Exception {

  System.out.println("executeQueryMap:" + sql);

  Map map = null;

  Connection con = null;

  PreparedStatement pstmt = null;

  ResultSet rs = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,

     ResultSet.CONCUR_UPDATABLE);

   this.setParameters(pstmt,value,type);  //调用私有方法设置参数

   rs = pstmt.executeQuery();

   if (rs.next()) { // 有记录的情况

    map = new HashMap();

    ResultSetMetaData rsm = rs.getMetaData();

    int colnum = rsm.getColumnCount(); // 得到记录的列数

    for (int i = 1; i <= colnum; i++) {

     // rsm.getColumnName(i).toUpperCase()统一列名大写

     // rs.getObject(i)更的值为对象类型

     map

       .put(rsm.getColumnName(i).toUpperCase(), rs

         .getObject(i));

    }

   }

  } catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  }finally {

   if (rs != null) {

    rs.close();

   }

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return map;

 }

 public int executeUpdate(String sql) throws Exception {

  System.out.println("executeUpdate:" + sql);

  int re = 0;

  Connection con = null;

  PreparedStatement pstmt = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt = con.prepareStatement(sql);

   re = pstmt.executeUpdate();

  }catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  } finally {

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return re;

 }

 public int executeUpdate(String sql, String[] value, int[] type)

   throws Exception {

  System.out.println("executeUpdate:" + sql);

  int re = 0;

  Connection con = null;

  PreparedStatement pstmt = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,

     ResultSet.CONCUR_UPDATABLE);

   this.setParameters(pstmt,value,type);  //调用私有方法设置参数

   re = pstmt.executeUpdate(); // 影响的记录行数

  }catch(Exception ex){

   ex.printStackTrace();

   con.rollback();

  } finally {

   if (pstmt != null) {

    pstmt.close();

   }

  }

  return re;

 }

 private void setParameters(PreparedStatement pstmt,String[] value,int[] type)throws Exception{

  int j = 1;

  for (int i = 0; i < value.length; i++) {

   switch (type[i]) {

   case Types.INTEGER:

    pstmt.setInt(j, Integer.parseInt(value[i]));

    System.out.println("Parameter("+i+")    INTEGER      "+value[i]);

    break;

   case Types.FLOAT:

   case Types.DOUBLE:

   case Types.DECIMAL:

   case Types.NUMERIC:

    pstmt.setDouble(j, Double.parseDouble(value[i]));

    System.out.println("Parameter("+i+")    FLOAT、DOUBLE、DECIMAL、NUMERIC      "+value[i]);

    break;

   case Types.CHAR:

   case Types.VARCHAR:

   case Types.LONGVARCHAR:

    pstmt.setString(j, value[i]);

    System.out.println("Parameter("+i+")    CHAR、VARCHAR、LONGVARCHAR      "+value[i]);

    break;

   case Types.DATE:

   case Types.TIMESTAMP:

    pstmt.setString(j, value[i]);

    System.out.println("Parameter("+i+")    DATE、TIMESTAMP      "+value[i]);

    break;

   case Types.BOOLEAN:

    // Boolean.parseBoolean(value[i]) JDK5API

    pstmt.setBoolean(j, Boolean.getBoolean(value[i]));

    System.out.println("Parameter("+i+")    BOOLEAN      "+value[i]);

    break;

   }

   j = j + 1;

  }

 }

 public List<Object> executeProc(String procName,int returnType,int[] outParameter,Object... args) throws Exception {

  List<Object> list = null;

  Connection con = null;

  CallableStatement cstmt = null;

  try {

   con = ConnectionFactory.getInstance().getConnection();

   int iInStart = 1;// 存储过程输入参数的起始位

   // 统计问号的个数

   int parameterCount = args.length+outParameter.length; //调用的存储过程的总参数个数

   // 组装带问号的SQL

   String sql = "{call " + procName + "(";

   for (int i = 1; i <= parameterCount; i++) {

    if (i == parameterCount){

     sql += "?";

    }else{

     sql += "?,";

    }

   }

   sql += ")}";

   System.out.println("executeProc:"+sql);

   cstmt = con.prepareCall(sql);

   for (int i = 0; i < args.length; i++) {// 循环设定存储过程的输入参数

    cstmt.setObject(iInStart + i, args[i]);

    System.out.println("设置输入参数:"+iInStart+"  "+args[i]);

   }

   int iOutStart = args.length + iInStart;// 存储过程输出参数的起始位

   for (int i = 0; i < outParameter.length; i++) {// 循环设定存储过程的输出参数

    if (outParameter[i]==Types.REF){ //代表游标

     cstmt.registerOutParameter(i + iOutStart, oracle.jdbc.driver.OracleTypes.CURSOR);

     System.out.println("设置输出参数:"+(i + iOutStart)+" 游标");

    }else if((outParameter[i]==Types.INTEGER)){

     cstmt.registerOutParameter(i + iOutStart, Types.INTEGER);

     System.out.println("设置输出参数:"+(i + iOutStart)+" 数值型");

    }else{

     cstmt.registerOutParameter(i + iOutStart, Types.VARCHAR);

     System.out.println("设置输出参数:"+(i + iOutStart)+" 字符型");

    }

   }

   cstmt.execute();  //执行

   list = new ArrayList<Object>();

   switch (returnType) {

   case 0:// 把存储过程中返回的游标转换为CachedRowSet类型输出

    for (int i = 0; i < outParameter.length; i++) {// 循环获取存储过程的输出结果,包括游标、字符串

     if (outParameter[i]==Types.REF) { //处理游标

      CachedRowSet crs=new CachedRowSetImpl();

      //OracleCachedRowSet crs=new OracleCachedRowSet();  //使用Oracle10g的数据则必须要用10g的驱动,Oracle 10g驱动解决了“ORA-01002: 读取违反顺序”这个问题

      if (cstmt.getObject(i + iOutStart) != null){

       crs.populate((ResultSet)cstmt.getObject(i + iOutStart));

      }

      list.add(crs);

     } else{ //非游标

      list.add(cstmt.getObject(i + iOutStart));

     }

    }

    break;

   }

  } catch (Exception ex) {

   ex.printStackTrace();

   con.rollback();

  } finally {

   cstmt.close();

  }

  return list;

 } 

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

  //DBManager dbm = new DBManager();

  //dbm.execute("update dept set dname=? where deptno=? ",new String[]{"sdfsdfsad","51"},new int[]{Types.VARCHAR,Types.NUMERIC});

  // Connection con = SingletonDataSource.getInstance().getConnection();

  // CachedRowSet crs = dbm.executeQueryRowSet(

  // "SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO FROM EMP WHERE

  // EMPNO >7654",

  // con);

  // while (crs.next()) {

  // System.out.println(crs.getString("EMPNO") + " " +

  // crs.getString("ENAME"));

  // }

  // System.out.println("########################");

  // Map map = dbm.executeQueryMap(

  // "SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO FROM EMP WHERE

  // EMPNO=7654",

  // con);

  // System.out.println(map.get("EMPNO") + " " + map.get("ENAME") + " " +

  // map.get("COMM"));

  // con.close();

  // System.out.println(dbm.selectList("test", "empno", "ename", true,

  // "","select * from emp", null, null));

  //DBManager dbmt = new DBManager();

  //自动事物处理

  //dbmt.execute("insert into emp values (1236, 'TEST', 'TEST', 0, to_date('2008-03-12', 'yyyy-mm-dd'), 0, 0, 10)");

 }

}

package jing.dbnew;

import java.sql.*;

import javax.sql.*;

import javax.naming.*;

public class ConnectionFactory {

 private static DataSource ds = null;

 private static ConnectionFactory cf = new ConnectionFactory();

 private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(); //线程本地变量

 private ConnectionFactory() {

  try {

   InitialContext ctx = new InitialContext();

   // 使用时改成正确的数据配置

   //ds = (DataSource) ctx.lookup("jdbc/OracleSource");

   //TomCat中的连接池获取方式使用java:/comp/env/

   ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/OracleSource");

  } catch (NamingException ne) {

   System.out.println(ne.getMessage() + "/n没有使用连接池!");

  }

 }

 public static ConnectionFactory getInstance() {

  return cf;

 }

 public Connection getConnection() throws Exception {

  Connection con = threadLocal.get();

  if (con == null) {

   if (ds == null) {

    // 使用时改成正确的数据配置

    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    String url = "jdbc:oracle:thin:@localhost:1521:sample";

    con = DriverManager.getConnection(url, "scott", "tiger");

   } else {

    con = ds.getConnection();

   }

   //System.out.println("未使用线程中的连接!");

   threadLocal.set(con);

  }else{

   //System.out.println("使用线程中的连接!");

  }

  return con;

 }

 public void closeConnection() {

  Connection con = threadLocal.get();

  if (con != null) {

   try {

    con.close();

    threadLocal.set(null);

    con=null;

   } catch (Exception e) {

    e.printStackTrace();

   }

  }

 }

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

  Connection con =ConnectionFactory.getInstance().getConnection();

  ConnectionFactory.getInstance().closeConnection();

  con.createStatement();

 }

}

-------------------------------------------------------------------------------------------------

毕业后头五年决定你的一生                                    海量Android教程、开发资料和源码

10类最急需IT人才:Java开发者居首                   给将成为“Android高手”的10个建议 

成为Java高手的25个学习目标--非常经典           Android 4.1果冻豆新特性详解 

芯片巨头海思和展讯:给中国芯片业带来信心    海量经典Java教程、学习资料和源码

Java侵权诉讼Google获胜,Android厚积薄发       面试必备:Android笔试总结 

Android高手必须掌握的28大内容和10个建议     Android平台研发人才缺口30万 

Android开发环境安装和配置步骤详细图解        2012国内移动App开发者大调查结果 

Windows 7下搭建android开发环境步骤图解      Android 4.0的30个突出的新特性 

Android高手要经过的6个阶段和6个境界           linux下搭建Android开发环境步骤 

从IT菜鸟变为“IT骨干开发者”的11个建议        程序员编程技术迅速提高的终极攻略 

2012世界各国人均GDP排名,中国超泰国           2012年全国各省平均工资排行 

2012年中国大学高校排行榜(580强排名)      中国各省市面积和人口数量排名 

中国百万开发者大调查:程序员的薪水不错     Java高手需要越过的10座高山

周立功谈嵌入式:我的25年嵌入式生涯           Android和Java语言的异同和关系 

华为中国区手机销量达千万,80%为智能机        谷歌Android碎片化严重

2012年中国各省GDP和人均GDP排名              90后就业“钱景”:IT仍是最佳选择

2012全球城市竞争力500强,69个中国城市上榜   不要做浮躁的软件工程师 

2012年世界500强,79家大陆香港台湾公司上榜名单 给IT新兵的15个建议 

美国知名科技公司入门级软件工程师的薪水排名  回顾Java经过的风风雨雨 

71道经典Android面试题和答案--重要知识点都涉及到了 

高校应届毕业生“IT业”收入最高,Android技术最热门