JDBCパッケージDBUtil
1457 ワード
import java.sql.*;
public class DBUtil {
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","root");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn,String sql){
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return ps;
}
public static void closeConnection(Connection conn){
if(null != conn){
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closePreparedStatement(PreparedStatement ps){
if(null != ps){
try {
ps.close();
ps = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeResultSet(ResultSet rs){
if(null != rs){
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}