Compare commits
No commits in common. 'daoimpl' and 'src' have entirely different histories.
@ -1,84 +0,0 @@
|
|||||||
package flowershop.daoimpl;
|
|
||||||
|
|
||||||
|
|
||||||
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 Jdbc {
|
|
||||||
// 数据连接URL
|
|
||||||
static String url;
|
|
||||||
// 创建Properties对象
|
|
||||||
static Properties info = new Properties();
|
|
||||||
|
|
||||||
// 静态代码块
|
|
||||||
static {
|
|
||||||
// 获取属性文件的输入流
|
|
||||||
InputStream input = Jdbc.class.getClassLoader().getResourceAsStream("config.properties");
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (input == null) {
|
|
||||||
throw new NullPointerException("配置文件未找到");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 加载属性文件
|
|
||||||
info.load(input);
|
|
||||||
|
|
||||||
// 从属性文件中读取url
|
|
||||||
url = info.getProperty("url");
|
|
||||||
|
|
||||||
// 从属性文件中读取driver
|
|
||||||
String driverClassName = info.getProperty("driver");
|
|
||||||
|
|
||||||
// 加载驱动程序
|
|
||||||
Class.forName(driverClassName);
|
|
||||||
System.out.println("驱动程序加载成功...");
|
|
||||||
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
System.out.println("驱动程序加载失败...");
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("属性文件加载失败...");
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (NullPointerException e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建数据连接
|
|
||||||
public static Connection getConnection() throws SQLException {
|
|
||||||
// 创建数据连接
|
|
||||||
Connection conn = DriverManager.getConnection(url, info);
|
|
||||||
return conn;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭连接方法
|
|
||||||
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in new issue