package com.WR.StudentMS.dao.mysql; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; public class DBHelper { static String url; static Properties info = new Properties(); static { InputStream input = DBHelper.class.getClassLoader().getResourceAsStream("config.properties"); try { info.load(input); url = info.getProperty("url"); String driverClassName = info.getProperty("driver"); Class.forName(driverClassName); System.out.println("驱动加载成功"); }catch(ClassNotFoundException e) { System.out.println("驱动加载失败"); }catch(IOException e) { System.out.println("属性文件加载失败"); } } public static Connection getConnection() throws SQLException { Connection conn = DriverManager.getConnection(url,info); return conn; } public static void close(ResultSet rs,PreparedStatement pstmt,Connection conn ) throws SQLException { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }