我的db类库 新版

package bean;

public class z_usertest {

 private String name;
 
 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

package defaultDBHelper;

import java.sql.DriverManager;
import java.text.DateFormat;
import java.util.Date;
import zhongheling.java.sql.face.IDBHelper;

public class DBManage extends zhongheling.java.sql.manage.DBManage {
 
 /**** 全局得到连接的必经方法在这里定义
  * 你可以在这里使用连接池也可以为每个请求创建一个连接,DBHellper用完之后会自动关闭连接资源。
  * **/
 public java.sql.Connection getConnection() throws Exception {
  return  getConnection_static();
 }
 /****
  *  连接方法的实现
  * **/
 private static java.sql.Connection getConnection_static() throws Exception {
  // 数据库驱动
  //private final  String driver = "net.sourceforge.jtds.jdbc.Driver";
  //String driver = "net.sourceforge.jtds.jdbc.Driver";
  String driver = "oracle.jdbc.driver.OracleDriver";

  // 数据库地址以及数据源名称
  //private final String url = "jdbc:jtds:sqlserver://localhost:1433/newBBS";
  String url = "jdbc:oracle:thin:@10.38.202.37:1522:telf106";

  // 用户名
  String name = "yy0a";

  // 密码
  String password = "yy0a";

  java.sql.Connection conn = null;
  try {
   Class.forName(driver);
   conn = DriverManager.getConnection(url, name, password);
  } catch (Exception e) {
   System.out.println("连接数据库错误:" + e.getMessage());
   System.out.println(e.toString());
   throw e;
  }
  return conn;
 }
 
 
 /****
  *  设置数据库连接参数 时间格式属性
  *  父类己提供以下实现
  * **/
 /*
 private static DateFormat dateFormat = DateFormat.getDateInstance();
 */

 /****
  * 设置自动增长列字段,这个般不要需要
  * 父类默认己提供以下实现,自定义的话 重写一下即可
  * **/
 /*
 public void getIdentity(Class bealClass,IDBHelper s)throws Exception
 {
  adpater.getIdentityName(bealClass);
 }*/
 
 /****
  * 设置入库时时间格式
  * 父类默认己提供以下实现,自定义的话 重写一下即可
  * **/
 /*
 public   String formatTime(Date date) throws Exception {
  return dateFormat.format(date);
 }*/

 /****
  * IExceptionNoEx接口异常处理
  * 你可以在这里打印或 记录日志
  * 父类默认己提供以下实现,自定义的话 重写一下即可
  * **/
 
 /*
 public   void printException(String sql, String message, Exception e)throws Exception {
  printException_Static(sql,message,e);
 }*/
 
 /****
  * IExceptionNoEx接口异常处理
  * 你可以在这里打印或 记录日志
  * 父类默认己提供以下实现,自定义的话 重写一下即可
  * *******/
 
 /*
 public   void printExceptionNoEx(String sql, String message, Exception e) {
  printExceptionNoEx_Static(sql,message,e);
 }*/
 

 /****
  * 父类静态私有类供参考
  * *******/
 /*
 private static String formatTime_Static(Date date) throws Exception {
  return dateFormat.format(date);
 }
 */

 /****
  * 父类静态私有类供参考
  * *******/
 /*
 private static void printException_Static(String sql, String message, Exception e)throws Exception {
  System.out.println("SQL:"+sql);
  System.out.println("MESSAGE:"+message);
  System.out.println(e.toString());
  e.printStackTrace();
  throw e;
 }
 */
 

 /****
  * 父类静态私有类供参考
  * *******/
/*
 private static void printExceptionNoEx_Static(String sql, String message, Exception e){
  System.out.println("SQL:"+sql);
  System.out.println("MESSAGE:"+message);
  System.out.println(e.toString());
  e.printStackTrace();
 }
*/
}


package defaultDBHelper;

public class OreacleDBHelper<E> extends zhongheling.java.sql.dbhelper.DHHelperNoEx<E> implements zhongheling.java.sql.face.IDBHelperNoEx<E>{
 
 public OreacleDBHelper()
 {
  //指定DB共公方法管类(相关参数在DBManage类代码里设置)
  super(new defaultDBHelper.DBManage());
  //给DB分配Oracle适配器
  this.getDBManage().adpater=new zhongheling.java.sql.adapter.OracleAdapter<E>();
  //给DB分配sqlserver适配器
  //this.getDBManage().adpater=new zhongheling.java.sql.adapter.SqlServerAdapter<E>());
 
 }
}

package main;

import java.util.List;

import defaultDBHelper.OreacleDBHelper;


import bean.z_usertest;

public class main {

 public static void main(String[] args) {
  OreacleDBHelper<z_usertest> db = new OreacleDBHelper<z_usertest>();
  List<z_usertest> list = null;
  list = db.getBeanList("select * from z_usertest  ", z_usertest.class);
  for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i).getName());
  }
  z_usertest user;
  Object[] objs=new Object[1];
  objs[0]="梅西";
  String sql=" select * from z_usertest where name =? ";
  user=db.getBean(sql,z_usertest.class,objs);
  System.out.println(user.getName()+"@@");
 }

}


package zhongheling.java.sql.adapter;

public   interface DBAdapter<E>   {
 
 public  abstract String  getIdentityName(Class beanClass) throws Exception ;

 public  abstract String getInsertSQL(Object bean) throws Exception ;
 
 public  abstract String getUpdateSQL(Object bean, String where) throws Exception;

}


package zhongheling.java.sql.adapter;

import java.util.Date;

import zhongheling.java.sql.basic.DBBasic;

public class OracleAdapter<E> extends DBBasic<E>  implements  DBAdapter {


 // 根据表名得到自动增长列
 public    String getIdentityName(Class beanClass) throws Exception
 {
  return getIdentityName_static(beanClass.getCanonicalName());
 }
 
 /*
  *
  * Object obj 实体类斌好值的的实例
  *
  * 根据实体类 get方法(识别方式是get开头的方法)来生成 insert SQL字符串
  *
  * 此方法自动避开自动增长列
  *
  */
 public    String getInsertSQL(Object bean) throws Exception
 {
    return getInsertSQL_static(bean);
 }
 
 /*
  *
  * Object obj 实体类斌好值的的实例
  *
  * 根据实体类 get方法(识别方式是get开头的方法)来生成 Uqdate SQL字符串
  *
  * where条件自己指定 包括Where关键字也要自己补充
  *
  * 此方法自动避开自动增长列
  *
  */
 public   String getUpdateSQL(Object bean, String where) throws Exception
 {
  return   getUpdateSQL_static(  bean,   where);
 }
 
 private static String getInsertSQL_static(Object bean) throws Exception {
  Class cla = bean.getClass();
  // 定义一个新实体指针
  StringBuffer str = new StringBuffer();
  try {
   str.append("insert into " + cla.getSimpleName() + "");
   str.append(" ( ");

   // 得到传进来的实体的所有属性信息列表
   java.lang.reflect.Method[] methods = cla.getDeclaredMethods();
   boolean isFist = true;
   // 遍历所有方法
   for (java.lang.reflect.Method method : methods) {
    String GetMethodParameterTypes = "";
    GetMethodParameterTypes = method.getReturnType().toString();
    // 如果方法存在返回值

    if (!GetMethodParameterTypes.equals("void"))
     ;
    {
     String methodName = method.getName().toString();

     String fieldName = methodName.substring(3);

     if (GetMethodParameterTypes != null
       && methodName.substring(0, 3).equals("get")) {

      if (!fieldName.equalsIgnoreCase(getIdentityName_static(cla
        .getSimpleName()))) {
       if (!isFist) {
        str.append("," + fieldName);
       } else {
        str.append(fieldName);
        isFist = false;
       }
      }
     }
    }
   }
   str.append(") values (");
   isFist = true;
   // 遍历所有方法
   for (java.lang.reflect.Method method : methods) {
    String GetMethodParameterTypes = "";
    GetMethodParameterTypes = method.getReturnType().getCanonicalName();
    // 如果方法存在返回值
    if (!GetMethodParameterTypes.equals("void"))
     ;
    {
     String methodName = method.getName().toString();
     String fieldName = methodName.substring(3);
     if (!fieldName.equalsIgnoreCase(getIdentityName_static(cla
       .getSimpleName()))) {
      Object[] nullParameter = {};
      Object object = null;
      if (GetMethodParameterTypes != null
        && methodName.substring(0, 3).equals("get")) {
       if (GetMethodParameterTypes.equalsIgnoreCase("")) {
        object = DBBasic.getDBManage().formatTime((Date) object);
       }
       if (IsString(GetMethodParameterTypes)) {
        if (!isFist) {
         object = method.invoke(bean, nullParameter);
         str.append("," + "'" + object + "'");
        } else {
         object = method.invoke(bean, nullParameter);
         str.append("'" + object + "'");
         isFist = false;
        }
       } else {
        if (!isFist) {
         str
           .append(","
             + method.invoke(bean,
               nullParameter));
        } else {
         str.append(method
           .invoke(bean, nullParameter));
         isFist = false;
        }
       }
      }
     }
    }
   }
   str.append(")");
  } catch (Exception e) {
   DBBasic.getDBManage().printException(null,"根据" + bean.getClass().getCanonicalName()
     + "对象生成Insert SQL字符串时异常:" + e.getMessage(), e);
  }
  return str.toString();
 }

