java插⼊数据库字符串拼接_java连接mysql数据库实现单条插
⼊和批量插⼊
本⽂实例为⼤家分享了java连接mysql数据库实现单条和批量插⼊的具体代码,供⼤家参考,具体内容如下
本⽂插⼊数据库的数据来源:java + dom4j.jar提取xml⽂档内容
1、连接数据库
package h;
import java.sql.DriverManager;
import java.sql.SQLException;
sql.jdbc.Connection;
public class Connect_MySQL {
private static final String URL="jdbc:mysql://127.0.0.1:3306/news"; // ⼀般默认3306,这⾥设置成6666 (33060) MYSQL8 WMPNetworkSvc
private static final String USER="root";
private static final String PASSWORD="12345";
private static Connection connection=null;
static{
//1、加载驱动程序(反射的⽅法)
try {
Class.forName("sql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2、连接数据库
try {
connection=(Connection) DriverManager.
getConnection(URL, USER,PASSWORD);//地址,⽤户名,密码
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return connection;
}
}
2、单条插⼊
package h;
/**
* 单条插⼊数据
*/
import java.sql.SQLException;
import java.util.List;
sql.jdbc.Connection;
public class OperationPaper {
private static Connection connection=Connection();
public void addNewsPaper(NewsPaper newsPaper){//增
// connection = Connection();
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, ID());
ptmt.setString(2, Date());
ptmt.setString(3, Title());
ptmt.setString(4, Lead());
ptmt.setString(5, full());
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
OperationPaper operationPaper = new OperationPaper();
List listFile = AllFile("E:\\huadai\\1996\\07\\21", false); // ⽂件列表
for (String string : listFile) {
NewsPaper newsPaper = new NewsPaper(string);
if (newsPaper.isUseful())
批量更新sql语句operationPaper.addNewsPaper(newsPaper); // 插⼊数据库
}
}
}
3、批量插⼊
package h;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
sql.jdbc.Connection;
public class OperaOnNewsPaper implements Cloneable{
private static Connection connection=Connection();
/**
* ⽀持批量插⼊数据
* @param newsPaper
*/
public void addNewsPaper(ArrayList listNewsPaper){//增
String sql="insert into papertest (id, date, title, lead_pargraph, full_text) values(?, ?, ?, ?, ?)"; java.sql.PreparedStatement ptmt = null;
try {
connection.setAutoCommit(false);// 关闭事务
ptmt = connection.prepareStatement(sql);
} catch (SQLException e2) {
e2.printStackTrace();
}
for (NewsPaper paperaper : listNewsPaper) {
try {
ptmt.setLong(1, ID());
ptmt.setString(2, Date());
ptmt.setString(3, Title());
ptmt.setString(4, Lead());
ptmt.setString(5, full());
ptmt.addBatch();
}
catch (SQLException e) {
e.printStackTrace();
}
}
try {
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
OperaOnNewsPaper operation = new OperaOnNewsPaper();
List listFile = AllFile("E:\\huadai\\2007", false); // ⽂件列表ArrayList 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); //插⼊数据库
System.out.println(count);
listPaper.clear();
}
}
}
if (count %sizenum != 0) {
operation.addNewsPaper(listPaper);
System.out.println("zui hou ");
}
}
}
通过实际测试,⼤概⼗万级数据批量插⼊要不单条插⼊节省10分钟左右时间。因为每次单条插⼊就要和数据库建⽴⼀次连接,进⾏⼀次⽇志更新。但是,如果批量插⼊过程中,批量的数据值有⼀条不符合格式就将导致本次批量插⼊整体失败,因此需要对失败情况进⾏处理,或者对批量插⼊的数据进⾏预处理,保证批量插⼊能够成功。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持聚⽶学院。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。