DBConnection.java
上传用户:xinkaifrp
上传日期:2021-05-08
资源大小:372k
文件大小:2k
源码类别:

家庭/个人应用

开发平台:

Java

  1. package com.financing.dao;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.Properties;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. public class DBConnection {
  14.     private static String Driver;
  15.     private static String url;
  16.     private static String userName;
  17.     private static String password;
  18.     ResultSetMetaData rsmd = null;
  19.     static Connection conn = null;
  20.     static Statement stmt = null;
  21.     ResultSet rs = null;
  22.     public static Connection getConnection() {
  23.         try {
  24.             Properties p = new Properties();
  25.             InputStream input = new FileInputStream("C:/student.properties");
  26.             p.load(input);
  27.             Driver = p.getProperty("Driver");
  28.             url = p.getProperty("url");
  29.             userName = p.getProperty("userName");
  30.             password = p.getProperty("password");
  31.             input.close();
  32.             Class.forName(Driver);
  33.             conn = DriverManager.getConnection(url, userName, password);
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.         return conn;
  38.     }
  39.     public static Statement createStmt() {
  40.         conn = DBConnection.getConnection();
  41.         try {
  42.             stmt = conn.createStatement();
  43.         } catch (SQLException ex) {
  44.             Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  45.         }
  46.         return stmt;
  47.     }
  48.     public static void close(Statement stmt, ResultSet rs) {
  49.         if (stmt != null) {
  50.             try {
  51.                 stmt.close();
  52.             } catch (SQLException ex) {
  53.                 Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  54.             }
  55.         }
  56.         if (rs != null) {
  57.             try {
  58.                 rs.close();
  59.             } catch (SQLException ex) {
  60.                 Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  61.             }
  62.         }
  63.     }
  64. }