 private static String getIdentityName_static(String TableName) throws Exception {
  return "rowid";
 }
 
 private static String getUpdateSQL_static(Object obj, String where)
   throws Exception {
  Class cla = obj.getClass();
  // 定义一个新实体指针
  StringBuffer str = new StringBuffer();
  try {
   str.append("update  " + cla.getSimpleName() + " ");
   str.append(" set ");

   // 得到传进来的实体的所有属性信息列表
   java.lang.reflect.Method[] methods = cla.getDeclaredMethods();
   boolean isFist = true;

   // 遍历所有方法
   for (java.lang.reflect.Method method : methods) {
    String GetMethodParameterTypes = "";
    GetMethodParameterTypes = method.getReturnType().getCanonicalName();
    // 如果方法存在返回值
    if (!GetMethodParameterTypes.equals("void"))
     ;
    {
     String methodName = method.getName().toString();
     String fieldName = methodName.substring(3);
     if (!fieldName.equalsIgnoreCase(getIdentityName_static(cla
       .getSimpleName()))) {
      Object[] nullParameter = {};
      Object object = null;
      if (GetMethodParameterTypes != null
        && methodName.substring(0, 3).equals("get")) {
       if (GetMethodParameterTypes.equalsIgnoreCase("")) {
        object = DBBasic.getDBManage().formatTime((Date) object);
       }
       if (IsString(GetMethodParameterTypes)) {
        if (!isFist) {
         object = method.invoke(obj, nullParameter);
         str.append("," + " " + fieldName + " = ");
         str.append("'" + object + "'");
        } else {
         object = method.invoke(obj, nullParameter);
         str.append(" " + fieldName + " = ");
         str.append("'" + object + "'");
         isFist = false;
        }
       } else {
        if (!isFist) {
         str
           .append(", "
             + fieldName
             + " = "
             + method.invoke(obj,
               nullParameter));
        } else {
         str.append(method
           .invoke(obj, nullParameter));
         isFist = false;
        }
       }
      }
     }
    }
   }
   str.append(" where 1=1 " + where + " ");
  } catch (RuntimeException e) {
   DBBasic.getDBManage().printException(null,"根据" + obj.getClass().getCanonicalName()
     + "对象生成upldate SQL字符串时异常:" + e.getMessage(), e);
  }
  return str.toString();
 }
}


package zhongheling.java.sql.adapter;

import java.util.Date;
import zhongheling.java.sql.basic.DBBasic;

public class SqlServerAdapter<E>extends DBBasic<E> implements  DBAdapter{

 // 根据表名得到自动增长列
 public    String getIdentityName(Class beanClass ) throws Exception
 {
  return getIdentityName_static(beanClass);
 }
 
 /*
  *
  * Object obj 实体类斌好值的的实例
  *
  * 根据实体类 get方法(识别方式是get开头的方法)来生成 insert SQL字符串
  *
  * 此方法自动避开自动增长列
  *
  */
 public    String getInsertSQL(Object bean) throws Exception
 {
    return getInsertSQL_static(bean);
 }
 
 /*
  *
  * Object obj 实体类斌好值的的实例
  *
  * 根据实体类 get方法(识别方式是get开头的方法)来生成 Uqdate SQL字符串
  *
  * where条件自己指定 包括Where关键字也要自己补充
  *
  * 此方法自动避开自动增长列
  *
  */
 public   String getUpdateSQL(Object bean, String where) throws Exception
 {
  return   getUpdateSQL_static( bean,   where);
 }
 
 private static String getInsertSQL_static(Object bean) throws Exception {
  Class cla = bean.getClass();
  // 定义一个新实体指针
  StringBuffer str = new StringBuffer();
  try {
   str.append("insert into [" + cla.getSimpleName() + "]");
   str.append(" ( ");

   // 得到传进来的实体的所有属性信息列表
   java.lang.reflect.Method[] methods = cla.getDeclaredMethods();
   boolean isFist = true;
   // 遍历所有方法
   for (java.lang.reflect.Method method : methods) {
    String GetMethodParameterTypes = "";
    GetMethodParameterTypes = method.getReturnType().toString();
    // 如果方法存在返回值

    if (!GetMethodParameterTypes.equals("void"))
     ;
    {
     String methodName = method.getName().toString();

     String fieldName = methodName.substring(3);

     if (GetMethodParameterTypes != null
       && methodName.substring(0, 3).equals("get")) {

      if (!fieldName.equalsIgnoreCase(getIdentityName_static(cla))) {
       if (!isFist) {
        str.append("," + fieldName);
       } else {
        str.append(fieldName);
        isFist = false;
       }
      }

     }

    }
   }
   str.append(") values (");
   isFist = true;
   // 遍历所有方法
   for (java.lang.reflect.Method method : methods) {
    String GetMethodParameterTypes = "";
    GetMethodParameterTypes = method.getReturnType().toString();
    // 如果方法存在返回值
    if (!GetMethodParameterTypes.equals("void"))
     ;
    {
     String methodName = method.getName().toString();
     String fieldName = methodName.substring(3);
     if (!fieldName.equalsIgnoreCase(getIdentityName_static(cla))) {
      Object[] nullParameter = {};
      Object object = null;
      if (GetMethodParameterTypes != null
        && methodName.substring(0, 3).equals("get")) {
       if (GetMethodParameterTypes.equalsIgnoreCase("")) {
        object = DBBasic.getDBManage().formatTime((Date) object);
       }
       if (IsString(GetMethodParameterTypes)) {
        if (!isFist) {
         object = method.invoke(bean, nullParameter);
         str.append("," + "'" + object + "'");
        } else {
         object = method.invoke(bean, nullParameter);
         str.append("'" + object + "'");
         isFist = false;
        }
       } else {
        if (!isFist) {
         str
           .append(","
             + method.invoke(bean,
               nullParameter));
        } else {
         str.append(method
           .invoke(bean, nullParameter));
         isFist = false;
        }
       }
      }
     }
    }
   }
   str.append(")");
  } catch (Exception e) {
   DBBasic.getDBManage().printException(null,"根据" + bean.getClass().getCanonicalName()
     + "对象生成Insert SQL字符串时异常:" + e.getMessage(), e);
  }
  return str.toString();
 }

