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); } } }