public class JDBCUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
static{
try {
Properties properties=new Properties();
FileInputStream fileInputStream=new FileInputStream("src/db.properties");
properties.load(fileInputStream);
fileInputStream.close();
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
System.out.print(driver+url+username+password);
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception {
Class.forName(driver);
Connection connection=DriverManager.getConnection(url,username, password);
return connection;
}
public static void close(Connection connection,Statement statement,ResultSet resultSet) {
//保证任何一个出错其他都能关闭
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
}
JDBCUtils 封装
最新推荐文章于 2023-01-18 16:03:40 发布