 private static String getIdentityName_static(Class cla) throws Exception {
  String sql = "select name  from syscolumns   where id=object_id('["
    + cla.getCanonicalName()
    + "]') and COLUMNPROPERTY( syscolumns.id,syscolumns.name,'IsIdentity') =1";
  return (String)DBBasic.getObj(cla, sql,null);
 }
 private static String getUpdateSQL_static(Object obj, String where)
   throws Exception {
  Class cla = obj.getClass();
  // 定义一个新实体指针
  StringBuffer str = new StringBuffer();
  try {
   str.append("update [" + cla.getSimpleName() + "]");
   str.append(" set ");

   // 得到传进来的实体的所有属性信息列表
   java.lang.reflect.Method[] methods = cla.getDeclaredMethods();
   boolean isFist = true;

   // 遍历所有方法
   for (java.lang.reflect.Method method : methods) {
    String GetMethodParameterTypes = "";
    GetMethodParameterTypes = method.getReturnType().toString();
    // 如果方法存在返回值
    if (!GetMethodParameterTypes.equals("void"))
     ;
    {
     String methodName = method.getName().toString();
     String fieldName = methodName.substring(3);
     if (!fieldName.equalsIgnoreCase(getIdentityName_static(cla))) {
      Object[] nullParameter = {};
      Object object = null;
      if (GetMethodParameterTypes != null
        && methodName.substring(0, 3).equals("get")) {
       if (GetMethodParameterTypes.equalsIgnoreCase("")) {
        object = DBBasic.getDBManage().formatTime((Date) object);
       }
       if (IsString(GetMethodParameterTypes)) {
        if (!isFist) {
         object = method.invoke(obj, nullParameter);
         str.append("," + " " + fieldName + " = ");
         str.append("'" + object + "'");
        } else {
         object = method.invoke(obj, nullParameter);
         str.append(" " + fieldName + " = ");
         str.append("'" + object + "'");
         isFist = false;
        }
       } else {
        if (!isFist) {
         str
           .append(", "
             + fieldName
             + " = "
             + method.invoke(obj,
               nullParameter));
        } else {
         str.append(method
           .invoke(obj, nullParameter));
         isFist = false;
        }
       }
      }
     }
    }
   }
   str.append(" where 1=1 " + where + " ");
  } catch (RuntimeException e) {
   DBBasic.getDBManage().printException(null,"根据" + obj.getClass().getCanonicalName()
     + "对象生成upldate SQL字符串时异常:" + e.getMessage(), e);
  }
  return str.toString();
 }
}


package zhongheling.java.sql.basic;

import java.lang.reflect.Method;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.CallableStatement;
import java.util.ArrayList;
import java.util.List;

import zhongheling.java.sql.pack.PreparedStatementPack;
import zhongheling.java.sql.pack.ResultSetPack;

public abstract class DBBasic<E> {

 private static zhongheling.java.sql.manage.DBManageSuper DBManage;

 protected static java.sql.Connection getConnection(Connection conn)
   throws Exception {
  return DBManage.getConnection();
 }

 protected static boolean execute(Connection conn, Statement stat, String sql)
   throws Exception {
  boolean bl = false;
  try {
   conn = getConnection(conn);
   stat = conn.createStatement();
   bl = stat.execute(sql);
  } catch (Exception e) {
   DBManage.printException(sql, "execute", e);
  } finally {
   close(conn, stat);
  }
  return bl;
 }

 protected static ResultSet executeQuery(Connection conn, Statement stat,
   String sql) throws Exception {
  ResultSet rs = null;
  try {
   conn = getConnection(conn);
   stat = conn.createStatement();
   rs = stat.executeQuery(sql);
  } catch (Exception e) {
   DBManage.printException(sql, "executeQuery", e);
  } finally {
   close(null, stat);
  }
  return rs;
 }

 protected static int executeUpdate(Connection conn, Statement stat,
   String sql) throws Exception {
  int num = 0;
  try {
   conn = getConnection(conn);
   stat = conn.createStatement();
   num = stat.executeUpdate(sql);
  } catch (Exception e) {
   DBManage.printException(sql, "executeUpdate", e);
  } finally {
   close(null, stat);
  }
  return num;
 }

 protected static boolean prem_execute(PreparedStatement prem,
   Connection conn, String sql, Object[] parameters) throws Exception {

  boolean bl = false;
  try {
   conn = getConnection(conn);
   if (conn != null) {
    prem = conn.prepareStatement(sql);
   }
   bl = prem.execute(sql);
  } catch (Exception e) {
   DBManage.printException(sql, "prem_execute", e);
  } finally {
   close(conn, prem);
  }
  return bl;
 }

 protected static java.sql.ResultSet prem_executeQuery(
   PreparedStatement prem, Connection conn, String sql, Object[] parameters)
   throws Exception {
  java.sql.ResultSet rs = null;
  try {
   conn = getConnection(conn);
   if (conn != null) {
    prem = conn.prepareStatement(sql);
   }
   if (parameters != null) {
    SetPrepareStatement(prem, parameters);
   }
   rs = prem.executeQuery();
  } catch (Exception e) {
   DBManage.printException(sql, "prem_executeQuery", e);
  }
  return rs;
 }

 protected static int prem_executeUpdate(PreparedStatement prem,
   Connection conn, String sql, Object[] parameters) throws Exception {
  int i = 0;
  try {
   conn = getConnection(conn);
   if (conn != null) {
    prem = conn.prepareStatement(sql);
   }
   i = prem.executeUpdate();
  } catch (Exception e) {
   DBManage.printException(sql, "prem_executeUpdate", e);
  } finally {
   close(conn, prem);
  }
  return i;
 }

 protected static void Process(Connection conn, CallableStatement cst,
   String sql) throws Exception {
  try {
   cst = conn.prepareCall(sql);
   cst.setInt(1, 100);// 设置输入参数值
   cst.registerOutParameter(2, Types.VARCHAR);// 将第2个参数设为输出
   cst.execute();
   String res = cst.getString(2);// 获得执行后的输出值
   System.out.println(res);// 打印输出值
  } catch (SQLException e) {
   DBManage.printException(sql, "Process", e);
  } finally {
   close(conn, cst);
  }
 }

 /*
  *
  * Object obj 实体类斌好值的的实例
  *
  * 根据实体类 get方法(识别方式是get开头的方法)来保存实体
  *
  * 此方法自动避开自动增长列
  *
  */

 protected static int addBean_static(Object bean) throws Exception {
  int i = 0;
  try {
   PreparedStatement prem = null;
   Connection conn = null;
   i = prem_executeUpdate(prem, conn, DBManage.adpater.getInsertSQL(bean),null);
  } catch (Exception e) {
   DBManage.printException(null, "add "
     + bean.getClass().getCanonicalName() + "对象时异常:", e);
  }
  return i;
 }

 
 
 protected static Object getObj(Class beanClass, String sql, Object[] parameters)
   throws Exception {
  Object obj = null;
  ResultSet rs = null;
  PreparedStatement prem = null;
  Connection conn = null;
  try {
   rs = prem_executeQuery(prem, conn, sql,parameters);
   if (rs.next()) {
    Method packMethod = null;
    Class[] classes = { Integer.class };
    Object[] p = { new Integer(0) };
    packMethod = getPackMethod(beanClass.getCanonicalName(), classes,
      packMethod, rs.getClass());
    obj = packMethod.invoke(rs, p);
   }
  } catch (Exception e) {
   DBManage.printException(null, "调用StaticDBHelper的getObj方法执行" + sql
     + "时异常:", e);
  } finally {
   close(conn, prem, rs);
  }
  return obj;
 }

 protected static List<String> getStringList_static(String sql, Object[] parameters)
   throws Exception {
  List<String> i = null;

  ResultSet rs = null;
  PreparedStatement prem = null;
  Connection conn = null;
  try {
   rs = prem_executeQuery(prem, conn, sql,parameters);
   while (rs.next()) {
    i.add(rs.getString(1));
   }
  } catch (Exception e) {
   DBManage.printException(sql, "getStringList", e);
  } finally {
   close(conn, prem, rs);
  }
  return i;
 }

 protected static List<Object> getBeanList(String sql, Class beanClass,
    Statement stat,PreparedStatement prem, Connection conn, boolean IsPack, Object[] parameters)
   throws Exception {
  List<Object> list = null;
  ResultSet rs = null;
  try {
   if(parameters==null)
   {
    rs = executeQuery(conn, stat, sql);
   }
   else
   {
    rs = prem_executeQuery(prem, conn, sql,parameters);
   }
   list = (List<Object>) RsToBeanList(rs, beanClass, false, IsPack);
  } catch (Exception e) {
   DBManage.printException(sql, "getBeanList", e);
  } finally {
   close(stat, prem, conn, null, rs);
  }
  return list;
 }

 

 /*
  *
  * Object obj 实体类斌好值的的实例
  *
  * 根据实体类 get方法(识别方式是get开头的方法)来更新
  *
  * where条件自己指定 包括Where关键字也要自己补充
  *
  * 此方法自动避开自动增长列
  *
  */

 protected static int updateByWhere_static(Object obj, String where,Object[] parameters)
   throws Exception

