import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class Dbhelper { // 此方法爲獲取數據庫鏈接 public static Connection getConnection() { Connection conn = null; try { String driver = "com.mysql.jdbc.Driver"; // 數據庫驅動 String url = "jdbc:MySQL://127.0.0.1:3306/student";// 數據庫 String user = "root"; // 用戶名 String password = ""; // 密碼 Class.forName(driver); // 加載數據庫驅動 if (null == conn) { conn = (Connection) DriverManager.getConnection(url, user, password); } } catch (ClassNotFoundException e) { System.out.println("加載驅動失敗!"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 增刪改�?Add�?Del�?Update】 * * @param sql * @return int */ public static int executeNonQuery(String sql) { int result = 0; Connection conn = null; Statement stmt = null; try { conn = getConnection(); stmt = (Statement) conn.createStatement(); result = stmt.executeUpdate(sql); } catch (SQLException err) { err.printStackTrace(); free(null, stmt, conn); } finally { free(null, stmt, conn); } return result; } /** * 增刪改�?Add�?Delete�?Update】 * * @param sql * @param obj * @return int */ public static int executeNonQuery(String sql, Object... obj) { int result = 0; Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); pstmt = (PreparedStatement) conn.prepareStatement(sql); for (int i = 0; i < obj.length; i++) { pstmt.setObject(i + 1, obj[i]); } result = pstmt.executeUpdate(); } catch (SQLException err) { err.printStackTrace(); free(null, pstmt, conn); } finally { free(null, pstmt, conn); } return result; } /** * 查�?Query】 * * @param sql * @return ResultSet */ public static ResultSet executeQuery(String sql) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = (Statement) conn.createStatement(); rs = stmt.executeQuery(sql); } catch (SQLException err) { err.printStackTrace(); free(rs, stmt, conn); } return rs; } /** * 查�?Query】 * * @param sql * @param obj * @return ResultSet */ public static ResultSet executeQuery(String sql, Object... obj) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = getConnection(); pstmt = (PreparedStatement) conn.prepareStatement(sql); for (int i = 0; i < obj.length; i++) { pstmt.setObject(i + 1, obj[i]); } rs = pstmt.executeQuery(); } catch (SQLException err) { err.printStackTrace(); free(rs, pstmt, conn); } return rs; } /** * 判斷記錄是否存在 * * @param sql * @return Boolean */ public static Boolean isExist(String sql) { ResultSet rs = null; try { rs = executeQuery(sql); rs.last(); int count = rs.getRow(); if (count > 0) { return true; } else { return false; } } catch (SQLException err) { err.printStackTrace(); free(rs); return false; } finally { free(rs); } } /** 判斷記錄是�?�存在 * @param sql * @return Boolean */ public static Boolean isExist(String sql, Object... obj) { ResultSet rs = null; try { rs = executeQuery(sql, obj); rs.last(); int count = rs.getRow(); if (count > 0) { return true; } else { return false; } } catch (SQLException err) { err.printStackTrace(); free(rs); return false; } finally { free(rs); } } /** * 獲�?�查詢記錄的總行數 * @param sql * @return int */ public static int getCount(String sql) { int result = 0; ResultSet rs = null; try { rs = executeQuery(sql); rs.last(); result = rs.getRow(); } catch (SQLException err) { free(rs); err.printStackTrace(); } finally { free(rs); } return result; } /** * 獲�?�查詢記錄的總行數 * @param sql * @param obj * @return int */ public static int getCount(String sql, Object... obj) { int result = 0; ResultSet rs = null; try { rs = executeQuery(sql, obj); rs.last(); result = rs.getRow(); } catch (SQLException err) { err.printStackTrace(); } finally { free(rs); } return result; } /** * 釋放�?ResultSet】資�? * @param rs */ public static void free(ResultSet rs) { try { if (rs != null) { rs.close(); } } catch (SQLException err) { err.printStackTrace(); } } /** * 釋放�?Statement】資�? * @param st */ public static void free(Statement st) { try { if (st != null) { st.close(); } } catch (SQLException err) { err.printStackTrace(); } } /** * 釋放�?Connection】資�? * @param conn */ public static void free(Connection conn) { try { if (conn != null) { conn.close(); } } catch (SQLException err) { err.printStackTrace(); } } /** * 釋放全部數�?�資�? * @param rs * @param st * @param conn */ public static void free(ResultSet rs, Statement st, Connection conn) { free(rs); free(st); free(conn); } }