DBConn.java~4~
上传用户: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.         // 传入ACCESS数据库路径及所要执行的SQL语句,返回int
  47.         int rowcount = 0;
  48.         Connection con = null;
  49.         Statement stmt = null;
  50.         try {
  51.             con = getConnection();
  52.             stmt = con.createStatement();
  53.             con.setAutoCommit(false);
  54.             rowcount = stmt.executeUpdate(sql);
  55.             con.commit();
  56.         } catch (SQLException ex) {
  57.             System.out.println("sql-----------" + sql);
  58.             ex.printStackTrace();
  59.             try {
  60.                 if (con != null) {
  61.                     con.rollback();
  62.                 }
  63.             } catch (SQLException e) {
  64.                 e.printStackTrace();
  65.             }
  66.         } catch (UnsupportedOperationException ex) {
  67.             ex.printStackTrace();
  68.         } finally {
  69.             // 释放资源
  70.             if (stmt != null) {
  71.                 try {
  72.                     stmt.close();
  73.                     stmt = null;
  74.                 } catch (Exception e) {
  75.                     e.printStackTrace();
  76.                 }
  77.             }
  78.             if (con != null) {
  79.                 try {
  80.                     con.close();
  81.                     con = null;
  82.                 } catch (Exception e) {
  83.                     e.printStackTrace();
  84.                 }
  85.             }
  86.         }
  87.         return rowcount;
  88.     }
  89.     public String[][] getArray(String sql) {
  90.         ResultSet rs = null;
  91.         Connection con = null;
  92.         Statement stmt = null;
  93.         if (sql != null) {
  94.             try {
  95.                 con = getConnection();
  96.                 stmt = con.createStatement();
  97.                 rs = stmt.executeQuery(sql);
  98.                 ArrayList list = new ArrayList();
  99.                 if (rs != null) {
  100.                     while (rs.next()) {
  101.                         ArrayList columns = new ArrayList();
  102.                         int id = 1;
  103.                         while (true) {
  104.                             try {
  105.                                 String temp = rs.getString(id);
  106.                                 id++;
  107.                                 columns.add(temp);
  108.                             } catch (Exception e) {
  109.                                 list.add(columns);
  110.                                 break;
  111.                             }
  112.                         }
  113.                     }
  114.                 }
  115.                 if (list != null && list.size() > 0) {
  116.                     String[][] temp = new String[list.size()][];
  117.                     ArrayList tempsub = null;
  118.                     for (int i = 0; i < list.size(); i++) {
  119.                         tempsub = (ArrayList) list.get(i);
  120.                         temp[i] = new String[tempsub.size()];
  121.                         for (int j = 0; j < tempsub.size(); j++) {
  122.                             temp[i][j] = (String) tempsub.get(j);
  123.                         }
  124.                     }
  125.                     return temp;
  126.                 }
  127.             } catch (SQLException e) {
  128.                 if (con == null) {
  129.                     System.out.println("不能得到连接");
  130.                 }
  131.                 e.printStackTrace();
  132.             } finally {
  133.                 if (rs != null) {
  134.                     try {
  135.                         rs.close();
  136.                         rs = null;
  137.                     } catch (Exception e) {
  138.                         e.printStackTrace();
  139.                     }
  140.                 }
  141.                 if (stmt != null) {
  142.                     try {
  143.                         stmt.close();
  144.                         stmt = null;
  145.                     } catch (Exception e) {
  146.                         e.printStackTrace();
  147.                     }
  148.                 }
  149.                 if (con != null) {
  150.                     try {
  151.                         con.close();
  152.                         con = null;
  153.                     } catch (Exception e) {
  154.                         e.printStackTrace();
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.         return null;
  160.     }
  161.     public void connClose(Connection conn) {
  162.         try {
  163.             if (conn != null) {
  164.                 conn.close();
  165.             }
  166.         } catch (Exception ex) {
  167.             ex.printStackTrace();
  168.         }
  169.     }
  170. }