 {
  int i = 0;
  try {
   PreparedStatement prem = null;
   Connection conn = null;
   i = prem_executeUpdate(prem, conn, DBManage.adpater.getUpdateSQL(obj,where),parameters);
  } catch (Exception e) {
   DBManage.printException(null, "updateByWhere", e);
  }
  return i;
 }

 /*
  * 参数 ResultSet rs 查询后的带连结的结果集对象 参数 Class 需要设置到的实体的Class信息 返回 List<E> 实体列表
  *
  * 根据实体类 set方法(识别方式是set开头并无返回值的方法)来读ResultSet对象
  * 使用时需保证实体除了属性的set方法之外没有别的set方法, 实体的set方法 一般与查询后返回的结果集中的列名一一对应,
  * 使用时如果实体的set方法与ResultSet对象中的列名不一一对应最少应保证实体的set方法在ResultSet对象中有列名与之对应。
  *
  */
 protected static List<Object> RsToBeanList(ResultSet rs, Class BeanClass,
   boolean IsOneRow, boolean IsPack) throws Exception {

  List<Object[]> rowInfoList = getRowInfoList(BeanClass, IsPack);
  List<Object> beanList = null;
  try {
   beanList = new ArrayList<Object>();
   Object pack = null;
   if (IsOneRow) {
    if (rs.next())// 读每行
    {
     setBean(rs, BeanClass, IsPack, rowInfoList, beanList, pack);
    }
   } else {
    while (rs.next())// 读每行
    {
     setBean(rs, BeanClass, IsPack, rowInfoList, beanList, pack);
    }
   }
  } catch (Exception e) {
   DBManage.printException(null, "ResultSet对象信息封装到BeanList时异常:"
     + e.getMessage(), e);
  }
  return beanList;
 }

 /*
  *
  * 参数 Class 需要设置到的实体的Class信息 返回 List<Object[]> list
  * list中的每个元素Object[]按约定的位置放三个元素 Object[0]实际为Method对象 是包装类的getXX()方法
  * (包装类是一个自定义的适配器用于包装ResultSet对象的getXX()结果便于反射调用) Object[1]实际为Method对象
  * 是实体类的set方法 Object[2]实际为String对象 是实体类的属性(根据截取set方法名的后面字节得到)
  *
  */
 private static List<Object[]> getRowInfoList(Class cla, boolean IsPack)
   throws Exception {
  List<Object[]> RowInfoList = new ArrayList<Object[]>();

  // 得到实体的所有方法列表
  java.lang.reflect.Method[] methods = cla.getDeclaredMethods();
  // 遍历所有方法
  for (java.lang.reflect.Method method : methods) {
   getRowInfo(IsPack, RowInfoList, method);
  }
  return RowInfoList;
 }

 private static void SetPrepareStatement(PreparedStatement prem,
  Object[] parameters) throws Exception {
  Class[] classes =null;
  Object[] setMethodParameters=new Object[2];
  Method method = null;
   for (int i = 0; i < parameters.length; i++) {
    if (parameters[i] != null) {
     classes=new Class[2];
     classes[0]=Integer.class;
     method =getPrepareStatementMethod(parameters[i].getClass().getCanonicalName(),classes,PreparedStatementPack.class);  
     setMethodParameters[0]=new Integer(i+1);
     setMethodParameters[1]=parameters[i];
     DBManage.psPack.setPreparedStatement(prem);
     method.invoke(DBManage.psPack, setMethodParameters);
    }
   }
 }

 // 得到要读的字段的信息
 private static void getRowInfo(boolean IsPack, List<Object[]> RowInfoList,
   java.lang.reflect.Method method) throws Exception {
  // 得到形参数组(如果有)
  Class[] typeClass = method.getParameterTypes();
  // 如果方法存在一个以DBHelper形参
  if (null != typeClass && typeClass.length > 0) {
   // 得到形参类型字符串
   String MethodParameterTypes = "";
   MethodParameterTypes = typeClass[0].getCanonicalName();
   // 得到当前方法名字
   String methodName = method.getName().toString();
   String fieldName = null;
   Class[] classes = { String.class };
   // 如果是set开头的方法
   if (MethodParameterTypes != null
     && methodName.substring(0, 3).equals("set")) {
    fieldName = methodName.substring(3);
    Object[] t = new Object[3];
    Method packMethod = null;
    Class packClass = null;
    packClass = setPackClass(IsPack);
    packMethod = getPackMethod(MethodParameterTypes, classes,
      packMethod, packClass);
    t[0] = packMethod;
    t[1] = method;
    t[2] = fieldName;
    RowInfoList.add(t);
   }
  }
 }

 // getRowInfo方法的子方法,用于设置是得到哪个对像的getXX()方法(rs对象或Pack对象)
 private static Class setPackClass(boolean IsPack) throws Exception {
  Class packClass;
  if (IsPack) {
   packClass = ResultSetPack.class;
  } else {
   packClass = ResultSet.class;
  }
  return packClass;
 }

 // getInfo方法里的功能方法,得到要包装器或RS对象要执行的getXX()方法
 @SuppressWarnings("unchecked")
 private static Method getPackMethod(String MethodParameterTypes,
   Class[] classes, Method packMethod, Class packClass)
   throws Exception {
  if (MethodParameterTypes.equals("java.lang.String")) {
   packMethod = packClass.getMethod("getString", classes);// 得到包装类的方法
  } else if (MethodParameterTypes.equals("int")
    || MethodParameterTypes.equals("java.lang.Integer")) {
   packMethod = packClass.getMethod("getInt", classes);
  } else if (MethodParameterTypes.equals("java.util.Date")) {
   packMethod = packClass.getMethod("getDate", classes);
  } else if (MethodParameterTypes.equals("short")
    || MethodParameterTypes.equals("java.lang.Short")) {
   packMethod = packClass.getMethod("getShort", classes);
  } else if (MethodParameterTypes.equals("float")
    || MethodParameterTypes.equals("java.lang.Float")) {
   packMethod = packClass.getMethod("getFloat", classes);
  } else if (MethodParameterTypes.equals("double")
    || MethodParameterTypes.equals("java.lang.Double")) {
   packMethod = packClass.getMethod("getDouble", classes);
  } else if (MethodParameterTypes.equals("byte")
    || MethodParameterTypes.equals("java.lang.Byte")) {
   packMethod = packClass.getMethod("getByte", classes);
  } else if (MethodParameterTypes.equals("long")
    || MethodParameterTypes.equals("java.lang.Long")) {
   packMethod = packClass.getMethod("getLong", classes);
  } else {
   packMethod = packClass.getMethod("getObject", classes);
  }
  return packMethod;
 }

 // getInfo方法里的功能方法,得到要包装器或RS对象要执行的getXX()方法
 @SuppressWarnings("unchecked")
 private static Method getPrepareStatementMethod(
   String MethodParameterTypes, Class[] classes,
   Class  Cla) throws Exception {
  Method method=null;
  if (MethodParameterTypes.equals("java.lang.String")) {
   classes[1]=String.class;
   method=Cla.getMethod("setString",classes);// 得到包装类的方法;
  } else if (MethodParameterTypes.equals("int")
    || MethodParameterTypes.equals("java.lang.Integer")) {
   classes[1]=int.class;
   method=Cla.getMethod("setInt", classes);
  } else if (MethodParameterTypes.equals("java.util.Date")) {
   classes[1]=Date.class;
   method=Cla.getMethod("setDate", classes);
  } else if (MethodParameterTypes.equals("short")
    || MethodParameterTypes.equals("java.lang.Short")) {
   classes[1]=short.class;
   method=Cla.getMethod("setShort", classes);
  } else if (MethodParameterTypes.equals("float")
    || MethodParameterTypes.equals("java.lang.Float")) {
   classes[1]=float.class;
   method=Cla.getMethod("setFloat", classes);
  } else if (MethodParameterTypes.equals("double")
    || MethodParameterTypes.equals("java.lang.Double")) {
   classes[1]=double.class;
   method=Cla.getMethod("setDouble", classes);
  } else if (MethodParameterTypes.equals("byte")
    || MethodParameterTypes.equals("java.lang.Byte")) {
   classes[1]=byte.class;
   method=Cla.getMethod("setByte", classes);
  } else if (MethodParameterTypes.equals("long")
    || MethodParameterTypes.equals("java.lang.Long")) {
   classes[1]=long.class;
   method=Cla.getMethod("setLong", classes);
  } else {
   classes[1]=Object.class;
   method=Cla.getMethod("setObject", classes);
  }
  return   method;// 得到包装类的方法;
 }

