package util;
import java.sql.CallableStatement;
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 DBUtil {
public static Connection getCon() throws Exception {
String jdbcName = "com.mysql.jdbc.Driver";
Class.forName(jdbcName);
String dbUrl = "jdbc:mysql://ip:3306/databaseName?useUnicode=true&characterEncoding=UTF-8";
String dbUser = "username";
String dbPassword = "password";
Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return conn;
}
public static void close(Statement stmt, Connection conn) throws Exception {
if (stmt != null) {
stmt.close();
if (conn != null) {
conn.close();
}
}
}
public static void close(CallableStatement cstmt, Connection conn) throws Exception {
if (cstmt != null) {
cstmt.close();
if (conn != null) {
conn.close();
}
}
}
public static void close(PreparedStatement pstmt, Connection conn) throws SQLException {
if (pstmt != null) {
pstmt.close();
if (conn != null) {
conn.close();
}
}
}
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if (rs != null) {
try {
rs.close();
if (pstmt != null) {
pstmt.close();
if (conn != null) {
conn.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}