连接数据库语句(工具类)
package com.wkcto.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private DBUtil(){}
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static final String url = "jdbc:mysql://localhost:3306/test";
private static final String user = "root";
private static final String password = "root";
//创建连接 返回连接
public static Connection getConn() throws SQLException{
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
//关闭相关资源
public static void myClose(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException{
//关闭资源的顺序为 按照创建的顺序 逆序关闭
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}
}