 // RsToBeanList的子方法,用于设置RS对象每行的信息到Bean对象中
 private static void setBean(ResultSet rs, Class BeanClass, boolean IsPack,
   List<Object[]> rowInfoList, List<Object> beanList, Object pack)
   throws Exception {
  Object newBean;
  newBean = (Object) BeanClass.newInstance();// 新的bean实例
  // 设置每字段i表示第几个字段
  for (int i = 0; i < rowInfoList.size(); i++) {
   // 定义一个值对象指针
   Object value = null;

   // 得到包装类的方法
   Method packMethod = (Method) rowInfoList.get(i)[0];

   // 得到set方法
   Method setMethod = (Method) rowInfoList.get(i)[1];
   // 是否启用包装器
   if (IsPack) {
    // 设置ResultSet对象到包装器对象
    ((ResultSetPack) pack).setResultSet(rs);
   } else {
    // 直接使用ResultSet
    pack = rs;
   }
   // 得到数据库对应该set方法的列名
   String columnLabel = (String) rowInfoList.get(i)[2];

   // 调用包装类的get方法得到字段值
   value = packMethod.invoke(pack, columnLabel);

   // 设置值到字段
   setMethod.invoke(newBean, value);
  }
  beanList.add(newBean);
 }

 // 用于生成sql语句判断是否加双引号,返回真是加双引号,返回假时加单引号
 protected static boolean IsString(String type) throws Exception {
  if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")
    || type.equalsIgnoreCase("Short")
    || type.equals("java.lang.Short")
    || type.equalsIgnoreCase("Float")
    || type.equals("java.lang.Float")
    || type.equalsIgnoreCase("Double")
    || type.equals("java.lang.Double")
    || type.equalsIgnoreCase("Byte")
    || type.equals("java.lang.Byte")
    || type.equalsIgnoreCase("short")
    || type.equals("java.lang.Short")
    || type.equalsIgnoreCase("long")
    || type.equals("java.lang.Long")) {
   return false;
  } else {
   return true;
  }
 }

 protected static void close(Connection conn, Statement stat)
   throws Exception {
  close(stat, null, conn, null, null);
 }

 protected static void close(Connection conn, PreparedStatement prem)
   throws Exception {
  close(null, prem, conn, null, null);
 }

 protected static void close(Connection conn, PreparedStatement prem,
   ResultSet rs) throws Exception {
  close(null, prem, conn, null, rs);
 }

 protected static void close(Statement stat, PreparedStatement prem,
   Connection conn, CallableStatement cst, ResultSet rs)
   throws Exception {
  try {
   if (null != conn) {
    conn.close();
   }
   if (null != stat) {
    stat.close();
   }
   if (null != prem) {
    prem.close();
   }
   if (null != cst) {
    cst.close();
   }
   if (null != rs) {
    rs.close();
   }
  } catch (SQLException e) {
   DBManage.printException(null, "数据库操作资源己关闭:" + e.getMessage(), e);
  }
 }

 protected static void setDBManage(
   zhongheling.java.sql.manage.DBManageSuper manage) {
  DBManage = manage;
 }

 protected static zhongheling.java.sql.manage.DBManageSuper getDBManage() {
  return DBManage;
 }

}


package zhongheling.java.sql.dbhelper;

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.CallableStatement;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import zhongheling.java.sql.basic.DBBasic;
import zhongheling.java.sql.face.IDBHelper;
import zhongheling.java.sql.manage.DBManageSuper;
import zhongheling.java.sql.pack.ResultSetPack;

public class DBHelper<E> extends DBBasic<E> implements IDBHelper<E> {

 private Statement stat = null;

 private PreparedStatement prem = null;

 private Connection conn = null;

 private CallableStatement cst = null;

 private DBBasic<E> StaticDB;

 protected static DateFormat dateFormat = DateFormat.getDateInstance();

 protected static ResultSetPack pack = null;//得到包装类对象

 public DBHelper(DBManageSuper manage) {
  StaticDB.setDBManage(manage);
 }

 public boolean execute(String sql) throws Exception {

  return StaticDB.execute(conn, stat, sql);

 }

 public ResultSet executeQuery(String sql) throws Exception {

  return StaticDB.executeQuery(conn, stat, sql);
 }

 public int executeUpdate(String sql) throws Exception {
  return StaticDB.executeUpdate(conn, stat, sql);
 }

 public boolean prem_execute(String sql) throws Exception {

  return StaticDB.prem_execute(prem, conn, sql, null);
 }

 public java.sql.ResultSet prem_executeQuery(String sql) throws Exception {

  return StaticDB.prem_executeQuery(prem, conn, sql, null);
 }

 public int prem_executeUpdate(String sql) throws Exception {

  return StaticDB.prem_executeUpdate(prem, conn, sql, null);
 }

 public boolean prem_execute(String sql, Object[] parameters) throws Exception {

  return StaticDB.prem_execute(prem, conn, sql, parameters);
 }

 public java.sql.ResultSet prem_executeQuery(String sql, Object[] parameters)
   throws Exception {

  return StaticDB.prem_executeQuery(prem, conn, sql, parameters);
 }

 public int prem_executeUpdate(String sql, Object[] parameters) throws Exception {

  return StaticDB.prem_executeUpdate(prem, conn, sql, parameters);
 }

 public void Process(String sql) throws Exception {
  StaticDB.Process(conn, cst, sql);
 }

 public E getBean(String sql, Class bean) throws Exception {
  if (DBHelper.pack != null) {
   return (E) StaticDB.getBeanList(sql, bean,stat, prem, conn, true, null)
     .get(0);
  } else {
   return (E) StaticDB.getBeanList(sql, bean, stat,prem, conn, false, null)
     .get(0);
  }
 }

 public int updateBean(E bean) throws Exception {
  return StaticDB.updateByWhere_static(bean, null, null);
 }

 public int updateByWhere(E bean, String where) throws Exception {
  return StaticDB.updateByWhere_static(bean, where, null);
 }

 public List<E> getBeanList(String sql, Class bean) throws Exception {
  if (DBHelper.pack != null) {
   return (List<E>) StaticDB.getBeanList(sql, bean,stat, prem, conn, true,
     null);
  } else {
   return (List<E>) StaticDB.getBeanList(sql, bean, stat,prem, conn, false,
     null);
  }
 }

 public int updateBean(E bean, Object[] parameters) throws Exception {
  return StaticDB.updateByWhere_static(bean, null, parameters);
 }

 public int updateByWhere(E bean, String where, Object[] parameters)
   throws Exception {
  return StaticDB.updateByWhere_static(bean, where, parameters);
 }

 public E getBean(String sql, Class bean, Object[] parameters) throws Exception {
  if (DBHelper.pack != null) {
   return (E) StaticDB.getBeanList(sql, bean, stat,prem, conn, true, parameters)
     .get(0);
  } else {
   return (E) StaticDB.getBeanList(sql, bean, stat,prem, conn, false, parameters)
     .get(0);
  }
 }

 public List<E> getBeanList(String sql, Class bean, Object[] parameters)
   throws Exception {
  if (DBHelper.pack != null) {
   return (List<E>) StaticDB.getBeanList(sql, bean, stat,prem, conn, true,
     parameters);
  } else {
   return (List<E>) StaticDB.getBeanList(sql, bean, stat,prem, conn, false,
     parameters);
  }
 }

 public int addBean(E bean) throws Exception {
  return StaticDB.addBean_static(bean);
 }

 public byte getByte(String sql) throws Exception {
  return (Byte) StaticDB.getObj(byte.class, sql,null);
 }

 public int getInt(String sql) throws Exception {
  return (Integer) StaticDB.getObj(int.class, sql,null);
 }

 public float getFloat(String sql) throws Exception {
  return (Float) StaticDB.getObj(float.class, sql,null);
 }

 public short getShort(String sql) throws Exception {
  return (Short) StaticDB.getObj(short.class, sql,null);
 }

 public double getDouble(String sql) throws Exception {
  return (Double) StaticDB.getObj(double.class, sql,null);
 }

