DBCon

来源:互联网 发布:淘宝金币大转盘攻略 编辑:程序博客网 时间:2024/06/04 17:46
//这是我封装的DBCon,用来数据库的访问。就可避免频繁的数据增删查改操作,只需调用即可,用此类之前,首先要导入数据库驱动包。

import java.sql.*;
import java.util.*;

public class BaseCon { 
    static String driver="oracle.jdbc.driver.OracleDriver" ;   //oracle 驱动
    static String url="jdbc:oracle:thin:localhost:1521:orcl";  //url 
    private static String user = "scott";
    private static String password = "tiger";
    
      //加载驱动
        static{
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    
        //获取连接对象
        public  static Connection getCon(){
            try {
                return DriverManager.getConnection(urluserpassword);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        
        
        //释放资源
        public  static void closeAll(Connection con,Statement stmt,ResultSet rs){        
            if(rs != null){
                try {rs.close();} catch (SQLException e) {throw new RuntimeException(e);}finally{rs = null;}
            }
            if(stmt != null){
                try {stmt.close();} catch (SQLException e) {throw new RuntimeException(e);}finally{stmt = null;}
            }   
            if(con != null){
                try {con.close();} catch (SQLException e) {throw new RuntimeException(e);}finally{con = null;}
            }    
        }

   /** 执行数据库查询操作的公共发法*/
   public static List DBSelect(String sql) throws Exception {
            Connection conn = getCon();
            Statement stmt = conn.createStatement();
            ResultSet rst = stmt.executeQuery(sql);
            List list =ResultSetToList(rst);
            closeAll(conn, stmt, rst);
            return list;
   }

   private static List ResultSetToList(ResultSet rs) throws Exception{
            ResultSetMetaData rsm=rs.getMetaData(); //得到结果集数据
            int columnCount=rsm.getColumnCount();  //获得列的数目
            List list=new ArrayList();
            Map rowData;
            while(rs.next()){
                rowData=new HashMap(columnCount);
                for (int i = 1; i <=columnCount; i++) {  //数据库的列下标从1开始
                    String key=rsm.getColumnName(i);
                    Object value=rs.getObject(i);
                     if(value==null)
                         value="";
                    rowData.put(key, value);
                }
                list.add(rowData);
            }
            return list;
   }

     /**执行存储过程的统一方法*/
     public static ArrayList DBSelect(Connection con,CallableStatement cs) throws Exception {
            Connection conn = con;
            CallableStatement stmt = cs;
            ResultSet rst = stmt.executeQuery();
            ArrayList list = ResultSetToList(rst);
            closeAll(conn, stmt, rst);
            return list;
     }

    /** 执行数据库添加,删除,修改操作的公共发法*/
    public static boolean updateDb(String sql, Object[] obj) {
            Connection con = getCon();
            PreparedStatement ps = null;
            try {
                ps = con.prepareStatement(sql);
                if (obj != null && obj.length > 0) {
                    for (int i = 0; i < obj.length; i++) {
                        if (obj[i] == null)
                            obj[i] = "";
                        ps.setObject(i + 1, obj[i]);
                    }
                }
                if (ps.executeUpdate() > 0)
                    return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeAll(con, ps, null);
            }
            return false;
    }

      /**查询数目, 返回int 总数*/
     public  static int DBcount(String sql){
            Connection conn = BaseCon.getCon();
            PreparedStatement pst = null;
            ResultSet rs = null;
            int count=0;
            try {
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                if (rs.next()) {
                  count=rs.getInt(1); //取得总记录数量
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                BaseCon.closeAll(conn, pst, rs);
            }
            return count;
      }

}
原创粉丝点击