自定义连接数据库的util类
package com.shuzf.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtil { // 定义驱动器类的路径 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; // 定义用于连接数据库的URL private static final String URL = "jdbc:oracle:thin****l"; // 定义用于访问数据库的用户名及密码 private static final String USERNAME = "s****t"; private static final String PASSWORD = "t***r"; // 加载驱动器类 static { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } // 定义获得连接的方法 public static Connection getConnection() { Connection conn = null; ; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } // 定义释放数据库资源的方法 public static void destory(Connection con, Statement stat, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stat != null) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }