工具类:封装jdbc的驱动加载和连接和关闭
成员变量
private static String classname = "com.mysql.jdbc.Driver";
private static String url ="jdbc:mysql://localhost:3306/t_store?characterEncoding=utf8";
private static String user = "root";
private static String password = "aoxixi";
加载驱动
static{
try {
Class.forName(classname);
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
创建连接
public static Connection getConn() throws SQLException{
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}
关闭连接
public static void release(Connection conn){
if(conn !=null){
try{
conn.close();
}catch (SQLException e)
{
e.printStackTrace();
}
}
}
测试
public static void main(String[] args) throws SQLException {
Connection conn = JDBCUtil.getConn();
System.out.println(conn);
}