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

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.                         //this.getClass().getClassLoader().getResource("/").getPath()+ "\" + xmlName;
  25.                         String get[] = new String[4];
  26.                         XPath xpathEngine = XPathFactory.newInstance().newXPath();
  27.                         String xpathExpression = "/Root/port/text()";
  28.                         InputSource xmlSource = new InputSource(getpath);
  29.                         get[0] = xpathEngine.evaluate(xpathExpression, xmlSource);// 端口号
  30.                         xpathExpression = "/Root/dbname/text()";
  31.                         get[1] = xpathEngine.evaluate(xpathExpression, xmlSource);// 数据库名
  32.                         xpathExpression = "/Root/user/text()";
  33.                         get[2] = xpathEngine.evaluate(xpathExpression, xmlSource);// 用户名
  34.                         xpathExpression = "/Root/password/text()";
  35.                         get[3] = xpathEngine.evaluate(xpathExpression, xmlSource);// 用户密码
  36.                         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  37.                         this.conn = DriverManager.getConnection(
  38.                                         "jdbc:sqlserver://localhost:" + get[0] + ";DatabaseName="
  39.                                                         + get[1], get[2], get[3]);
  40.                 } catch (Exception ex) {
  41.                         System.out.println("数据库连接错误!,错误信息如下:");
  42.                         System.out.print(ex.getMessage());
  43.                 }
  44.                 return conn;
  45.         }
  46.         public int executeUpdate(String sql) {
  47.                 // 传入ACCESS数据库路径及所要执行的SQL语句,返回int
  48.                 int rowcount = 0;
  49.                 Connection con = null;
  50.                 Statement stmt = null;
  51.                 try {
  52.                         con = getConnection();
  53.                         stmt = con.createStatement();
  54.                         con.setAutoCommit(false);
  55.                         rowcount = stmt.executeUpdate(sql);
  56.                         con.commit();
  57.                 } catch (SQLException ex) {
  58.                         System.out.println("sql-----------" + sql);
  59.                         ex.printStackTrace();
  60.                         try {
  61.                                 if (con != null) {
  62.                                         con.rollback();
  63.                                 }
  64.                         } catch (SQLException e) {
  65.                                 e.printStackTrace();
  66.                         }
  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 String[][] getArray(String sql) {
  91.                 ResultSet rs = null;
  92.                 Connection con = null;
  93.                 Statement stmt = null;
  94.                 if (sql != null) {
  95.                         try {
  96.                                 con = getConnection();
  97.                                 stmt = con.createStatement();
  98.                                 rs = stmt.executeQuery(sql);
  99.                                 ArrayList list = new ArrayList();
  100.                                 if (rs != null) {
  101.                                         while (rs.next()) {
  102.                                                 ArrayList columns = new ArrayList();
  103.                                                 int id = 1;
  104.                                                 while (true) {
  105.                                                         try {
  106.                                                                 String temp = rs.getString(id);
  107.                                                                 id++;
  108.                                                                 columns.add(temp);
  109.                                                         } catch (Exception e) {
  110.                                                                 list.add(columns);
  111.                                                                 break;
  112.                                                         }
  113.                                                 }
  114.                                         }
  115.                                 }
  116.                                 if (list != null && list.size() > 0) {
  117.                                         String[][] temp = new String[list.size()][];
  118.                                         ArrayList tempsub = null;
  119.                                         for (int i = 0; i < list.size(); i++) {
  120.                                                 tempsub = (ArrayList) list.get(i);
  121.                                                 temp[i] = new String[tempsub.size()];
  122.                                                 for (int j = 0; j < tempsub.size(); j++) {
  123.                                                         temp[i][j] = (String) tempsub.get(j);
  124.                                                 }
  125.                                         }
  126.                                         return temp;
  127.                                 }
  128.                         } catch (SQLException e) {
  129.                                 if (con == null) {
  130.                                         System.out.println("不能得到连接");
  131.                                 }
  132.                                 e.printStackTrace();
  133.                         } finally {
  134.                                 if (rs != null) {
  135.                                         try {
  136.                                                 rs.close();
  137.                                                 rs = null;
  138.                                         } catch (Exception e) {
  139.                                                 e.printStackTrace();
  140.                                         }
  141.                                 }
  142.                                 if (stmt != null) {
  143.                                         try {
  144.                                                 stmt.close();
  145.                                                 stmt = null;
  146.                                         } catch (Exception e) {
  147.                                                 e.printStackTrace();
  148.                                         }
  149.                                 }
  150.                                 if (con != null) {
  151.                                         try {
  152.                                                 con.close();
  153.                                                 con = null;
  154.                                         } catch (Exception e) {
  155.                                                 e.printStackTrace();
  156.                                         }
  157.                                 }
  158.                         }
  159.                 }
  160.                 return null;
  161.         }
  162.         public void connClose(Connection conn) {
  163.                 try {
  164.                         if (conn != null) {
  165.                                 conn.close();
  166.                         }
  167.                 } catch (Exception ex) {
  168.                         ex.printStackTrace();
  169.                 }
  170.         }
  171. }