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

OA系统

开发平台:

Java

  1. package officeol.mc.tools;
  2. import java.sql.*;
  3. import java.util.*;
  4. import com.microsoft.sqlserver.jdbc.SQLServerDriver;
  5. import javax.xml.xpath.XPath;
  6. import javax.xml.xpath.XPathFactory;
  7. import org.xml.sax.InputSource;
  8. public class DBConn {
  9.     private Connection conn;
  10.     private String xmlName = null;
  11.     public DBConn() {
  12.     }
  13.     public String getXMLPath(String xmlname) {
  14.         this.xmlName = xmlname;
  15.         String path =
  16.                 this.getClass().getClassLoader().getResource("/").getPath()
  17.                 + "\" + xmlName;
  18.         System.out.println(path);
  19.         return path;
  20.     }
  21.     public Connection getConnection() {
  22.         try {
  23.             String getpath = this.getXMLPath("user.xml");
  24.             String get[] = new String[4];
  25.             XPath xpathEngine = XPathFactory.newInstance().newXPath();
  26.             String xpathExpression = "/Root/port/text()";
  27.             InputSource xmlSource = new InputSource(getpath);
  28.             get[0] = xpathEngine.evaluate(xpathExpression, xmlSource); // 端口号
  29.             xpathExpression = "/Root/dbname/text()";
  30.             get[1] = xpathEngine.evaluate(xpathExpression, xmlSource); // 数据库名
  31.             xpathExpression = "/Root/user/text()";
  32.             get[2] = xpathEngine.evaluate(xpathExpression, xmlSource); // 用户名
  33.             xpathExpression = "/Root/password/text()";
  34.             get[3] = xpathEngine.evaluate(xpathExpression, xmlSource); // 用户密码
  35.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  36.             this.conn = DriverManager.getConnection(
  37.                     "jdbc:sqlserver://localhost:" + get[0] + ";DatabaseName="
  38.                     + get[1], get[2], get[3]);
  39.         } catch (Exception ex) {
  40.             System.out.println("数据库连接错误!,错误信息如下:");
  41.             System.out.print(ex.getMessage());
  42.         }
  43.         return conn;
  44.     }
  45.     public int executeUpdate(String sql) {
  46.         int rowcount = 0;
  47.         Connection con = null;
  48.         Statement stmt = null;
  49.         try {
  50.             con = getConnection();
  51.             stmt = con.createStatement();
  52.             con.setAutoCommit(false);
  53.             rowcount = stmt.executeUpdate(sql);
  54.             con.commit();
  55.         } catch (SQLException ex) {
  56.             System.out.println("sql-----------" + sql);
  57.             ex.printStackTrace();
  58.             try {
  59.                 if (con != null) {
  60.                     con.rollback();
  61.                 }
  62.             } catch (SQLException e) {
  63.                 e.printStackTrace();
  64.             }
  65.         } catch (UnsupportedOperationException ex) {
  66.             ex.printStackTrace();
  67.         } finally {
  68.             // 释放资源
  69.             if (stmt != null) {
  70.                 try {
  71.                     stmt.close();
  72.                     stmt = null;
  73.                 } catch (Exception e) {
  74.                     e.printStackTrace();
  75.                 }
  76.             }
  77.             if (con != null) {
  78.                 try {
  79.                     con.close();
  80.                     con = null;
  81.                 } catch (Exception e) {
  82.                     e.printStackTrace();
  83.                 }
  84.             }
  85.         }
  86.         return rowcount;
  87.     }
  88.     public String[][] getArray(String sql) {
  89.         ResultSet rs = null;
  90.         Connection con = null;
  91.         Statement stmt = null;
  92.         if (sql != null) {
  93.             try {
  94.                 con = getConnection();
  95.                 stmt = con.createStatement();
  96.                 rs = stmt.executeQuery(sql);
  97.                 ArrayList list = new ArrayList();
  98.                 if (rs != null) {
  99.                     while (rs.next()) {
  100.                         ArrayList columns = new ArrayList();
  101.                         int id = 1;
  102.                         while (true) {
  103.                             try {
  104.                                 String temp = rs.getString(id);
  105.                                 id++;
  106.                                 columns.add(temp);
  107.                             } catch (Exception e) {
  108.                                 list.add(columns);
  109.                                 break;
  110.                             }
  111.                         }
  112.                     }
  113.                 }
  114.                 if (list != null && list.size() > 0) {
  115.                     String[][] temp = new String[list.size()][];
  116.                     ArrayList tempsub = null;
  117.                     for (int i = 0; i < list.size(); i++) {
  118.                         tempsub = (ArrayList) list.get(i);
  119.                         temp[i] = new String[tempsub.size()];
  120.                         for (int j = 0; j < tempsub.size(); j++) {
  121.                             temp[i][j] = (String) tempsub.get(j);
  122.                         }
  123.                     }
  124.                     return temp;
  125.                 }
  126.             } catch (SQLException e) {
  127.                 if (con == null) {
  128.                     System.out.println("不能得到连接");
  129.                 }
  130.                 e.printStackTrace();
  131.             } finally {
  132.                 if (rs != null) {
  133.                     try {
  134.                         rs.close();
  135.                         rs = null;
  136.                     } catch (Exception e) {
  137.                         e.printStackTrace();
  138.                     }
  139.                 }
  140.                 if (stmt != null) {
  141.                     try {
  142.                         stmt.close();
  143.                         stmt = null;
  144.                     } catch (Exception e) {
  145.                         e.printStackTrace();
  146.                     }
  147.                 }
  148.                 if (con != null) {
  149.                     try {
  150.                         con.close();
  151.                         con = null;
  152.                     } catch (Exception e) {
  153.                         e.printStackTrace();
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.         return null;
  159.     }
  160.     public void connClose(Connection conn) {
  161.         try {
  162.             if (conn != null) {
  163.                 conn.close();
  164.             }
  165.         } catch (Exception ex) {
  166.             ex.printStackTrace();
  167.         }
  168.     }
  169. }