DBConnection.java
上传用户:xinkaifrp
上传日期:2021-05-08
资源大小:372k
文件大小:2k
- package com.financing.dao;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class DBConnection {
- private static String Driver;
- private static String url;
- private static String userName;
- private static String password;
- ResultSetMetaData rsmd = null;
- static Connection conn = null;
- static Statement stmt = null;
- ResultSet rs = null;
- public static Connection getConnection() {
- try {
- Properties p = new Properties();
- InputStream input = new FileInputStream("C:/student.properties");
- p.load(input);
- Driver = p.getProperty("Driver");
- url = p.getProperty("url");
- userName = p.getProperty("userName");
- password = p.getProperty("password");
- input.close();
- Class.forName(Driver);
- conn = DriverManager.getConnection(url, userName, password);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return conn;
- }
- public static Statement createStmt() {
- conn = DBConnection.getConnection();
- try {
- stmt = conn.createStatement();
- } catch (SQLException ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- return stmt;
- }
- public static void close(Statement stmt, ResultSet rs) {
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }