package com.hanqi.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcConnectionUtil {

    private static final String USERNAME = "test"; 
    private static final String PASSWORD = "test";
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
    private static final String DRIVERCLASSNAME = "oracle.jdbc.OracleDriver";

    public static Connection getConnection() { //链接数据库
        Connection conn = null;
        try {
            Class.forName(DRIVERCLASSNAME);
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void destroy(Connection conn, Statement sm, ResultSet rs) { //Statement 是PreparedStatement的一个父类
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (sm != null) {
            try {
                sm.close();
                sm = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

相关文章:

  • 2021-07-20
  • 2021-04-07
  • 2021-06-12
  • 2021-07-18
  • 2021-12-30
  • 2021-08-26
  • 2021-12-08
  • 2021-10-19
猜你喜欢
  • 2021-09-01
  • 2021-11-27
  • 2021-12-22
  • 2021-09-13
  • 2021-11-28
  • 2021-12-16
  • 2021-06-09
相关资源
相似解决方案