标签:name void 工具类 url except catch com public getc
public class JDBCUtils {
//获取connection对象
public static Connection getConn(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/demo01?characterEncoding=utf8";
String user="demo01";
String password="123456";
conn=DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
//增删改释放资源
public static void close(Connection conn,Statement pst){
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询释放资源
public static void close(Connection conn,Statement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
标签:name void 工具类 url except catch com public getc
原文地址:https://www.cnblogs.com/nbkls/p/12654844.html