 public String getString(String sql) throws Exception {
  return (String) StaticDB.getObj(String.class, sql,null);
 }

 public Date getDate(String sql) throws Exception {
  return (Date) StaticDB.getObj(Date.class, sql,null);
 }

 public Object getObject(String sql) throws Exception {
  return StaticDB.getObj(Object.class, sql,null);
 }

 public List<String> getStringList(String sql) throws Exception {
  return StaticDB.getStringList_static(sql, null);
 }

 public byte getByte(String sql, Object[] parameters) throws Exception {
  return (Byte) StaticDB.getObj(byte.class, sql, parameters);
 }

 public int getInt(String sql, Object[] parameters) throws Exception {
  return (Integer) StaticDB.getObj(int.class, sql, parameters);
 }

 public float getFloat(String sql, Object[] parameters) throws Exception {
  return (Float) StaticDB.getObj(float.class, sql, parameters);
 }

 public short getShort(String sql, Object[] parameters) throws Exception {
  return (Short) StaticDB.getObj(short.class, sql, parameters);
 }

 public double getDouble(String sql, Object[] parameters) throws Exception {
  return (Double) StaticDB.getObj(double.class, sql, parameters);
 }

 public String getString(String sql, Object[] parameters) throws Exception {
  return (String) StaticDB.getObj(String.class, sql, parameters);
 }

 public Date getDate(String sql, Object[] parameters) throws Exception {
  return (Date) StaticDB.getObj(Date.class, sql, parameters);
 }

 public Object getObject(String sql, Object[] parameters) throws Exception {
  return StaticDB.getObj(Object.class, sql, parameters);
 }

 public List<String> getStringList(String sql, Object[] parameters)
   throws Exception {
  return StaticDB.getStringList_static(sql, parameters);
 }

 public void close() throws Exception {
  StaticDB.close(stat, prem, conn, cst, null);
 }

}


package zhongheling.java.sql.dbhelper;

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.CallableStatement;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import zhongheling.java.sql.basic.DBBasic;
import zhongheling.java.sql.face.IDBHelperNoEx;
import zhongheling.java.sql.manage.DBManageSuper;
import zhongheling.java.sql.pack.ResultSetPack;

public class DHHelperNoEx<E> extends DBBasic<E> implements IDBHelperNoEx<E> {

 private Statement stat = null;

 private PreparedStatement prem = null;

 private Connection conn = null;

 private CallableStatement cst = null;

 private DBBasic<E> StaticDB;

 protected static DateFormat dateFormat = DateFormat.getDateInstance();

 protected static ResultSetPack pack = null;//得到包装类对象

 public DHHelperNoEx(DBManageSuper manage) {
  StaticDB.setDBManage(manage);
 }

 public boolean execute(String sql) {

  boolean b = false;
  try {
   b = StaticDB.execute(conn, stat, sql);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_execute", e);
  }
  return b;
 }

 public ResultSet executeQuery(String sql) {
 
  ResultSet r = null;
  try {
   r = StaticDB.executeQuery(conn, stat, sql);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_executeQuery", e);
  }
  return r;
 }

 public int executeUpdate(String sql) {
 
  int i =0;
  try {
   i = StaticDB.executeUpdate(conn, stat, sql);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_executeUpdate", e);
  }
  return i;
 }

 public boolean prem_execute(String sql) {
 
  boolean b =false;
  try {
   b = StaticDB.prem_execute(prem, conn, sql,null);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_prem_execute", e);
  }
  return b;
 }

 public java.sql.ResultSet prem_executeQuery(String sql) {
 
  ResultSet r = null;
  try {
   r = StaticDB.prem_executeQuery(prem, conn, sql,null);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_prem_executeQuery", e);
  }
  return r;
 }

