You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.5 KiB
59 lines
1.5 KiB
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|