package sample;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author heavens
*/
public class ExecuteSqlUtil {
private Class clsDBAccess;
private Object objClass;
/**
* 调用默认构造方法,不带参数
*
* @param classname
* 调用的类名
*/
public ExecuteSqlUtil(String classname) {
try {
clsDBAccess = Class.forName(classname);
objClass = clsDBAccess.newInstance();
} catch (InstantiationException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**
* 调用带有参数的构造方法
*
* @param classname
* 调用类名
* @param parameterTypes
* 参数类型组
* @param parameterValues
* 参数值组
*/
public ExecuteSqlUtil(String classname, Class[] parameterTypes,
Object[] parameterValues) {
try {
clsDBAccess = Class.forName(classname);
Constructor cs = clsDBAccess.getDeclaredConstructor(parameterTypes);// 构造方法
objClass = cs.newInstance(parameterValues);
} catch (InstantiationException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (SecurityException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**
* 对update方法的封装
*
* @param sql
* @return
*/
public boolean updateSql(String sql) {
boolean flag = false;
try {
Class[] types = new Class[1];
types[0] = String.class;
Method mtd = clsDBAccess.getMethod("update", types);
mtd.invoke(objClass, sql);
Method mtd2 = clsDBAccess.getMethod("close");
mtd2.invoke(objClass);
flag = true;
} catch (IllegalArgumentException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (SecurityException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
}
return flag;
}
/**
* 对query方法的封装
*
* @param sql
* @return
*/
public ResultSet querySql(String sql) {
ResultSet rs = null;
try {
Class[] types = new Class[1];
types[0] = String.class;
Method mtd = clsDBAccess.getMethod("query", types);
rs = (ResultSet) mtd.invoke(objClass, sql);
} catch (IllegalArgumentException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (SecurityException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
}
return rs;
}
/**
* 关闭数据库连接
*
* @return
*/
public boolean closeConnection() {
boolean flag = false;
try {
Method mtd = clsDBAccess.getMethod("close");
mtd.invoke(objClass);
flag = true;
} catch (IllegalArgumentException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (SecurityException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
}
return flag;
}
public static void main(String argv[]) {
try {
ExecuteSqlUtil mysql = new ExecuteSqlUtil(
"ConnPoolLcl.DatabaseAccess",
new Class[] { boolean.class }, new Object[] { true });
ResultSet rs = mysql.querySql("select * from user");
while (rs.next()) {
System.out
.println(rs.getString(1) + "======" + rs.getString(2));
}
System.exit(0);
} catch (SQLException ex) {
Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}