 public int prem_executeUpdate(String sql) {
 
  int i =0;
  try {
   i = StaticDB.prem_executeUpdate(prem, conn, sql,null);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_prem_executeUpdate", e);
  }
  return i;
 }
public boolean prem_execute(String sql,Object[] parameters) {
 
  boolean b =false;
  try {
   b = StaticDB.prem_execute(prem, conn, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_prem_execute", e);
  }
  return b;
 }

 public java.sql.ResultSet prem_executeQuery(String sql,Object[] parameters) {
 
  ResultSet r = null;
  try {
   r = StaticDB.prem_executeQuery(prem, conn, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_prem_executeQuery", e);
  }
  return r;
 }

 public int prem_executeUpdate(String sql,Object[] parameters) {
 
  int i =0;
  try {
   i = StaticDB.prem_executeUpdate(prem, conn, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_prem_executeUpdate", e);
  }
  return i;
 }

 public void Process(String sql) {
 
  try {
   StaticDB.Process(conn, cst, sql);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_Process", e);
  }
 }
 public int addBean(E bean) {
 
  int i=0;
  try {
   i= StaticDB.addBean_static(bean);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(null, "DHHelperNoEx<E>_addBean", e);
  }
  return i;
 }
 @SuppressWarnings("unchecked")
 public E getBean(String sql, Class bean) {
 
  Object object=null;
  try {
   if (DBHelper.pack != null) {
    object= StaticDB.getBeanList(sql, bean,stat, prem, conn, true, null).get(0);
   } else {
    object= StaticDB.getBeanList(sql, bean,stat, prem, conn, false, null).get(0);
   }
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getBean", e);
  }
  return(E)object;
 }
 

 @SuppressWarnings("unchecked")
 public List<E> getBeanList(String sql, Class bean) {
 
  List<E> list=null;
  try {
   if (DBHelper.pack != null) {
    list= (List<E>) StaticDB.getBeanList(sql, bean,stat, prem, conn, true,null);
   } else {
    list= (List<E>) StaticDB.getBeanList(sql, bean, stat,prem, conn, false,null);
   }
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getBeanList", e);
  }
 
  return list;
 }

 @SuppressWarnings("unchecked")
 public List<E> getBeanList(String sql, Class bean, boolean IsPack) {
 
  List<E> list=null;
  try {
   list= (List<E>) StaticDB.getBeanList(sql, bean, stat,prem, conn, IsPack,null);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getBeanList", e);
  }
  return list;
 }

 public int updateBean(E bean) {
 
  int i=0;
  try {
   i= StaticDB.updateByWhere_static(bean, null, null);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(null, "DHHelperNoEx<E>_updateBean", e);
  }
  return i;
 }

 public int updateByWhere(E bean, String where) {
 
  int i=0;
  try {
   i= StaticDB.updateByWhere_static(bean, where,null);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(null, "DHHelperNoEx<E>_updateByWhere", e);
  }
  return i;
 }
 @SuppressWarnings("unchecked")
 public E getBean(String sql, Class bean,Object[] parameters) {
 
  Object object=null;
  try {
   if (DBHelper.pack != null) {
    object= StaticDB.getBeanList(sql, bean, stat,prem, conn, true,parameters).get(0);
   } else {
    object= StaticDB.getBeanList(sql, bean,stat, prem, conn, false,parameters).get(0);
   }
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getBean", e);
  }
  return(E)object;
 }

 @SuppressWarnings("unchecked")
 public List<E> getBeanList(String sql, Class bean,Object[] parameters) {
 
  List<E> list=null;
  try {
   if (DBHelper.pack != null) {
    list= (List<E>) StaticDB.getBeanList(sql, bean,stat, prem, conn, true,parameters);
   } else {
    list= (List<E>) StaticDB.getBeanList(sql, bean, stat,prem, conn, false,parameters);
   }
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getBeanList", e);
  }
 
  return list;
 }

 @SuppressWarnings("unchecked")
 public List<E> getBeanList(String sql, Class bean, boolean IsPack,Object[] parameters) {
 
  List<E> list=null;
  try {
   list= (List<E>) StaticDB.getBeanList(sql, bean,stat, prem, conn, IsPack,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getBeanList", e);
  }
  return list;
 }

 public int updateBean(E bean,Object[] parameters) {
 
  int i=0;
  try {
   i=StaticDB.updateByWhere_static(bean, null, parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(null, "DHHelperNoEx<E>_updateBean", e);
  }
  return i;
 }

 public int updateByWhere(E bean, String where,Object[] parameters) {
 
  int i=0;
  try {
   i= StaticDB.updateByWhere_static(bean, where,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(null, "DHHelperNoEx<E>_updateByWhere", e);
  }
  return i;
 }


 public byte getByte(String sql) {
  
   byte b=0;
   try {
    b= (Byte) StaticDB.getObj(byte.class, sql,null);
   } catch (Exception e) {
    this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getByte", e);
   }
   return b;
  }

  public int getInt(String sql) {
  
   int i=0;
   try {
    i= (Integer) StaticDB.getObj(int.class, sql,null);
   } catch (Exception e) {
    this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getInt", e);
   }
   return i;
  }

  public float getFloat(String sql) {
  
   float f=0;
   try {
    f= (Float) StaticDB.getObj(float.class, sql,null);
   } catch (Exception e) {
    this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getFloat", e);
   }
   return f;
  }

  public short getShort(String sql) {
  
   short s=0;
   try {
    s= (Short) StaticDB.getObj(short.class, sql,null);
   } catch (Exception e) {
   }
   return s;
  }

  public double getDouble(String sql) {
  
   double d=0;
   try {
    d= (Double) StaticDB.getObj(double.class, sql,null);
   } catch (Exception e) {
   }
   return d;
  }

  public String getString(String sql) {
  
   String s=null;
   try {
    s= (String) StaticDB.getObj(String.class, sql,null);
   } catch (Exception e) {
    this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getString", e);
   }
   return s;
  }

  public Date getDate(String sql) {
  
   Date d=null;
   try {
    d= (Date) StaticDB.getObj(Date.class, sql,null);
   } catch (Exception e) {
    this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getDate", e);
   }
   return d;
  }

  public Object getObject(String sql) {
  
  
   Object o=null;
   try {
    o= (Object) StaticDB.getObj(Object.class, sql,null);
   } catch (Exception e) {
    this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getObject", e);
   }
   return o;
  }

  public List<String> getStringList(String sql) {
  
   List<String> list=null;
   try {
    list= StaticDB.getStringList_static(sql,null);
   } catch (Exception e) {
    this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getStringList", e);
   }
   return list;
  }
 public byte getByte(String sql,Object[] parameters) {
 
  byte b=0;
  try {
   b= (Byte) StaticDB.getObj(byte.class, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getByte", e);
  }
  return b;
 }

 public int getInt(String sql,Object[] parameters) {
 
  int i=0;
  try {
   i= (Integer) StaticDB.getObj(int.class, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getInt", e);
  }
  return i;
 }

 public float getFloat(String sql,Object[] parameters) {
 
  float f=0;
  try {
   f= (Float) StaticDB.getObj(float.class, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getFloat", e);
  }
  return f;
 }

 public short getShort(String sql,Object[] parameters) {
 
  short s=0;
  try {
   s= (Short) StaticDB.getObj(short.class, sql,parameters);
  } catch (Exception e) {
  }
  return s;
 }

 public double getDouble(String sql,Object[] parameters) {
 
  double d=0;
  try {
   d= (Double) StaticDB.getObj(double.class, sql,parameters);
  } catch (Exception e) {
  }
  return d;
 }

 public String getString(String sql,Object[] parameters) {
 
  String s=null;
  try {
   s= (String) StaticDB.getObj(String.class, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getString", e);
  }
  return s;
 }

 public Date getDate(String sql,Object[] parameters) {
 
  Date d=null;
  try {
   d= (Date) StaticDB.getObj(Date.class, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getDate", e);
  }
  return d;
 }

 public Object getObject(String sql,Object[] parameters) {
 
 
  Object o=null;
  try {
   o= (Object) StaticDB.getObj(Object.class, sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getObject", e);
  }
  return o;
 }

 public List<String> getStringList(String sql,Object[] parameters) {
 
  List<String> list=null;
  try {
   list= StaticDB.getStringList_static(sql,parameters);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(sql, "DHHelperNoEx<E>_getStringList", e);
  }
  return list;
 }

 
 public void close() {
 
  try {
       StaticDB.close(stat, prem, conn, cst, null);
  } catch (Exception e) {
   this.getDBManage().printExceptionNoEx(null, "DHHelperNoEx<E>_close", e);
  }
 }

}

package zhongheling.java.sql.face;

import java.sql.ResultSet;
import java.util.Date;
import java.util.List;

public interface IDBHelper<E> {

 public abstract boolean execute(String sql) throws Exception;

 public abstract ResultSet executeQuery(String sql) throws Exception;

 public abstract int executeUpdate(String sql) throws Exception;

 public abstract boolean prem_execute(String sql) throws Exception;

 public abstract java.sql.ResultSet prem_executeQuery(String sql)
   throws Exception;

 public abstract int prem_executeUpdate(String sql) throws Exception;

 public abstract boolean prem_execute(String sql, Object[] objs)
   throws Exception;

 public abstract java.sql.ResultSet prem_executeQuery(String sql,
   Object[] objs) throws Exception;

 public abstract int prem_executeUpdate(String sql, Object[] objs)
   throws Exception;

 public abstract void Process(String sql) throws Exception;

 public abstract int addBean(E bean) throws Exception;

 public abstract E getBean(String sql, Class cla) throws Exception;

 public abstract List<E> getBeanList(String sql, Class cla) throws Exception;

 public abstract int updateBean(E bean) throws Exception;

 public abstract int updateByWhere(E bean, String where) throws Exception;

 public abstract E getBean(String sql, Class cla, Object[] objs)
   throws Exception;

 public abstract List<E> getBeanList(String sql, Class cla, Object[] objs)
   throws Exception;

 public abstract int updateBean(E bean, Object[] objs) throws Exception;

 public abstract int updateByWhere(E bean, String where, Object[] objs)
   throws Exception;

 public abstract byte getByte(String sql) throws Exception;

 public abstract int getInt(String sql) throws Exception;

 public abstract float getFloat(String sql) throws Exception;

 public abstract short getShort(String sql) throws Exception;

 public abstract double getDouble(String sql) throws Exception;

 public abstract String getString(String sql) throws Exception;

 public abstract Date getDate(String sql) throws Exception;

 public abstract Object getObject(String sql) throws Exception;

 public abstract List<String> getStringList(String sql) throws Exception;

 public abstract byte getByte(String sql, Object[] parameters) throws Exception;

 public abstract int getInt(String sql, Object[] parameters) throws Exception;

 public abstract float getFloat(String sql, Object[] parameters) throws Exception;

 public abstract short getShort(String sql, Object[] parameters) throws Exception;

 public abstract double getDouble(String sql, Object[] parameters)
   throws Exception;

 public abstract String getString(String sql, Object[] parameters)
   throws Exception;

 public abstract Date getDate(String sql, Object[] parameters) throws Exception;

 public abstract Object getObject(String sql, Object[] parameters)
   throws Exception;

 public abstract List<String> getStringList(String sql, Object[] parameters)
   throws Exception;

 public abstract void close() throws Exception;
}


package zhongheling.java.sql.face;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;

public interface IDBHelperNoEx<E> {

 public abstract boolean execute(String sql) ;

 public abstract ResultSet executeQuery(String sql) ;

 public abstract int executeUpdate(String sql) ;

 public abstract boolean prem_execute(String sql) ;

 public abstract java.sql.ResultSet prem_executeQuery(String sql);

 public abstract int prem_executeUpdate(String sql) ;

 public abstract boolean prem_execute(String sql, Object[] parameters);

 public abstract java.sql.ResultSet prem_executeQuery(String sql,
   Object[] parameters) ;

 public abstract int prem_executeUpdate(String sql, Object[] parameters);

 public abstract void Process(String sql) ;

 public abstract int addBean(E bean) ;

 public abstract E getBean(String sql, Class cla) ;

 public abstract List<E> getBeanList(String sql, Class cla) ;

 public abstract int updateBean(E bean) ;

 public abstract int updateByWhere(E bean, String where) ;

 public abstract E getBean(String sql, Class cla, Object[] parameters);

 public abstract List<E> getBeanList(String sql, Class cla, Object[] parameters);

 public abstract int updateBean(E bean, Object[] parameters) ;

 public abstract int updateByWhere(E bean, String where, Object[] parameters);

 public abstract byte getByte(String sql) ;

 public abstract int getInt(String sql) ;

 public abstract float getFloat(String sql) ;

 public abstract short getShort(String sql) ;

 public abstract double getDouble(String sql) ;

 public abstract String getString(String sql) ;

 public abstract Date getDate(String sql) ;

 public abstract Object getObject(String sql) ;

 public abstract List<String> getStringList(String sql) ;

 public abstract byte getByte(String sql, Object[] parameters) ;

 public abstract int getInt(String sql, Object[] parameters) ;

 public abstract float getFloat(String sql, Object[] parameters) ;

 public abstract short getShort(String sql, Object[] parameters) ;

 public abstract double getDouble(String sql, Object[] parameters);

 public abstract String getString(String sql, Object[] parameters);

 public abstract Date getDate(String sql, Object[] parameters) ;

 public abstract Object getObject(String sql, Object[] parameters);

 public abstract List<String> getStringList(String sql, Object[] parameters);

 public abstract void close() ;

}


package zhongheling.java.sql.manage;

import java.sql.DriverManager;
import java.text.DateFormat;
import java.util.Date;

import zhongheling.java.sql.face.IDBHelper;

public class DBManage extends DBManageSuper {
 
 //时间格式属性
 protected static DateFormat dateFormat = DateFormat.getDateInstance();

 //得到连接
 public java.sql.Connection getConnection() throws Exception {
 
  return  getConnection_static();
 }
 //设置自动增长列字段
 public void getIdentity(Class bealClass,IDBHelper s)throws Exception
 {
  adpater.getIdentityName(bealClass);
 }
 
 //设置入库时时间格式
 public   String formatTime(Date date) throws Exception {
  return dateFormat.format(date);
 }

 //数据库访问过程中的异常处理,可以在这里打印日志等
 public   void printException(String sql, String message, Exception e)throws Exception {
  printException_Static(sql,message,e);
 }
 //数据库访问过程中的异常处理,可以在这里打印日志等
 public   void printExceptionNoEx(String sql, String message, Exception e) {
  printExceptionNoEx_Static(sql,message,e);
 }
 private static java.sql.Connection getConnection_static() throws Exception {
  // 数据库驱动
  //private final  String driver = "net.sourceforge.jtds.jdbc.Driver";
  //String driver = "net.sourceforge.jtds.jdbc.Driver";
  String driver = "oracle.jdbc.driver.OracleDriver";

  // 数据库地址以及数据源名称
  //private final String url = "jdbc:jtds:sqlserver://localhost:1433/newBBS";
  String url = "jdbc:oracle:thin:@10.38.202.37:1522:telf106";

  // 用户名
  String name = "yy0a";

  // 密码
  String password = "yy0a";

  java.sql.Connection conn = null;
  try {
   Class.forName(driver);
   conn = DriverManager.getConnection(url, name, password);
  } catch (Exception e) {
   System.out.println("连接数据库错误:" + e.getMessage());
   System.out.println(e.toString());
   throw e;
  }
  return conn;
 }
 
 private static String formatTime_Static(Date date) throws Exception {
  return dateFormat.format(date);
 }
 
 private static void printException_Static(String sql, String message, Exception e)throws Exception {
  System.out.println("SQL:"+sql);
  System.out.println("MESSAGE:"+message);
  System.out.println(e.toString());
  e.printStackTrace();
  throw e;
 }
 private static void printExceptionNoEx_Static(String sql, String message, Exception e){
 
  System.out.println("SQL:"+sql);
  System.out.println("MESSAGE:"+message);
  System.out.println(e.toString());
  e.printStackTrace();
 }

 
}


package zhongheling.java.sql.manage;
 
import java.util.Date;
import zhongheling.java.sql.adapter.DBAdapter;
import zhongheling.java.sql.face.IDBHelper;
import zhongheling.java.sql.pack.PreparedStatementPack;
import zhongheling.java.sql.pack.ResultSetPack;

public abstract class DBManageSuper {

 // 得到包装类对象,如果为空的话则不启用
 public static ResultSetPack rsPack = null;
 
 public static PreparedStatementPack psPack=new PreparedStatementPack();;
 //适配器
 public static DBAdapter adpater=null;
 
 //得到连接
 public abstract java.sql.Connection getConnection()throws Exception;
 
 //设置自动增长列
 public abstract void getIdentity(Class bealClass,IDBHelper db)throws Exception;

 //异常处理 并抛出
 public abstract   void printException(String sql, String message, Exception e)throws Exception;
 
 //异常处理 不抛出
 public abstract   void printExceptionNoEx(String sql, String message, Exception e);
 
 //入库时间格式设置
 public abstract   String formatTime(Date date) throws Exception ;
}


package zhongheling.java.sql.pack;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PreparedStatementPack {
 
 private static  PreparedStatement prem;

 public void  setPreparedStatement(PreparedStatement prem)
 {
  PreparedStatementPack.prem=prem;
 }
 public static void  setBoolean(Integer index , Boolean value) throws SQLException {
 
    prem.setBoolean(index, value);
 }

 public static void setByte(Integer index , Byte value) throws Exception {

  prem.setByte(index, value);
 }

 public static void setShort(Integer index , Short value) throws Exception {

  prem.setShort(index, value);
 }

 public static void setInt(Integer index , Integer value) throws Exception {

  prem.setInt(index, value);
 }

 public static void setLong(Integer index , Long value) throws Exception {

  prem.setLong(index, value);
 }

 public static void setFloat(Integer index , Float value) throws Exception {

  prem.setFloat(index, value);

 }

 public static void setDouble(Integer index , Double value) throws Exception {

  prem.setDouble(index, value);

 }
 
 public static void setString(Integer index , String value) throws Exception {

  prem.setString(index.intValue(), value);

 }
 
 public static void setDate(Integer index , java.sql.Date value ) throws Exception {

  prem.setDate(index, value);

 }


}


package zhongheling.java.sql.pack;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class ResultSetPack  {

 private static ResultSet resultSet;
 
 public static Boolean getBoolean(String columnLabel) throws SQLException {

  return new Boolean(resultSet.getBoolean(columnLabel));
 }

 public static Byte getByte(String columnLabel) throws SQLException {

  return new Byte(resultSet.getByte(columnLabel));
 }

 public static Short getShort(String columnLabel) throws SQLException {

  return new Short(resultSet.getShort(columnLabel));
 }

 public static Integer getInt(String columnLabel) throws SQLException {

  return new Integer(resultSet.getInt(columnLabel));
 }

 public static Long getLong(String columnLabel) throws SQLException {

  return new Long(resultSet.getLong(columnLabel));
 }

 public static Float getFloat(String columnLabel) throws SQLException {

  return new Float(resultSet.getFloat(columnLabel));

 }

 public static Double getDouble(String columnLabel) throws SQLException {

  return new Double(resultSet.getDouble(columnLabel));

 }

 public static String getString(String columnLabel) throws SQLException {

  return resultSet.getString(columnLabel);

 }

 public static Date getDate(String columnLabel) throws SQLException {

  return resultSet.getDate(columnLabel);

 }

 public static Boolean getBoolean(int columnIndex) throws SQLException {

  return new Boolean(resultSet.getBoolean(columnIndex));

 }

 public static Byte getByte(int columnIndex) throws SQLException {

  return new Byte(resultSet.getByte(columnIndex));

 }

 public static Short getShort(int columnIndex) throws SQLException {

  return new Short(resultSet.getShort(columnIndex));

 }

 public static Integer getInt(int columnIndex) throws SQLException {

  return new Integer(resultSet.getInt(columnIndex));

 }

 public static Long getLong(int columnIndex) throws SQLException {

  return new Long(resultSet.getLong(columnIndex));

 }

 public static Float getFloat(int columnIndex) throws SQLException {

  return new Float(resultSet.getFloat(columnIndex));

 }

 public static Double getDouble(int columnIndex) throws SQLException {

  return new Double(resultSet.getDouble(columnIndex));

 }

 public static String getString(int columnIndex) throws SQLException {

  return resultSet.getString(columnIndex);
 }

 public static Date getDate(int columnIndex) throws SQLException {

  return resultSet.getDate(columnIndex);
 }

 public static void setResultSet(ResultSet rs) {

  resultSet = rs;
 }

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qq454952101/archive/2011/04/29/6372802.aspx

原文地址:https://www.cnblogs.com/heling/p/2033691.html