/**
* 通用的数据库操作类,可以动态的生成增加,删除,修改,查找方法,并且自动把查出的信息覆值到bean实体返回
* 此类用于在Formbean中使用,Bean中的private 变量应该与后台数据库表的字段命名相同。
* 如果private变量与后台没有必然的联系请用 nn开头 如:nnAddbtn,
* bean的命名会被翻译成后台数据库表的命名 如:className 会被翻译成 tbl_className
* @author GuoPeng
* Created on : 2004.6.12
*/import java.sql.*;
import java.util.*;
import org.apache.log4j.*;
import java.lang.reflect.*;
import com.huawei.nsp.smtask.connectioncache.*;public class DatabaseManage {
private Category logger = Category.getInstance(DatabaseManage.class);
private static boolean verbose = false;
/**
* 通用的ActionFormBean赋值的方法,如果查到符合查询条件的就返回列表,如果没有就实例化一个返回。
*
* @param 指定的查询Sql
* @param className,Sql相对应得JavaBean/FormBean类的名字
* @return 以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集
* 合的className对象的转化
*/
public ArrayList select(String sql, String className) {
Connection connection = CacheConnection.checkOut();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList paraList = new ArrayList();
try {
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
String recordValue = "";
Object c1 = null;
paraList = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
c1 = Class.forName(className).newInstance();
for (int i = 1; i <= columnCount; i++) {
if (rs.getString(rsmd.getColumnName(i)) != null) {
recordValue = rs.getString(rsmd.getColumnName(i));
}
else {
recordValue = "";
}
Method m = c1.getClass().getMethod(getSetMethodName(rsmd.
getColumnName(i)), new Class[] {recordValue.getClass()});
m.invoke(c1, new Object[] {recordValue});
}
paraList.add(c1);
}
if (paraList.size() < 1) {
c1 = Class.forName(className).newInstance();
recordValue = "";
for (int i = 1; i <= columnCount; i++) {
Method m = c1.getClass().getMethod(getSetMethodName(rsmd.
getColumnName(i)), new Class[] {recordValue.getClass()});
m.invoke(c1, new Object[] {recordValue});
}
}
paraList.add(c1);
}
catch (SQLException ex) {
ex.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
finally {
try {
rs.close();
rs = null;
}
catch (Exception ex) {
}
try {
stmt.close();
stmt = null;
}
catch (Exception ex) {
}
}
CacheConnection.checkIn(connection);
return paraList;
}
* 通用的数据库操作类,可以动态的生成增加,删除,修改,查找方法,并且自动把查出的信息覆值到bean实体返回
* 此类用于在Formbean中使用,Bean中的private 变量应该与后台数据库表的字段命名相同。
* 如果private变量与后台没有必然的联系请用 nn开头 如:nnAddbtn,
* bean的命名会被翻译成后台数据库表的命名 如:className 会被翻译成 tbl_className
* @author GuoPeng
* Created on : 2004.6.12
*/import java.sql.*;
import java.util.*;
import org.apache.log4j.*;
import java.lang.reflect.*;
import com.huawei.nsp.smtask.connectioncache.*;public class DatabaseManage {
private Category logger = Category.getInstance(DatabaseManage.class);
private static boolean verbose = false;
/**
* 通用的ActionFormBean赋值的方法,如果查到符合查询条件的就返回列表,如果没有就实例化一个返回。
*
* @param 指定的查询Sql
* @param className,Sql相对应得JavaBean/FormBean类的名字
* @return 以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集
* 合的className对象的转化
*/
public ArrayList select(String sql, String className) {
Connection connection = CacheConnection.checkOut();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList paraList = new ArrayList();
try {
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
String recordValue = "";
Object c1 = null;
paraList = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
c1 = Class.forName(className).newInstance();
for (int i = 1; i <= columnCount; i++) {
if (rs.getString(rsmd.getColumnName(i)) != null) {
recordValue = rs.getString(rsmd.getColumnName(i));
}
else {
recordValue = "";
}
Method m = c1.getClass().getMethod(getSetMethodName(rsmd.
getColumnName(i)), new Class[] {recordValue.getClass()});
m.invoke(c1, new Object[] {recordValue});
}
paraList.add(c1);
}
if (paraList.size() < 1) {
c1 = Class.forName(className).newInstance();
recordValue = "";
for (int i = 1; i <= columnCount; i++) {
Method m = c1.getClass().getMethod(getSetMethodName(rsmd.
getColumnName(i)), new Class[] {recordValue.getClass()});
m.invoke(c1, new Object[] {recordValue});
}
}
paraList.add(c1);
}
catch (SQLException ex) {
ex.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
finally {
try {
rs.close();
rs = null;
}
catch (Exception ex) {
}
try {
stmt.close();
stmt = null;
}
catch (Exception ex) {
}
}
CacheConnection.checkIn(connection);
return paraList;
}
解决方案 »
- MyEclipse 动态网站开发
- web系统登录问题,急在线等
- ejb 如何进行分布式部署
- MyEclipse JSP action 奇怪现象,大家有没有遇到过?
- 那位高手可以给我一个JAVA学习的一个好些的流程
- 求解,软件工程大三学生 水平要到什么程度才能找到好的实习
- 跪求WebLogic8.1 Server For AIX5.1 或 AIX5.2 的安装程序!!!
- 深圳程序员请留下MSN联系号码
- 做一个ejb后报以下错误:求教!
- 请问我在XML中增加节点,怎么做?很急详细一点,就55分了,都给了!
- 部署struts时遇到了问题Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true.
- 數據插入表里後,前臺頁面不顯示新增的數據?
* 通用的ActionFormBean判断信息是否存在的方法
*
* @param 指定的查询Sql
* @param className,Sql相对应得JavaBean/FormBean类的名字
* @return 存在返回true,不存在返回false
*/
public boolean ifExistByFields(String[] fieldsName, String[] fieldsValue,
String className) {
if( verbose )
logger.debug("===>in ifExistByFields");
Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String SQLStr = "select count(*) as ct ";
SQLStr += " from " + getTableName(className) + " Where 1=1 ";
for (int i = 0; i < fieldsName.length; i++) {
SQLStr += " and " + fieldsName[i] + "='" + fieldsValue[i] + "'";
}
if (logger.isDebugEnabled() && verbose)
logger.debug("===>" + SQLStr); try {
connection = CacheConnection.checkOut();
stmt = connection.prepareStatement(SQLStr);
rs = stmt.executeQuery();
if (rs.next()) {
if (rs.getInt(1) > 0) {
return true;
}
else {
return false;
}
}
else {
if( verbose )
logger.debug("===>null is select");
return false;
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
}
catch (Exception ex) {
} try {
if (stmt != null) {
stmt.close();
stmt = null;
}
}
catch (Exception ex) {
}
CacheConnection.checkIn(connection);
}
return true;
}
* 通过传入的参数动态生成查询SQL
*
* @param fieldsName 字段名称
* @param fieldsValue 字段值
* @param className Formbean名称
*/
public ArrayList selectByFields(String[] fieldsName, String[] fieldsValue,
String className) {
if( verbose )
logger.debug("===>in selectByFields");
String SQLStr = "select ";
SQLStr += getFieldsNames(className);
SQLStr += " from " + getTableName(className) + " Where 1=1 ";
for (int i = 0; i < fieldsName.length; i++) {
SQLStr += " and " + fieldsName[i] + "='" + fieldsValue[i] + "'";
}
if (logger.isDebugEnabled() && verbose )
logger.debug("===>" + SQLStr);
return this.select(SQLStr, className);
} /**
* 通过传入的参数动态生成删除SQL
*
* @param fieldsName 字段名称
* @param fieldsValue 字段值
* @param className Formbean名称
*/
public void deleteByFields(String[] fieldsName, String[] fieldsValue,
String className) {
if( verbose )
logger.debug("===>in deleteByFields");
String SQLStr = "delete from " + getTableName(className) + " Where 1=1 ";
for (int i = 0; i < fieldsName.length; i++) {
SQLStr += " and " + fieldsName[i] + "='" + fieldsValue[i] + "'";
}
if (logger.isDebugEnabled() && verbose)
logger.debug("===>" + SQLStr);
executeUpdate(SQLStr);
} /**
* 通过传入的参数生成Update SQL
*
* @param obj Formbean实例
* @param className 实例名称
*/
public void update(String[] fieldsName, String[] fieldsValue, Object obj) {
if( verbose )
logger.debug("===>in update");
String className = obj.getClass().getName();
String SQLStr = "update " + getTableName(className) + " set ";
String SQLSet = "";
String[] fieldsNames = getFieldsNames(className).split(",");
try {
for (int i = 0; i < fieldsNames.length; i++) {
//通过反射机制来获得传入参数obj的get方法,从而取出obj装载的信息。
Method getMethod = obj.getClass().getMethod(getGetMethodName(
fieldsNames[i]), new Class[] {});
String valueStr = (String) getMethod.invoke(obj, new Object[] {});
if (null != valueStr) {
if (SQLSet.length() == 0)
SQLSet += fieldsNames[i] + " = '" + valueStr + "'";
else
SQLSet += "," + fieldsNames[i] + " = '" + valueStr + "'";
}
}
SQLStr += SQLSet + " where 1=1 ";
for (int i = 0; i < fieldsName.length; i++) {
SQLStr += " and " + fieldsName[i] + "='" + fieldsValue[i] + "'";
}
if (logger.isDebugEnabled() && verbose )
logger.debug("===>" + SQLStr);
executeUpdate(SQLStr);
}
catch (Exception e) {
e.printStackTrace();
}
}