一个项目思路(0):DButil的抽象写法
2015-09-05 04:43
309 查看
这个是我老师写的(根据他写的,想到了后面的思路):
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.clas
99d9
s);
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();
}
}
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.clas
99d9
s);
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();
}
}
相关文章推荐
- asp数据库连接rs("user.id")
- asp连接SQL和Access数据代码(asp里的随机函数)
- SqlServer强制断开数据库已有连接的方法
- C#如何实现对sql server数据库的增删改查
- SQLite 入门教程四 增删改查 有讲究
- javascript实现信息增删改查的方法
- BootStrap实现带有增删改查功能的表格(DEMO详解)
- Oracle 数据库连接查询SQL语句
- hibernate中的增删改查实现代码
- asp.net 数据库的连接和datatable类
- Android--SQLite(增,删,改,查)操作实例代码
- node.js操作mysql(增删改查)
- php+mysql实现简单的增删改查功能
- sqlserver 增删改查一些不常用的小技巧
- PHP+MYSQL实现用户的增删改查
- ASP.NET2.0 SQL Server数据库连接详解
- java常用工具类之数据库连接类(可以连接多种数据库)
- Java Web--增删改查之一界面jsp的操作部分
- mysql++的编译及使用mysql++连接mysql数据库
- C#、SQL server数据库的连接、增、删、改、查等操作的类