最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java數(shù)據(jù)庫開發(fā)之JDBC的完整封裝兼容多種數(shù)據(jù)庫

 更新時間:2020年02月19日 16:17:32   作者:放蕩的小跳蛙  
這篇文章主要介紹了java數(shù)據(jù)庫開發(fā)之JDBC的完整封裝兼容多種數(shù)據(jù)庫,需要的朋友可以參考下

目前此代碼我只用過mysql和oracle數(shù)據(jù)庫測試過,但相信其它數(shù)據(jù)庫都是可以的,只要導(dǎo)入你需要操作的數(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類,然后對傳入對應(yīng)的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)鏈接

相關(guān)文章

  • iOS socket網(wǎng)絡(luò)編程實例詳解

    iOS socket網(wǎng)絡(luò)編程實例詳解

    socket是一個針對TCP和UDP編程的接口,你可以借助它建立TCP連接等。這篇文章主要介紹了iOS socket網(wǎng)絡(luò)編程 ,需要的朋友可以參考下
    2017-03-03
  • Spring解密之XML解析與Bean注冊示例詳解

    Spring解密之XML解析與Bean注冊示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring解密之XML解析與Bean注冊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Spring?Security用戶定義?

    Spring?Security用戶定義?

    這篇文章主要介紹了Spring?Security用戶定義,大家都知道?Spring?Security的用戶定義有很多方式,其實主要有兩種,基于內(nèi)存的和基于數(shù)據(jù)庫的,下面我給大家簡單介紹一下這兩種方式,需要的朋友可以參考下
    2022-02-02
  • Spring Boot中使用Spring-data-jpa實現(xiàn)數(shù)據(jù)庫增刪查改

    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ā)

    這篇文章主要介紹了詳解基于Spring Cloud幾行配置完成單點登錄開發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Springboot項目通過redis實現(xiàn)接口的冪等性

    Springboot項目通過redis實現(xiàn)接口的冪等性

    這篇文章主要為大家介紹了Springboot項目通過redis實現(xiàn)接口的冪等性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • java上界通配符(? extends Type)的使用

    java上界通配符(? extends Type)的使用

    在Java中,? extends Type是一個上界通配符,本文主要介紹了java上界通配符(? extends Type)的使用,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • 利用Jackson實現(xiàn)數(shù)據(jù)脫敏的示例詳解

    利用Jackson實現(xiàn)數(shù)據(jù)脫敏的示例詳解

    在我們的企業(yè)項目中,為了保護(hù)用戶隱私,數(shù)據(jù)脫敏成了必不可少的操作,那么我們怎么優(yōu)雅的利用Jackson實現(xiàn)數(shù)據(jù)脫敏呢,本文就來和大家詳細(xì)聊聊,希望對大家有所幫助
    2023-05-05
  • SpringBoot中日志切面實現(xiàn)小結(jié)

    SpringBoot中日志切面實現(xiàn)小結(jié)

    本文介紹了SpringBoot中日志切面實現(xiàn)小結(jié),通過定義一個自定義注解和創(chuàng)建一個日志切面類,為方法添加日志記錄功能,感興趣的可以了解一下
    2024-11-11
  • Spring和SpringBoot比較及區(qū)別解惑

    Spring和SpringBoot比較及區(qū)別解惑

    這篇文章主要介紹了Spring和SpringBoot比較解惑區(qū)別,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06

最新評論

尼木县| 苏州市| 泸水县| 同德县| 牟定县| 莫力| 阿克| 内丘县| 长乐市| 丹凤县| 青川县| 攀枝花市| 许昌县| 秦皇岛市| 大新县| 连城县| 安宁市| 扎鲁特旗| 昂仁县| 涞源县| 砀山县| 祁东县| 张掖市| 分宜县| 博白县| 恭城| 咸宁市| 工布江达县| 兰溪市| 阳春市| 罗城| 鹿泉市| 文水县| 高平市| 沁水县| 巨鹿县| 鞍山市| 徐汇区| 四平市| 河池市| 田林县|