废话不多说,直接上代码
package test.com.xxbb.db;
import java.sql.*;
public class DBUtil{
private static final String DRIVER_NAME="com.mysql.jdbc.Driver";
private static final String CONNECTION_STRING="jdbc:mysql://localhost:3306/student";
private static final String NAME="root";
private static final String PASSWORD="root";
public static Connection getConnection(){
try{
Class.forName(DRIVER_NAME);
Connection conn= DriverManager.getConnection(CONNECTION_STRING,NAME,PASSWORD);
return conn;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static void close(PreparedStatement pr, Statement st,
Connection conn, ResultSet res) {
try {
if (pr != null) {
pr.close();
}
if (conn != null && !conn.isClosed()) {
conn.close();
}
if (res != null) {
res.close();
}
if (st != null) {
st.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}