这个是我老师写的(根据他写的,想到了后面的思路):
private static ComboPooledDataSource dataSource;//这里是用c3p0创建连接池
static
{
try
{
ResourceBundle bundle = ResourceBundle.getBundle("config/database/db");
bundle = ResourceBundle.getBundle("config/database/"+bundle.getString("dbfile"));
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(bundle.getString("driverClass"));
dataSource.setJdbcUrl(bundle.getString("jdbcUrl"));
dataSource.setUser(bundle.getString("user"));
dataSource.setPassword(bundle.getString("password"));
dataSource.setMaxPoolSize(Integer.parseInt(bundle.getString("maxPoolSize")));
dataSource.setMinPoolSize(Integer.parseInt(bundle.getString("minPoolSize")));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static boolean cud(String sql,List<Object> params)//增删改
{
boolean flag = false;
// Statement stat = null;
Connection conn = null;
PreparedStatement pstat = null;
try
{
//创建语句对象
//stat = conn.createStatement();
conn = dataSource.getConnection();
pstat = conn.prepareStatement(sql);
if(params!=null)
{
for(int i=1;i<=params.size();i++)
{
pstat.setObject(i, params.get(i-1));
}
}
// int result = stat.executeUpdate(sql);
int result = pstat.executeUpdate();
if(result>0) flag = true;
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
close(null,pstat,conn);
}
return flag;
}
public static List<Object> find(String sql,List<Object> params,Object po)//查找,这里用到的反射机制是后面思路的核心
{
List<Object> objList = new ArrayList<Object>();
PreparedStatement pstat = null;
Connection conn = null;
ResultSet rs = null;
try
{
conn = dataSource.getConnection();
pstat = conn.prepareStatement(sql);
if(params!=null)
{
for(int i=1;i<=params.size();i++)
{
pstat.setObject(i, params.get(i-1));
}
}
rs = pstat.executeQuery();
/*******把rs封装到对象中************/
//取得ResultSet的列名
ResultSetMetaData rsmd = rs.getMetaData();
int columnsCount = rsmd.getColumnCount();
String[] columnNames = new String[columnsCount];
for (int i = 0; i < columnsCount; i++) {
columnNames[i] = rsmd.getColumnLabel(i + 1);
}
//取得Method方法 //反射
Method[] methods = po.getClass().getMethods();
String setMethodName = "";
//遍历ResultSet
while (rs.next()) {
po = po.getClass().newInstance();
//反射, 从ResultSet绑定到JavaBean
for(int i = 0; i < columnNames.length; i++) {
//取得Set方法
setMethodName = "set" + columnNames[i];
//遍历Method
for (int j = 0; j < methods.length; j++) {
if (methods[j].getName().equalsIgnoreCase(setMethodName)) {
setMethodName = methods[j].getName();
Object value = rs.getObject(columnNames[i]);
if(value!=null)
{
//执行Set方法
try
{
//JavaBean内部属性和ResultSet中一致时候
Method setMethod = null;
if(value.getClass()==java.sql.Date.class)
{
setMethod = po.getClass().getMethod(setMethodName, Date.class);
Date Value=new Date(((java.sql.Date)value).getTime());//在取出时,直接把date类型sql 转换成 util
setMethod.invoke(po, Value);
}
else
{
setMethod = po.getClass().getMethod(setMethodName, value.getClass());
setMethod.invoke(po, value);
}
}
catch (Exception e)
{
//System.out.println("数据库数据"+value.toString()+"类型是:"+value.getClass());
//JavaBean内部属性和ResultSet中不一致时候,使用String来输入值
Method setMethod = po.getClass().getMethod(setMethodName, String.class);
setMethod.invoke(po, value);
}
}
}
}
}
objList.add(po);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
close(null,pstat,conn);
}
return objList;
}
private static void close(ResultSet rs,PreparedStatement pstat,Connection conn)
{
try
{
if(rs!=null)rs.close();
if(pstat!=null)pstat.close();
if(conn!=null)conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}