jdbcutil封装成一个工具类
作者:互联网
package review.jdbc;
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 JDBCUtils {
public static Connection getConnection() {
Connection conn=null;
InputStream is=JDBCUtils.class.getClassLoader().getResourceAsStream(“jdbc.properties”);
Properties pros=new Properties();
try {
pros.load(is);
String driver=pros.getProperty(“driver”);
String url=pros.getProperty(“url”);
String user=pros.getProperty(“user”);
String password=pros.getProperty(“password”);
Class.forName(driver);
conn=DriverManager.getConnection(
url,
user,
password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeRes(Connection conn,PreparedStatement ps, ResultSet rs) {
try {
if(conn!=null) {
conn.close();
}if(ps!=null) {
ps.close();
}if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
标签:java,sql,pros,printStackTrace,import,封装,工具,jdbcutil,conn 来源: https://blog.csdn.net/zwmonk/article/details/90743754