java連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)單條插入和批量插入
本文實(shí)例為大家分享了java連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)單條和批量插入的具體代碼,供大家參考,具體內(nèi)容如下
本文插入數(shù)據(jù)庫(kù)的數(shù)據(jù)來(lái)源:java + dom4j.jar提取xml文檔內(nèi)容
1、連接數(shù)據(jù)庫(kù)
package com.njupt.ymh;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class Connect_MySQL {
private static final String URL="jdbc:mysql://127.0.0.1:3306/news"; // 一般默認(rèn)3306,這里設(shè)置成6666 (33060) MYSQL8 WMPNetworkSvc
private static final String USER="root";
private static final String PASSWORD="12345";
private static Connection connection=null;
static{
//1、加載驅(qū)動(dòng)程序(反射的方法)
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2、連接數(shù)據(jù)庫(kù)
try {
connection=(Connection) DriverManager.
getConnection(URL, USER,PASSWORD);//地址,用戶名,密碼
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return connection;
}
}
2、單條插入
package com.njupt.ymh;
/**
* 單條插入數(shù)據(jù)
*/
import java.sql.SQLException;
import java.util.List;
import com.mysql.jdbc.Connection;
public class OperationPaper {
private static Connection connection=Connect_MySQL.getConnection();
public void addNewsPaper(NewsPaper newsPaper){//增
// connection = Connect_MySQL.getConnection();
String sql="insert into papertest (id, date, title, lead_pargraph, full_text) values(?, ?, ?, ?, ?)";
java.sql.PreparedStatement ptmt = null;
try {
ptmt = connection.prepareStatement(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
ptmt.setLong(1, newsPaper.getID());
ptmt.setString(2, newsPaper.getDate());
ptmt.setString(3, newsPaper.getTitle());
ptmt.setString(4, newsPaper.getLead());
ptmt.setString(5, newsPaper.getfull());
ptmt.execute();//執(zhí)行給定的SQL語(yǔ)句,該語(yǔ)句可能返回多個(gè)結(jié)果
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
OperationPaper operationPaper = new OperationPaper();
List<String> listFile = SearchFile.getAllFile("E:\\huadai\\1996\\07\\21", false); // 文件列表
for (String string : listFile) {
NewsPaper newsPaper = new NewsPaper(string);
if (newsPaper.isUseful())
operationPaper.addNewsPaper(newsPaper); // 插入數(shù)據(jù)庫(kù)
}
}
}
3、批量插入
package com.njupt.ymh;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
public class OperaOnNewsPaper implements Cloneable{
private static Connection connection=Connect_MySQL.getConnection();
/**
* 支持批量插入數(shù)據(jù)
* @param newsPaper
*/
public void addNewsPaper(ArrayList<NewsPaper> listNewsPaper){//增
String sql="insert into papertest (id, date, title, lead_pargraph, full_text) values(?, ?, ?, ?, ?)";
java.sql.PreparedStatement ptmt = null;
try {
connection.setAutoCommit(false);// 關(guān)閉事務(wù)
ptmt = connection.prepareStatement(sql);
} catch (SQLException e2) {
e2.printStackTrace();
}
for (NewsPaper paperaper : listNewsPaper) {
try {
ptmt.setLong(1, paperaper.getID());
ptmt.setString(2, paperaper.getDate());
ptmt.setString(3, paperaper.getTitle());
ptmt.setString(4, paperaper.getLead());
ptmt.setString(5, paperaper.getfull());
ptmt.addBatch();
}
catch (SQLException e) {
e.printStackTrace();
}
}
try {
ptmt.executeBatch();//執(zhí)行給定的SQL語(yǔ)句,該語(yǔ)句可能返回多個(gè)結(jié)果
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
OperaOnNewsPaper operation = new OperaOnNewsPaper();
List<String> listFile = SearchFile.getAllFile("E:\\huadai\\2007", false); // 文件列表
ArrayList<NewsPaper> listPaper = new ArrayList<>();
int count = 0;
int sizenum = 1000;
for (String string : listFile) {
NewsPaper newsPaper = new NewsPaper(string);
if (newsPaper.isUseful()) {
count++;
listPaper.add(newsPaper); // 新聞列表
if (count % sizenum == 0) {
//System.out.println("ok");
System.out.println(" " + count);
operation.addNewsPaper(listPaper); //插入數(shù)據(jù)庫(kù)
System.out.println(count);
listPaper.clear();
}
}
}
if (count %sizenum != 0) {
operation.addNewsPaper(listPaper);
System.out.println("zui hou ");
}
}
}
通過(guò)實(shí)際測(cè)試,大概十萬(wàn)級(jí)數(shù)據(jù)批量插入要不單條插入節(jié)省10分鐘左右時(shí)間。因?yàn)槊看螁螚l插入就要和數(shù)據(jù)庫(kù)建立一次連接,進(jìn)行一次日志更新。但是,如果批量插入過(guò)程中,批量的數(shù)據(jù)值有一條不符合格式就將導(dǎo)致本次批量插入整體失敗,因此需要對(duì)失敗情況進(jìn)行處理,或者對(duì)批量插入的數(shù)據(jù)進(jìn)行預(yù)處理,保證批量插入能夠成功。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
idea創(chuàng)建springboot項(xiàng)目,Application.java不能運(yùn)行問(wèn)題及解決
這篇文章主要介紹了idea創(chuàng)建springboot項(xiàng)目,Application.java不能運(yùn)行問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring?Boot?Actuator使用說(shuō)明
SpringBootActuator?是一個(gè)用于監(jiān)控和管理?Spring?Boot?應(yīng)用程序的強(qiáng)大工具,通過(guò)引入依賴并配置,可以啟用默認(rèn)的監(jiān)控接口,本文介紹Spring?Boot?Actuator的使用,感興趣的朋友一起看看吧2025-02-02
Springboot編寫(xiě)CRUD時(shí)訪問(wèn)對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null的問(wèn)題及解決方法
我在學(xué)習(xí)springboot,其中在編寫(xiě)CRUD時(shí)發(fā)現(xiàn)訪問(wèn)數(shù)據(jù)的函數(shù)執(zhí)行下去返回值是null但是其它部分正常,這篇文章主要介紹了Springboot在編寫(xiě)CRUD時(shí),訪問(wèn)對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null,需要的朋友可以參考下2024-02-02
Kafka?日志存儲(chǔ)實(shí)現(xiàn)過(guò)程
這篇文章主要為大家介紹了Kafka?日志存儲(chǔ)的實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java項(xiàng)目如何引入日志生成器及其日志分級(jí)
這篇文章主要介紹了Java項(xiàng)目引入日志生成器及其日志分級(jí),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
springboot項(xiàng)目同時(shí)啟動(dòng)web服務(wù)和grpc服務(wù)的方法
本文主要介紹了springboot項(xiàng)目同時(shí)啟動(dòng)web服務(wù)和grpc服務(wù)的方法,通過(guò)實(shí)際代碼示例展示了實(shí)現(xiàn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
java:程序包org.bouncycastle.jce.provider不存在問(wèn)題及解決
這篇文章主要介紹了java:程序包org.bouncycastle.jce.provider不存在問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringBoot整合Mybatis-plus案例及用法實(shí)例
mybatis-plus是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Mybatis-plus案例及用法實(shí)例的相關(guān)資料,需要的朋友可以參考下2022-11-11
Aop動(dòng)態(tài)代理和cglib實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Aop動(dòng)態(tài)代理和cglib實(shí)現(xiàn)代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12

