您的位置:首页 > 编程语言 > Java开发

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

2014-03-18 14:10 507 查看

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操作数据库,为考虑其通用性多次进行这个类的修改,这个算是代码量比较少的,虽然可读性低些,有空在研究其实用性
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: