如何使用JDBC實現(xiàn)工具類抽取
這篇文章主要介紹了如何使用JDBC實現(xiàn)工具類抽取,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
1、JDBC工具類抽取
上一篇做了JDBC的基本操作,但是獲取連接及釋放資源是比較重復(fù)的操作,可以抽取工具類而達(dá)到代碼重用的目的
工程結(jié)構(gòu)如圖
JDBC工具類代碼
db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://192.168.47.151:3306/web?useUnicode=true&characterEncoding=utf8 username=root password=root
JDBCUtils.java
package com.rookie.bigdata.util; import java.io.InputStream; import java.sql.*; import java.util.Properties; import java.util.ResourceBundle; /** * Created by dell on 2019/5/22. */ package com.rookie.bigdata.util; import java.io.InputStream; import java.sql.*; import java.util.Properties; import java.util.ResourceBundle; /** * Created by dell on 2019/5/22. */ public class JDBCUtils { private static String driver; private static String url; private static String username; private static String password; // //靜態(tài)代碼塊加載配置文件信息 // static { // ResourceBundle db = ResourceBundle.getBundle("db"); // driver = db.getString("driver"); // url = db.getString("url"); // username = db.getString("username"); // password = db.getString("password"); // } //靜態(tài)代碼塊加載配置文件信息 static { try { //獲取類加載器 ClassLoader classLoader = JDBCUtils.class.getClassLoader(); //通過類加載器的方法獲取一個輸入流 InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(resourceAsStream); //獲取相關(guān)參數(shù)的值 driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取連接 * * @return */ public static Connection getConnection() { Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 釋放資源 * @param conn * @param pstmt * @param rs */ public static void relase(Connection conn, PreparedStatement pstmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
2、批量插入數(shù)據(jù)
package com.rookie.bigdata; import com.rookie.bigdata.util.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; /** * CREATE TABLE `user` ( * `USERNAME` varchar(30) DEFAULT NULL COMMENT '用戶名', * `PASSWORD` varchar(10) DEFAULT NULL COMMENT '密碼' * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; */ public class JDBCBatch { public static void main(String[] args) throws Exception { Connection connection = JDBCUtils.getConnection(); //設(shè)置自動提交關(guān)閉 connection.setAutoCommit(false); PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USER VALUES (?,?)"); for (int i = 1; i <= 5000; i++) { preparedStatement.setString(1, "張三" + i); preparedStatement.setString(2, "123" + i); preparedStatement.addBatch(); if (i % 1000 == 0) { preparedStatement.executeUpdate(); connection.commit(); preparedStatement.clearBatch(); } } preparedStatement.executeUpdate(); connection.commit(); preparedStatement.clearBatch(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-puls中的resultMap數(shù)據(jù)映射
這篇文章主要介紹了mybatis-puls中的resultMap數(shù)據(jù)映射,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Springboot+redis+Interceptor+自定義annotation實現(xiàn)接口自動冪等
本篇文章給大家介紹了使用springboot和攔截器、redis來優(yōu)雅的實現(xiàn)接口冪等,對于冪等在實際的開發(fā)過程中是十分重要的,因為一個接口可能會被無數(shù)的客戶端調(diào)用,如何保證其不影響后臺的業(yè)務(wù)處理,如何保證其只影響數(shù)據(jù)一次是非常重要的,感興趣的朋友跟隨小編一起看看吧2019-07-07使用Springboot封裝一個自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類
我們在接收前臺傳輸?shù)臄?shù)據(jù)時,往往SpringBoot使用內(nèi)置的數(shù)據(jù)類型轉(zhuǎn)換器把我們提交的數(shù)據(jù)自動封裝成對象等類型,下面這篇文章主要給大家介紹了關(guān)于使用Springboot封裝一個自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類的相關(guān)資料,需要的朋友可以參考下2023-03-03springboot+swagger2.10.5+mybatis-plus 入門詳解
這篇文章主要介紹了springboot+swagger2.10.5+mybatis-plus 入門,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Spring Boot 從靜態(tài)json文件中讀取數(shù)據(jù)所需字段
本文重點給大家介紹Spring Boot 從靜態(tài)json文件中讀取數(shù)據(jù)所需字段,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05Java中StringBuilder字符串類型的操作方法及API整理
Java中的StringBuffer類繼承于AbstractStringBuilder,用來創(chuàng)建非線程安全的字符串類型對象,下面即是對Java中StringBuilder字符串類型的操作方法及API整理2016-05-05