JDBC通用类

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



public class BaseDao {
private static Connection con;
private static PreparedStatement ps;
private static ResultSet rs;
private static final String driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url="jdbc:sqlserver://localhost:1433;databaseName=test";
static{
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
*封装数据库的查操作
* @param c 反射类的对象
* @param sql 操作的查询SQL语句
* @param parameters 参数集,调用时无则写null
* @return list 集合
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws SQLException
* @throws InstantiationException
*/
public static List add(Class c,String sql,List<String> parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SQLException, InstantiationException
{
List list = new ArrayList();
Object o = null;
con = getCon();
ps = con.prepareStatement(sql);
setParameter(parameters);
rs = ps.executeQuery();
//得到列信息ResultSetMetaDate对象
ResultSetMetaData rsmd =rs.getMetaData();

//创建一个String的数组,用来保存所有的列名
//rsmd.getColumnCount()为当前结果集中的列的总数,所以定义为长度
String[] cName = new String[rsmd.getColumnCount()];
for (int i = 0; i < cName.length; i++) {
cName[i] = rsmd.getColumnName(i + 1);
}

//得到反射类中的所有的方法
Method[] methods=c.getMethods();

while(rs.next())
{
//如果结果集得到了数据,则实例一个对象
o = c.newInstance();
for(int i=0;i<cName.length;i++)
{
for(Method m:methods)
{
//把从结果集中得到列名前面加上"set",并把第一位设置为大写,加上后面的,成为一个set的名称,
//然后用反射得到的方法名与之比较,相同的话则激活此方法
if(m.getName().equals("set"+cName[i].substring(0,1).toUpperCase()+cName[i].substring(1)))
{
//激活得到方法,并设置值
m.invoke(o, rs.getObject(i+1));
}
}
}
//添加到list集合中
list.add(o);
}
return list;
}

/**
* 封装数据库的增,删,改操作
* @param sql 操作的SQL语句
* @param parameters 参数集
* @return 影响行数
*/
public static int update(String sql,List<String> parameters)
{
int result = 0;
try {
con = getCon();
ps = con.prepareStatement(sql);
setParameter(parameters);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally
{
closeRs(rs);
closePs(ps);
closeCon(con);
}
return result;
}

/**
* 设置参数
* @param parameters 参数集
* @throws SQLException 抛出SQL异常
*/
private static void setParameter(List<String> parameters)
throws SQLException {
if(parameters!=null && parameters.size()>0)
{
for(int i = 0;i<parameters.size();i++)
{
ps.setString(i+1, parameters.get(i));
}
}
}

/**
* 得到Connection连接
* @return Connection连接
* @throws SQLException
*/
public static Connection getCon() throws SQLException
{
con = DriverManager.getConnection(url,"sa","sa");
return con;
}

/**
* 关闭程序中的Connectin 连接
* @param con Connection对象
*/
public static void closeCon(Connection con)
{
if(con!=null)
{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 关闭程序中PreparedStatement对象
* @param ps PreparedStatement对象
*/
public static void closePs(PreparedStatement ps)
{
if(ps!=null)
{
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 关闭程序中ResultSet 对象
* @param rs ResultSet对象
*/
public static void closeRs(ResultSet rs)
{
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值