DBConn.java~5~
资源名称:bangong.rar [点击查看]
上传用户:dlqqsh
上传日期:2021-11-13
资源大小:7840k
文件大小:6k
源码类别:
OA系统
开发平台:
Java
- package officeol.mc.tools;
- import java.sql.*;
- import java.util.*;
- import com.microsoft.sqlserver.jdbc.SQLServerDriver;
- import javax.xml.xpath.XPath;
- import javax.xml.xpath.XPathFactory;
- import org.xml.sax.InputSource;
- public class DBConn {
- private Connection conn;
- private String xmlName = null;
- public DBConn() {
- }
- public String getXMLPath(String xmlname) {
- this.xmlName = xmlname;
- String path =
- this.getClass().getClassLoader().getResource("/").getPath()
- + "\" + xmlName;
- System.out.println(path);
- return path;
- }
- public Connection getConnection() {
- try {
- String getpath = this.getXMLPath("user.xml");
- String get[] = new String[4];
- XPath xpathEngine = XPathFactory.newInstance().newXPath();
- String xpathExpression = "/Root/port/text()";
- InputSource xmlSource = new InputSource(getpath);
- get[0] = xpathEngine.evaluate(xpathExpression, xmlSource); // 端口号
- xpathExpression = "/Root/dbname/text()";
- get[1] = xpathEngine.evaluate(xpathExpression, xmlSource); // 数据库名
- xpathExpression = "/Root/user/text()";
- get[2] = xpathEngine.evaluate(xpathExpression, xmlSource); // 用户名
- xpathExpression = "/Root/password/text()";
- get[3] = xpathEngine.evaluate(xpathExpression, xmlSource); // 用户密码
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- this.conn = DriverManager.getConnection(
- "jdbc:sqlserver://localhost:" + get[0] + ";DatabaseName="
- + get[1], get[2], get[3]);
- } catch (Exception ex) {
- System.out.println("数据库连接错误!,错误信息如下:");
- System.out.print(ex.getMessage());
- }
- return conn;
- }
- public int executeUpdate(String sql) {
- int rowcount = 0;
- Connection con = null;
- Statement stmt = null;
- try {
- con = getConnection();
- stmt = con.createStatement();
- con.setAutoCommit(false);
- rowcount = stmt.executeUpdate(sql);
- con.commit();
- } catch (SQLException ex) {
- System.out.println("sql-----------" + sql);
- ex.printStackTrace();
- try {
- if (con != null) {
- con.rollback();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } catch (UnsupportedOperationException ex) {
- ex.printStackTrace();
- } finally {
- // 释放资源
- if (stmt != null) {
- try {
- stmt.close();
- stmt = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return rowcount;
- }
- public String[][] getArray(String sql) {
- ResultSet rs = null;
- Connection con = null;
- Statement stmt = null;
- if (sql != null) {
- try {
- con = getConnection();
- stmt = con.createStatement();
- rs = stmt.executeQuery(sql);
- ArrayList list = new ArrayList();
- if (rs != null) {
- while (rs.next()) {
- ArrayList columns = new ArrayList();
- int id = 1;
- while (true) {
- try {
- String temp = rs.getString(id);
- id++;
- columns.add(temp);
- } catch (Exception e) {
- list.add(columns);
- break;
- }
- }
- }
- }
- if (list != null && list.size() > 0) {
- String[][] temp = new String[list.size()][];
- ArrayList tempsub = null;
- for (int i = 0; i < list.size(); i++) {
- tempsub = (ArrayList) list.get(i);
- temp[i] = new String[tempsub.size()];
- for (int j = 0; j < tempsub.size(); j++) {
- temp[i][j] = (String) tempsub.get(j);
- }
- }
- return temp;
- }
- } catch (SQLException e) {
- if (con == null) {
- System.out.println("不能得到连接");
- }
- e.printStackTrace();
- } finally {
- if (rs != null) {
- try {
- rs.close();
- rs = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- stmt = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (con != null) {
- try {
- con.close();
- con = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- return null;
- }
- public void connClose(Connection conn) {
- try {
- if (conn != null) {
- conn.close();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }