java(JDBC連線資料庫)[完整版封裝]
阿新 • • 發佈:2018-12-31
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.util.ArrayList;
-
import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * 資料庫連線類
- * 說明:封裝了 無參,有參,儲存過程的呼叫
- * @author iflytek
- *
- */
- publicclass ConnectionDB {
- /**
- * 資料庫驅動類名稱
- */
- privatestaticfinal String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- /**
-
* 連線字串
- */
- privatestaticfinal String URLSTR = "jdbc:sqlserver://localhost:1433; databaseName=Northwind";
- /**
- * 使用者名稱
- */
- privatestaticfinal String USERNAME = "sa";
- /**
- * 密碼
- */
- privatestaticfinal String USERPASSWORD = "111111";
- /**
-
* 建立資料庫連線物件
- */
- private Connection connnection = null;
- /**
- * 建立PreparedStatement物件
- */
- private PreparedStatement preparedStatement = null;
- /**
- * 建立CallableStatement物件
- */
- private CallableStatement callableStatement = null;
- /**
- * 建立結果集物件
- */
- private ResultSet resultSet = null;
- static {
- try {
- // 載入資料庫驅動程式
- Class.forName(DRIVER);
- } catch (ClassNotFoundException e) {
- System.out.println("載入驅動錯誤");
- System.out.println(e.getMessage());
- }
- }
- /**
- * 建立資料庫連線
- * @return 資料庫連線
- */
- public Connection getConnection() {
- try {
- // 獲取連線
- connnection = DriverManager.getConnection(URLSTR, USERNAME,
- USERPASSWORD);
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return connnection;
- }
- /**
- * insert update delete SQL語句的執行的統一方法
- * @param sql SQL語句
- * @param params 引數陣列,若沒有引數則為null
- * @return 受影響的行數
- */
- publicint executeUpdate(String sql, Object[] params) {
- // 受影響的行數
- int affectedLine = 0;
- try {
- // 獲得連線
- connnection = this.getConnection();
- // 呼叫SQL
- preparedStatement = connnection.prepareStatement(sql);
- // 引數賦值
- if (params != null) {
- for (int i = 0; i < params.length; i++) {
- preparedStatement.setObject(i + 1, params[i]);
- }
- }
- // 執行
- affectedLine = preparedStatement.executeUpdate();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- // 釋放資源
- closeAll();
- }
- return affectedLine;
- }
- /**
- * SQL 查詢將查詢結果直接放入ResultSet中
- * @param sql SQL語句
- * @param params 引數陣列,若沒有引數則為null
- * @return 結果集
- */
- private ResultSet executeQueryRS(String sql, Object[] params) {
- try {
- // 獲得連線
- connnection = this.getConnection();
- // 呼叫SQL
- preparedStatement = connnection.prepareStatement(sql);
- // 引數賦值
- if (params != null) {
- for (int i = 0; i < params.length; i++) {
- preparedStatement.setObject(i + 1, params[i]);
- }
- }
- // 執行
- resultSet = preparedStatement.executeQuery();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return resultSet;
- }
- /**
- * 獲取結果集,並將結果放在List中
- *
- * @param sql
- * SQL語句
- * @return List
- * 結果集
- */
- public List<Object> excuteQuery(String sql, Object[] params) {
- // 執行SQL獲得結果集
- ResultSet rs = executeQueryRS(sql, params);
- // 建立ResultSetMetaData物件
- ResultSetMetaData rsmd = null;
- // 結果集列數