java數(shù)據(jù)庫開發(fā)之JDBC的完整封裝兼容多種數(shù)據(jù)庫
目前此代碼我只用過mysql和oracle數(shù)據(jù)庫測試過,但相信其它數(shù)據(jù)庫都是可以的,只要導入你需要操作的數(shù)據(jù)庫jar包,驅(qū)動等就可,下面上代碼:
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;
/**
* 對jdbc的完整封裝
*
*/
public class JDBCUtil {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
private CallableStatement callableStatement = null;//創(chuàng)建CallableStatement對象
private Connection conn = null;
private PreparedStatement pst = null;
private ResultSet rst = null;
/* static {
try {
// 加載數(shù)據(jù)庫驅(qū)動程序
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("加載驅(qū)動錯誤");
System.out.println(e.getMessage());
}
} */
public JDBCUtil(String driver,String url ,String username,String password) {
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
}
/**
* 建立數(shù)據(jù)庫連接
* @return 數(shù)據(jù)庫連接
*/
public Connection getConnection() {
try {
// 加載數(shù)據(jù)庫驅(qū)動程序
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("加載驅(qū)動錯誤");
System.out.println(e.getMessage());
e.printStackTrace();
}
// 獲取連接
conn = DriverManager.getConnection(url, username,
password);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
/**
* insert update delete SQL語句的執(zhí)行的統(tǒng)一方法
* @param sql SQL語句
* @param params 參數(shù)數(shù)組,若沒有參數(shù)則為null
* @return 受影響的行數(shù)
*/
public int executeUpdate(String sql, Object[] params) {
// 受影響的行數(shù)
int affectedLine = 0;
try {
// 獲得連接
conn = this.getConnection();
// 調(diào)用SQL
pst = conn.prepareStatement(sql);
// 參數(shù)賦值
if (params != null) {
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
}
/*在此 PreparedStatement 對象中執(zhí)行 SQL 語句,
該語句必須是一個 SQL 數(shù)據(jù)操作語言(Data Manipulation Language,DML)語句,比如 INSERT、UPDATE 或 DELETE
語句;或者是無返回內(nèi)容的 SQL 語句,比如 DDL 語句。 */
// 執(zhí)行
affectedLine = pst.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
// 釋放資源
closeAll();
}
return affectedLine;
}
/**
* SQL 查詢將查詢結(jié)果直接放入ResultSet中
* @param sql SQL語句
* @param params 參數(shù)數(shù)組,若沒有參數(shù)則為null
* @return 結(jié)果集
*/
private ResultSet executeQueryRS(String sql, Object[] params) {
try {
// 獲得連接
conn = this.getConnection();
// 調(diào)用SQL
pst = conn.prepareStatement(sql);
// 參數(shù)賦值
if (params != null) {
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
}
// 執(zhí)行
rst = pst.executeQuery();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return rst;
}
/**
* SQL 查詢將查詢結(jié)果:一行一列
* @param sql SQL語句
* @param params 參數(shù)數(shù)組,若沒有參數(shù)則為null
* @return 結(jié)果集
*/
public Object executeQuerySingle(String sql, Object[] params) {
Object object = null;
try {
// 獲得連接
conn = this.getConnection();
// 調(diào)用SQL
pst = conn.prepareStatement(sql);
// 參數(shù)賦值
if (params != null) {
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
}
// 執(zhí)行
rst = pst.executeQuery();
if(rst.next()) {
object = rst.getObject(1);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
closeAll();
}
return object;
}
/**
* 獲取結(jié)果集,并將結(jié)果放在List中
*
* @param sql SQL語句
* params 參數(shù),沒有則為null
* @return List
* 結(jié)果集
*/
public List<Object> excuteQuery(String sql, Object[] params) {
// 執(zhí)行SQL獲得結(jié)果集
ResultSet rs = executeQueryRS(sql, params);
// 創(chuàng)建ResultSetMetaData對象
ResultSetMetaData rsmd = null;
// 結(jié)果集列數(shù)
int columnCount = 0;
try {
rsmd = rs.getMetaData();
// 獲得結(jié)果集列數(shù)
columnCount = rsmd.getColumnCount();
} catch (SQLException e1) {
System.out.println(e1.getMessage());
}
// 創(chuàng)建List
List<Object> list = new ArrayList<Object>();
try {
// 將ResultSet的結(jié)果保存到List中
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
map.put(rsmd.getColumnLabel(i), rs.getObject(i));
}
list.add(map);//每一個map代表一條記錄,把所有記錄存在list中
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
// 關(guān)閉所有資源
closeAll();
}
return list;
}
/**
* 存儲過程帶有一個輸出參數(shù)的方法
* @param sql 存儲過程語句
* @param params 參數(shù)數(shù)組
* @param outParamPos 輸出參數(shù)位置
* @param SqlType 輸出參數(shù)類型
* @return 輸出參數(shù)的值
*/
public Object excuteQuery(String sql, Object[] params,int outParamPos, int SqlType) {
Object object = null;
conn = this.getConnection();
try {
// 調(diào)用存儲過程
// prepareCall:創(chuàng)建一個 CallableStatement 對象來調(diào)用數(shù)據(jù)庫存儲過程。
callableStatement = conn.prepareCall(sql);
// 給參數(shù)賦值
if(params != null) {
for(int i = 0; i < params.length; i++) {
callableStatement.setObject(i + 1, params[i]);
}
}
// 注冊輸出參數(shù)
callableStatement.registerOutParameter(outParamPos, SqlType);
// 執(zhí)行
callableStatement.execute();
// 得到輸出參數(shù)
object = callableStatement.getObject(outParamPos);
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
// 釋放資源
closeAll();
}
return object;
}
/**
* 關(guān)閉所有資源
*/
private void closeAll() {
// 關(guān)閉結(jié)果集對象
if (rst != null) {
try {
rst.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
// 關(guān)閉PreparedStatement對象
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
// 關(guān)閉CallableStatement 對象
if (callableStatement != null) {
try {
callableStatement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
// 關(guān)閉Connection 對象
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
}
使用的時候直接new一個JDBCUtil類,然后對傳入對應的sql語句,例:
public class JDBCTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
JDBCUtil jdbcUtil = new JDBCUtil("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/myhelp?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","142014068");
String sql = "delete from menu_detial where count=6";
System.out.println(jdbcUtil.executeUpdate(sql, null));
}
}
更多關(guān)于java數(shù)據(jù)庫開發(fā)的文章請查看下面的相關(guān)鏈接
- Java基礎(chǔ)之JDBC的數(shù)據(jù)庫連接與基本操作
- java使用JDBC連接數(shù)據(jù)庫的五種方式(IDEA版)
- Java連接 JDBC基礎(chǔ)知識(操作數(shù)據(jù)庫:增刪改查)
- Java 數(shù)據(jù)庫連接(JDBC)的相關(guān)總結(jié)
- Java 如何使用JDBC連接數(shù)據(jù)庫
- 詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(操作數(shù)據(jù)庫:增刪改查)
- Java如果通過jdbc操作連接oracle數(shù)據(jù)庫
- Java連接數(shù)據(jù)庫JDBC技術(shù)之prepareStatement的詳細介紹
- Java之jdbc連接mysql數(shù)據(jù)庫的方法步驟詳解
- Java基礎(chǔ)開發(fā)之JDBC操作數(shù)據(jù)庫增刪改查,分頁查詢實例詳解
- java數(shù)據(jù)庫開發(fā)之JDBC基礎(chǔ)使用方法及實例詳解
- Java JDBC連接數(shù)據(jù)庫常見操作總結(jié)
- Java使用JDBC連接postgresql數(shù)據(jù)庫示例
- Java實現(xiàn)JDBC連接數(shù)據(jù)庫簡單案例
- java使用jdbc連接數(shù)據(jù)庫簡單實例
- Java使用jdbc連接MySQL數(shù)據(jù)庫實例分析
- Java基于JDBC連接數(shù)據(jù)庫及顯示數(shù)據(jù)操作示例
- 詳細說明關(guān)于Java的數(shù)據(jù)庫連接(JDBC)
相關(guān)文章
Spring Boot中使用Spring-data-jpa實現(xiàn)數(shù)據(jù)庫增刪查改
本篇文章主要介紹了Spring Boot中使用Spring-data-jpa實現(xiàn)增刪查改,非常具有實用價值,需要的朋友可以參考下。2017-03-03
詳解基于Spring Cloud幾行配置完成單點登錄開發(fā)
這篇文章主要介紹了詳解基于Spring Cloud幾行配置完成單點登錄開發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Springboot項目通過redis實現(xiàn)接口的冪等性
這篇文章主要為大家介紹了Springboot項目通過redis實現(xiàn)接口的冪等性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
利用Jackson實現(xiàn)數(shù)據(jù)脫敏的示例詳解
在我們的企業(yè)項目中,為了保護用戶隱私,數(shù)據(jù)脫敏成了必不可少的操作,那么我們怎么優(yōu)雅的利用Jackson實現(xiàn)數(shù)據(jù)脫敏呢,本文就來和大家詳細聊聊,希望對大家有所幫助2023-05-05
SpringBoot中日志切面實現(xiàn)小結(jié)
本文介紹了SpringBoot中日志切面實現(xiàn)小結(jié),通過定義一個自定義注解和創(chuàng)建一個日志切面類,為方法添加日志記錄功能,感興趣的可以了解一下2024-11-11

