package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtils {
public static Connection getConnection() {
//1.注册驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.获取连接
String url = "jdbc:mysql://localhost:3306/webtest?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
String user="root";
String password = "000000";
Connection conn = null;
try {
conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection con,Statement st,ResultSet rs) {
try{
con.close();
st.close();
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void close(Connection con,PreparedStatement ps) {
try{
con.close();
ps.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
|