DAO.java
上传用户:dlqqsh
上传日期:2021-11-13
资源大小:7840k
文件大小:19k
源码类别:

OA系统

开发平台:

Java

  1. package officeol.mc.tools;
  2. import java.io.*;
  3. import java.lang.reflect.*;
  4. import java.sql.*;
  5. import java.util.*;
  6. import javax.naming.*;
  7. import javax.sql.DataSource;
  8. public class DAO {
  9.     public static String Log = "OFF";
  10.     public static String username = "";
  11.     public DAO() {
  12.         super();
  13.     }
  14.     public static int executeUpdate(String sql) {
  15.         int rowcount = 0;
  16.         Connection con = null;
  17.         Statement stmt = null;
  18.         try {
  19.             con = getConnection();
  20.             stmt = con.createStatement();
  21.             con.setAutoCommit(false);
  22.             rowcount = stmt.executeUpdate(sql);
  23.             con.commit();
  24.         } catch (SQLException ex) {
  25.             System.out.println("sql-----------" + sql);
  26.             ex.printStackTrace();
  27.             try {
  28.                 if (con != null) {
  29.                     con.rollback();
  30.                 }
  31.             } catch (SQLException e) {
  32.                 e.printStackTrace();
  33.             }
  34.         } catch (UnsupportedOperationException ex) {
  35.             ex.printStackTrace();
  36.         } finally {
  37.             //释放资源
  38.             if (stmt != null) {
  39.                 try {
  40.                     stmt.close();
  41.                     stmt = null;
  42.                 } catch (Exception e) {
  43.                     e.printStackTrace();
  44.                 }
  45.             }
  46.             if (con != null) {
  47.                 try {
  48.                     con.close();
  49.                     con = null;
  50.                 } catch (Exception e) {
  51.                     e.printStackTrace();
  52.                 }
  53.             }
  54.         }
  55.         return rowcount;
  56.     }
  57.     public static int executeUpdate(String sql, String s) {
  58.         int rowcount = 0;
  59.         Connection con = null;
  60.         Statement stmt = null;
  61.         try {
  62.             con = getConnection();
  63.             stmt = con.createStatement();
  64.             rowcount = stmt.executeUpdate(sql);
  65.         } catch (SQLException ex) {
  66.             ex.printStackTrace();
  67.         } catch (UnsupportedOperationException ex) {
  68.             ex.printStackTrace();
  69.         } finally {
  70.             //释放资源
  71.             if (stmt != null) {
  72.                 try {
  73.                     stmt.close();
  74.                     stmt = null;
  75.                 } catch (Exception e) {
  76.                     e.printStackTrace();
  77.                 }
  78.             }
  79.             if (con != null) {
  80.                 try {
  81.                     con.close();
  82.                     con = null;
  83.                 } catch (Exception e) {
  84.                     e.printStackTrace();
  85.                 }
  86.             }
  87.         }
  88.         return rowcount;
  89.     }
  90.     public static int executeProcedure(String procname, String para) {
  91.         int rowcount = 0;
  92.         Connection con = null;
  93.         CallableStatement cstmt = null;
  94.         try {
  95.             con = getConnection();
  96.             cstmt = con.prepareCall("{call " + procname + "(?)}");
  97.             con.setAutoCommit(false);
  98.             cstmt.setString(1, para);
  99.             rowcount = cstmt.executeUpdate();
  100.             con.commit();
  101.         } catch (SQLException ex) {
  102.             try {
  103.                 if (con != null) {
  104.                     con.rollback();
  105.                 }
  106.             } catch (SQLException e) {
  107.                 e.printStackTrace();
  108.             }
  109.         } catch (UnsupportedOperationException ex) {
  110.             ex.printStackTrace();
  111.         } finally {
  112.             //释放资源
  113.             if (cstmt != null) {
  114.                 try {
  115.                     cstmt.close();
  116.                     cstmt = null;
  117.                 } catch (Exception e) {
  118.                     e.printStackTrace();
  119.                 }
  120.             }
  121.             if (con != null) {
  122.                 try {
  123.                     con.close();
  124.                     con = null;
  125.                 } catch (Exception e) {
  126.                     e.printStackTrace();
  127.                 }
  128.             }
  129.         }
  130.         return rowcount;
  131.     }
  132.     /**
  133.      * 创建一个数据库连接
  134.      * 如果在应用服务器环境中配置了数据源,则从数据源中取. 否则自己创建.
  135.      * @return 一个数据库连接对象
  136.      */
  137.     public static Connection getConnection() throws SQLException {
  138.         Connection conn = null;
  139.         /*      String driver = Config.getDriver();
  140.               String url = Config.getUrl();
  141.               String username = Config.getUsername();
  142.               String password = Config.getPassword();
  143.               try {
  144.                   Class.forName(driver);
  145.               } catch (ClassNotFoundException ex) {
  146.                   ex.printStackTrace();
  147.               }
  148.               conn = DriverManager.getConnection(url, username,
  149.                       password);*/
  150.         try {
  151.             System.out.println("开始读数据源");
  152.             DataSource ds = null;
  153.             Context initCtx = new InitialContext();
  154.             Context ctx = (Context) initCtx.lookup("java:comp/env");
  155.             //获取数据源对象
  156.             Object obj = (Object) ctx.lookup("jdbc/train");
  157.             ds = (javax.sql.DataSource) obj;
  158.             conn=ds.getConnection();
  159.         } catch (Exception ex) {
  160.             ex.printStackTrace();
  161.             System.out.println("无法获得连接");
  162.         }
  163.         return conn;
  164.     }
  165.     public static Collection generalQuery(String sql, int first,
  166.                                           int recNum,
  167.                                           Class infoClass) {
  168.         Connection con = null;
  169.         Statement stmt = null;
  170.         try {
  171.             //获得连接,创建ResultSet
  172.             con = getConnection();
  173.             stmt = con.createStatement(ResultSet.
  174.                                        TYPE_SCROLL_INSENSITIVE,
  175.                                        ResultSet.
  176.                                        CONCUR_READ_ONLY);
  177.             ResultSet rs = stmt.executeQuery(sql);
  178.             //找到第一条有效记录
  179.             boolean hasRow = true;
  180.             for (int i = 0; i < first && hasRow; i++) {
  181.                 hasRow = rs.next();
  182.             }
  183.             if (!hasRow && recNum != 0) { //结果集数据达不到first个数,此时取最后的几个
  184.                 //取最后一次的结果
  185.                 rs.last();
  186.                 int maxSize = rs.getRow();
  187.                 int begin = maxSize / recNum;
  188.                 int num = maxSize % recNum;
  189.                 first = begin * recNum;
  190.                 recNum = num;
  191.                 hasRow = true;
  192.             }
  193.             //要取得的个数是否超界
  194.             rs.last();
  195.             int maxSize = rs.getRow();
  196.             rs.first();
  197.             if (first + recNum > maxSize) {
  198.                 recNum = maxSize - first + 1;
  199.             }
  200.             //用来保存结果的类数组,就是将ResultSet结果赋给类数组
  201.             ArrayList cs = new ArrayList();
  202.             rs.first();
  203.             if (first == 1) {} else {
  204.                 if (first != 0) {
  205.                     rs.absolute(first);
  206.                 }
  207.             }
  208.             Hashtable classFields = new Hashtable();
  209.             List fieldList = new ArrayList();
  210.             Field[] fields = null;
  211.             Class superClass = infoClass;
  212.             while (!superClass.getName().equalsIgnoreCase("java.lang.Object")) {
  213.                 fields = superClass.getDeclaredFields();
  214.                 for (int i = 0; i < fields.length; ++i) {
  215.                     fieldList.add(fields[i]);
  216.                 }
  217.                 superClass = superClass.getSuperclass();
  218.             }
  219.             fields = new Field[fieldList.size()];
  220.             fieldList.toArray(fields);
  221.             ResultSetMetaData rsmd = rs.getMetaData();
  222.             //将ResultSet中的列的序号与类的属性对应起来
  223.             int colCount = rsmd.getColumnCount();
  224.             for (int i = 0; i < colCount; i++) {
  225.                 String colName = rsmd.getColumnName(i + 1); //列名
  226.                 for (int j = 0; j < fields.length; j++) {
  227.                     if (colName.toUpperCase().equals(fields[j].
  228.                             getName().
  229.                             toUpperCase())) {
  230.                         classFields.put(new Integer(i + 1),
  231.                                         fields[j]);
  232.                         break;
  233.                     }
  234.                 }
  235.             }
  236.             //浏览ResultSet,将内容写入ArrayList
  237.             if (recNum == 0) { //赋最大值
  238.                 recNum = maxSize;
  239.             }
  240.             for (int i = 0; i < recNum && hasRow; i++) {
  241.                 try {
  242.                     Object row = infoClass.newInstance();
  243.                     ArrayList colArray = new ArrayList();
  244.                     for (Enumeration enum1 = classFields.keys();
  245.                                              enum1.hasMoreElements(); ) {
  246.                         colArray.add(enum1.nextElement());
  247.                     }
  248.                     Collections.sort(colArray);
  249.                     for (Iterator cols_it = colArray.iterator();
  250.                                             cols_it.hasNext(); ) {
  251.                         Integer colIdx = (Integer) cols_it.next();
  252.                         Field theField = (Field) classFields.get(
  253.                                 colIdx);
  254.                         Class paraType = theField.getType();
  255.                         theField.setAccessible(true);
  256.                         if (paraType.getName().equals("boolean")) {
  257.                             String temp = rs.getString(colIdx.
  258.                                     intValue());
  259.                             boolean value = (temp == null ||
  260.                                              temp.length() == 0 ||
  261.                                              temp.charAt(0) ==
  262.                                              '0') ? false : true;
  263.                             theField.setBoolean(row, value);
  264.                         } else if (paraType.getName().equals(
  265.                                 "byte")) {
  266.                             theField.setByte(row,
  267.                                              rs.getByte(colIdx.
  268.                                     intValue()));
  269.                         } else if (paraType.getName().equals(
  270.                                 "short")) {
  271.                             theField.setShort(row,
  272.                                               rs.getShort(colIdx.
  273.                                     intValue()));
  274.                         } else if (paraType.getName().equals("int")) {
  275.                             theField.setInt(row,
  276.                                             rs.getInt(colIdx.
  277.                                     intValue()));
  278.                         } else if (paraType.getName().equals(
  279.                                 "long")) {
  280.                             theField.setLong(row,
  281.                                              rs.getLong(colIdx.
  282.                                     intValue()));
  283.                         } else if (paraType.getName().equals(
  284.                                 "float")) {
  285.                             theField.setFloat(row,
  286.                                               rs.getFloat(colIdx.
  287.                                     intValue()));
  288.                         } else if (paraType.getName().equals(
  289.                                 "double")) {
  290.                             theField.setDouble(row,
  291.                                                rs.getDouble(colIdx.
  292.                                     intValue()));
  293.                         } else if (paraType.getName().equals(
  294.                                 "char")) {
  295.                             String temp = rs.getString(colIdx.
  296.                                     intValue());
  297.                             char value = (temp == null ||
  298.                                           temp.length() == 0) ?
  299.                                          '' :
  300.                                          temp.charAt(0);
  301.                             theField.setChar(row, value);
  302.                         } else if (paraType.getName().indexOf(
  303.                                 "String") != -1) {
  304.                             String temp = rs.getString(colIdx.
  305.                                     intValue());
  306.                             if (temp == null) {
  307.                                 temp = "";
  308.                             }
  309.                             theField.set(row, temp);
  310.                         } else if (paraType.getName().indexOf(
  311.                                 "Date") != -1) {
  312.                             java.sql.Date tempDate = rs.getDate(
  313.                                     colIdx.intValue());
  314.                             theField.set(row, tempDate);
  315.                         }
  316.                         //处理byte[]
  317.                         else if (paraType.isArray() &&
  318.                                  paraType.getComponentType().
  319.                                  getName().equals("byte")) {
  320.                             byte[] tempBytes = rs.getBytes(
  321.                                     colIdx.intValue());
  322.                             theField.set(row, tempBytes);
  323.                         } else {
  324.                             theField.set(row,
  325.                                          rs.getObject(colIdx.
  326.                                     intValue()));
  327.                         }
  328.                     }
  329.                     cs.add(row);
  330.                 } catch (Exception e) {
  331.                     //throw new RuntimeException(
  332.                     //      "进行通用查询中的类型转换时出现问题:" + e.getMessage());
  333.                     e.printStackTrace();
  334.                 }
  335.                 //下一行
  336.                 hasRow = rs.next();
  337.             }
  338.             //返回结果
  339.             return cs;
  340.         } catch (Exception e) {
  341.             e.printStackTrace();
  342.         } finally {
  343.             //释放资源
  344.             if (stmt != null) {
  345.                 try {
  346.                     stmt.close();
  347.                     stmt = null;
  348.                 } catch (Exception e) {
  349.                     e.printStackTrace();
  350.                 }
  351.             }
  352.             if (con != null) {
  353.                 try {
  354.                     con.close();
  355.                     con = null;
  356.                 } catch (Exception e) {
  357.                     e.printStackTrace();
  358.                 }
  359.             }
  360.         }
  361.         return null;
  362.     }
  363.     public static String[][] getArray(String sql) {
  364.         ResultSet rs = null;
  365.         Connection con = null;
  366.         Statement stmt = null;
  367.         if (sql != null) {
  368.             try {
  369.                 con = getConnection();
  370.                 stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  371.                                            ResultSet.CONCUR_READ_ONLY);
  372.                 rs = stmt.executeQuery(sql);
  373.                 ArrayList list = new ArrayList();
  374.                 if (rs != null) {
  375.                     while (rs.next()) {
  376.                         ArrayList columns = new ArrayList();
  377.                         int id = 1;
  378.                         while (true) {
  379.                             try {
  380.                                 String temp = rs.getString(id);
  381.                                 id++;
  382.                                 columns.add(temp);
  383.                             } catch (Exception e) {
  384.                                 list.add(columns);
  385.                                 break;
  386.                             }
  387.                         }
  388.                     }
  389.                 }
  390.                 if (list != null && list.size() > 0) {
  391.                     String[][] temp = new String[list.size()][];
  392.                     ArrayList tempsub = null;
  393.                     for (int i = 0; i < list.size(); i++) {
  394.                         tempsub = (ArrayList) list.get(i);
  395.                         temp[i] = new String[tempsub.size()];
  396.                         for (int j = 0; j < tempsub.size(); j++) {
  397.                             temp[i][j] = (String) tempsub.get(j);
  398.                         }
  399.                     }
  400.                     return temp;
  401.                 }
  402.             } catch (SQLException e) {
  403.                 if (con == null) {
  404.                     System.out.println("不能得到连接");
  405.                 }
  406.                 e.printStackTrace();
  407.             } finally {
  408.                 if (rs != null) {
  409.                     try {
  410.                         rs.close();
  411.                         rs = null;
  412.                     } catch (Exception e) {
  413.                         e.printStackTrace();
  414.                     }
  415.                 }
  416.                 if (stmt != null) {
  417.                     try {
  418.                         stmt.close();
  419.                         stmt = null;
  420.                     } catch (Exception e) {
  421.                         e.printStackTrace();
  422.                     }
  423.                 }
  424.                 if (con != null) {
  425.                     try {
  426.                         con.close();
  427.                         con = null;
  428.                     } catch (Exception e) {
  429.                         e.printStackTrace();
  430.                     }
  431.                 }
  432.             }
  433.         }
  434.         return null;
  435.     }
  436.     public static DatabaseMetaData getMetaData() {
  437.         Connection con = null;
  438.         try {
  439.             con = getConnection();
  440.             if (con != null) {
  441.                 return con.getMetaData();
  442.             } else {
  443.                 return null;
  444.             }
  445.         } catch (SQLException e) {
  446.             e.printStackTrace();
  447.         } finally {
  448.             if (con != null) {
  449.                 try {
  450.                     con.close();
  451.                     con = null;
  452.                 } catch (SQLException e) {
  453.                     e.printStackTrace();
  454.                 }
  455.             }
  456.         }
  457.         return null;
  458.     }
  459.     public static String[] getTableNames() {
  460.         try {
  461.             String[] types = new String[1];
  462.             types[0] = "TABLE";
  463.             ResultSet rs = getMetaData().getTables(null, null, null, types);
  464.             ArrayList namelist = new ArrayList();
  465.             if (rs != null) {
  466.                 while (rs.next()) {
  467.                     String tablename = rs.getString("TABLE_NAME");
  468.                     if (!tablename.equals("dtproperties")) {
  469.                         namelist.add(tablename);
  470.                     }
  471.                 }
  472.             }
  473.             if (!namelist.isEmpty()) {
  474.                 String[] names = new String[namelist.size()];
  475.                 for (int i = 0; i < namelist.size(); i++) {
  476.                     names[i] = (String) namelist.get(i);
  477.                 }
  478.                 return names;
  479.             }
  480.         } catch (SQLException e) {
  481.             e.printStackTrace();
  482.         }
  483.         return null;
  484.     }
  485.     public static String toGB(String iso) {
  486.         try {
  487.             return new String(iso.getBytes("ISO-8859-1"), "GB2312");
  488.         } catch (UnsupportedEncodingException e) {
  489.             e.printStackTrace();
  490.         }
  491.         return iso;
  492.     }
  493. }