JAVA操作数据库的一个通用类


import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet;  
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 
import java.sql.Statement;
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.logging.Level;
import java.util.logging.Logger;
import myapp.conf.ConnectDerbyX;
public class AppSql 
{
    //获取连接
    private static final  Connection conn =ConnectDerbyX.connectDerbyX();
    //根据单句sql语句,构建PreparedStatement的对象.
    private static PreparedStatement getPreparedStatement(String sql) 
    { 
       PreparedStatement pst = null;
        try {                               
            pst = conn.prepareStatement(sql);   
           // System.out.println(getConnect());
        } catch (SQLException ex) {
            Logger.getLogger(AppSql.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pst; 
    }  
    //设置sql语句参数
    private static void setParam(PreparedStatement pst, Object... param) 
    { 
        try {
            int length = param.length;
            for(int i = 0; i < length; i++)
            {
                pst.setObject(i + 1, param[i]);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AppSql.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
    //无结果更新
    public static int executeUpdate(String sql, Object... param)
    {
        int rows = 0; 
        PreparedStatement pst = getPreparedStatement(sql); 
        setParam(pst, param); 
        try
        { 
            rows = pst.executeUpdate(); 
        } 
        catch(SQLException ex) { 
              Logger.getLogger(AppSql.class.getName()).log(Level.SEVERE, null, ex);
        } 
        finally
        { 
            closeDB(new Object[]{conn,pst}); 
        } 
        return rows; 
    } 
    //有结果
    public static List<Map<Object, Object>> executeQuery(String sql, Object... param) 
    { 
        List<Map<Object, Object>> lst = new ArrayList<Map<Object, Object>>();    
        ResultSet rs = null; 
        ResultSetMetaData rsd ; 
        PreparedStatement pst = getPreparedStatement(sql); 
        setParam(pst, param); 
        try
        {
            rs = pst.executeQuery(); 
            if(rs != null) 
            { 
                rsd = rs.getMetaData(); 
                while(rs.next()) 
                { 
                    int columnCount = rsd.getColumnCount();  
                    Map<Object, Object> map  = new HashMap<Object, Object>(); 
                    for(int i = 1; i < columnCount; i++) 
                    { 
                        map.put(rsd.getColumnName(i), rs.getObject(i));  
                    }  
                    lst.add(map); 
                } 
            }  
        } catch(SQLException ex) { 
              Logger.getLogger(AppSql.class.getName()).log(Level.SEVERE, null, ex);
        } finally{ 
            closeDB(new Object[]{conn,rs,pst}); 
        }  
        return lst;  
    }  
  
    public static void execteBatch(String sql[])
    {
        int count=sql.length;
        Statement pst=null;
        try {
            if(count>0)
            {
                pst=conn.createStatement();      
              for(int i=0;i<count;i++)
              {
                  pst.addBatch(sql[i]);
              }
               pst.executeBatch();
            }
        } catch (SQLException ex) {
            Logger.getLogger(AppSql.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{ 
            closeDB(new Object[]{conn,pst,"",""}); 
        }  
    }
    private static void closeDB(Object[] object) 
    {    
        int count=object.length;
        try {
            if(count==3)
            {
                ((Connection)object[0]).close();
                ((ResultSet)object[1]).close();
                ((PreparedStatement)object[2]).close();
            }
            else
            if(count==2)
            {    
                ((Connection)object[0]).close();
                ((PreparedStatement)object[1]).close();
            }
            else
            if(count==4)
            {
                ((Connection)object[0]).close();
                ((Statement)object[1]).close();
            }
            } 
            catch (SQLException ex) 
            {
                Logger.getLogger(AppSql.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
} 


备注:其实算是修改后的,经常使用java操作数据库,为考虑其通用性多次进行这个类的修改,这个算是代码量比较少的,虽然可读性低些,有空在研究其实用性


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值