亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

myJava applicaition

發布時間:2020-08-18 08:56:07 來源:ITPUB博客 閱讀:171 作者:liyihongcug 欄目:編程語言

fdisk -l

mount -t vfat -o iocharset=cp950 /dev/sda1 /mnt/dos

實現Java批量插入數據庫數據,在javaeye中看到過幾篇關于實現Java批量插入數據庫數據,轉載時沒有找到,就自己寫一下,也算是對自己學習過程中所遇到過的問題做一個總結。

一般關于批量向數據庫插入數據都采用PreparedStatement、Statement…………也包括直接使用JDBC API、框架…………

也看到過幾篇關于這些內容的總結,及大家的評論,以下為我總結的關于批量向數據庫插入數據。

1,使用JDBC API實現配量插入數據:有篇文章介紹過關于JDBC API、Hibernate實現批量插入數據,采用JDBC API 方式實現隨著數據的增長,速度更勝于Hibernate。當然,對于這個測試的準確我并不保證,但是我也會優先選用JDBC API方式實現(原因:簡單、易學、相對于框架更通用,不會過時)。

2,采用PreparedStatement對象實現批量插入數據:PreparedStatement是真正的批處理命令,不是其他的偽批處理命令可以相比的(個人意見),它相對于其他的實現批量處理是非常的強大,比如字段不斷改變,每次都要從文件從新讀取就只能使用PreparedStatement對象來實現。再有就是存在即合理,既然PreparedStatement對象可以多次高效地執行預編譯的語句,就一定有其原因(JDk源碼沒有分析過,和Statement實現的區別不了解)。

3,實現批量插入數據庫數據

Java代碼 myJava applicaition
  1. Class.forName("com.mysql.jdbc.Driver");
  2. Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://" +
  3. "localhost:3306/excel2mysql", "wanle", "wanle");
  4. // 關閉事務自動提交
  5. con.setAutoCommit(false);
  6. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
  7. TimeZone t = sdf.getTimeZone();
  8. t.setRawOffset(0);
  9. sdf.setTimeZone(t);
  10. Long startTime = System.currentTimeMillis();
  11. PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into test04 values (?,'中國')");
  12. for (int i = 0; i < 10000; i++) {
  13. pst.setInt(1, i);
  14. // 把一個SQL命令加入命令列表
  15. pst.addBatch();
  16. }
  17. // 執行批量更新
  18. pst.executeBatch();
  19. // 語句執行完畢,提交本事務
  20. con.commit();
  21. Long endTime = System.currentTimeMillis();
  22. System.out.println("用時:" + sdf.format(new Date(endTime - startTime)));
  23. pst.close();
  24. con.close();
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://" +
                    "localhost:3306/excel2mysql", "wanle", "wanle");
            // 關閉事務自動提交
            con.setAutoCommit(false);

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
            TimeZone t = sdf.getTimeZone();
            t.setRawOffset(0);
            sdf.setTimeZone(t);
            Long startTime = System.currentTimeMillis();

            PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into test04 values (?,'中國')");
            for (int i = 0; i < 10000; i++) {
                pst.setInt(1, i);
                // 把一個SQL命令加入命令列表
                pst.addBatch();
            }
            // 執行批量更新
            pst.executeBatch();
            // 語句執行完畢,提交本事務
            con.commit();

            Long endTime = System.currentTimeMillis();
            System.out.println("用時:" + sdf.format(new Date(endTime - startTime)));

            pst.close();
            con.close();

插入10000條數據用時3141毫秒,對于我已經很理想了, 畢竟我們不會使用MySQL進行非常大型項目的開發,對于10000條數據3秒多點,已經可以了,我相信對于大家應該也足以應付了,我們不會每天都插入10000條吧,當然對于我的話如果有這樣的需求,我不會選擇MySQL。

以上所有內容均為對于我所學習使用過程中、實際項目開發中的總結,也應用于其中。對于批量插入,數據導入均采用這樣的方式。


System.getProperties().list(System.out);
Connection conn=initConnect2();
Statement statement = conn.createStatement();
//String sql = "insert into newsenginedb.ResolveTranslation select `id`,`typeId`,`disciplineId`,`collectedName`,`oddsModelObjectId`,`isApproved`,`lastUsedDate`,`note`,0 from oddsenginedb.ResolveTranslation where id =10017444";
//statement.executeUpdate(sql);
//String sql = "select `id`,`typeId`,`disciplineId`,`collectedName`,`oddsModelObjectId`,`isApproved`,`lastUsedDate`,`note`,0 from oddsenginedb.ResolveTranslation where id =10133053 or id =10159387"; //10017444
String sql = "select `id`,`typeId`,`disciplineId`,`collectedName`,`oddsModelObjectId`,`isApproved`,`lastUsedDate`,`note`,0 from oddsenginedb.ResolveTranslation limit 290";
ResultSet rs= statement.executeQuery(sql);
String aa="sdfsd'f";
aa=aa.replaceAll("'", "'");
System.out.println(aa);

while(rs.next()){
String id= rs.getString("id");
String typeId= rs.getString("typeId");
String disciplineId= rs.getString("disciplineId");
String collectedName= rs.getString("collectedName");
String oddsModelObjectId= rs.getString("oddsModelObjectId");
collectedName=collectedName.replaceAll("'", "'");
collectedName=new String(collectedName.getBytes("utf-8"),"latin1");
String isApproved= rs.getString("isApproved");
String lastUsedDate= rs.getString("lastUsedDate");
String note= rs.getString("note");
System.out.println(collectedName);
char[] test=new char[collectedName.length()];
collectedName.getChars(0, collectedName.length(), test, 0);
for(int i=0;iSystem.out.println((byte)test[i]);
}
System.out.println(collectedName);
//Connection conn2=initConnect3();
Statement statement2 = conn.createStatement();
String sql2 = "insert into newsenginedb.aa values("+id+","+typeId+","+disciplineId+",'"+collectedName+"',"+oddsModelObjectId+","+isApproved+",'"+lastUsedDate+"','"+note+"',0)";
System.out.println(sql2);
statement2.executeUpdate(sql2);

}

//package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.*;

//import Record;


public class Test {
private static List sqlList = new ArrayList();
static{
sqlList.add("select b.familyName,b.givenName from UserEntityIP b where trim(ifnull(b.familyName,'')) <>'' and trim(ifnull(b.givenName,'')) <>'' group by b.familyName,b.givenName having count(*)>=2");
sqlList.add("select b.email from UserEntityIP b where trim(ifnull(b.email,'')) <>''group by b.email having count(*)>=2");
sqlList.add("select b.phone from UserEntityIP b where trim(ifnull(b.phone,'')) <>''group by b.phone having count(*)>=2");
sqlList.add("select b.addressLine1 from UserEntityIP b where trim(ifnull(b.addressLine1,'')) <>''group by b.addressLine1 having count(*)>=2");
sqlList.add("select b.birthday from UserEntityIP b where trim(ifnull(b.birthday,'')) <>''group by b.birthday having count(*)>=2");
sqlList.add("select b.city from UserEntityIP b where b.city regexp '[A-Za-z]'group by b.city having count(*)>=2");
sqlList.add("select b.gender from UserEntityIP b where trim(ifnull(b.gender,'')) <>''group by b.gender having count(*)>=2");
sqlList.add("select b.country from UserEntityIP b where trim(ifnull(b.country,'')) <>''group by b.country having count(*)>=2");
sqlList.add("select b.zipCode from UserEntityIP b where trim(ifnull(b.zipCode,'')) <>''group by b.zipCode having count(*)>=2");
sqlList.add("select b.ip from UserEntityIP b where trim(ifnull(b.ip,'')) <>''group by b.ip having count(*)>=2");
}
public static void main(String[] args) throws Exception {
Connection conn=initConnect();
Statement statement = conn.createStatement();
Long startTime =System.currentTimeMillis();

String sql = "select * from UserEntityIP";
ResultSet rs= statement.executeQuery(sql);
List recordList = new ArrayList();
while(rs.next()){
String fullName = rs.getString("familyName") +rs.getString("givenName");
String email = rs.getString("email");
String address1 = rs.getString("addressLine1");
String zipCode = rs.getString("zipCode");
String city = rs.getString("city");
String country = rs.getString("country");
String gender = rs.getString("gender");
String phone = rs.getString("phone");
String birthday = rs.getString("birthday");
String ipStr = rs.getString("ip");
Record record = new Record( fullName, email,
address1, zipCode, city,
country, gender, phone, birthday,ipStr);
recordList.add(record);
}

List distinctFullNameList = getAttList(statement, sqlList.get(0), "familyName", "givenName");
List distinctEmailList = getAttList(statement, sqlList.get(1), "email",null);
List distinctPhoneList = getAttList(statement, sqlList.get(2), "phone", null);
List distinctAddress1List = getAttList(statement, sqlList.get(3), "addressLine1",null);
List distinctBirthdayList = getAttList(statement, sqlList.get(4), "birthday", null);
List distinctCityList = getAttList(statement, sqlList.get(5), "city", null);
List distinctGenderList = getAttList(statement, sqlList.get(6), "gender", null);
List distinctCountryList = getAttList(statement, sqlList.get(7), "country", null);
List distinctZipcodeList = getAttList(statement, sqlList.get(8), "zipCode", null);
List distinctIPList = getAttList(statement, sqlList.get(9), "ip", null);


List fullNameList = new ArrayList();

List emailList = new ArrayList();

List address1List = new ArrayList();

List phoneList = new ArrayList();

List birthdayList = new ArrayList();

List cityList = new ArrayList();

List genderList = new ArrayList();

List countryList = new ArrayList();

List zipcodeList = new ArrayList();

List ipList = new ArrayList();

for(Record record:recordList){
fullNameList.add(record.getFullName());
emailList.add(record.getEmail());
address1List.add(record.getAddress1());
phoneList.add(record.getPhone());
birthdayList.add(record.getBirthday());
cityList.add(record.getCity());
genderList.add(record.getGender());
countryList.add(record.getCountry());
zipcodeList.add(record.getZipCode());
ipList.add(record.getIpStr());
}

int[] fullNameScores= calculateAttScore(fullNameList,distinctFullNameList,20);
int[] emailScores= calculateAttScore(emailList,distinctEmailList,20);
int[] address1Scores= calculateAttScore(address1List,distinctAddress1List,10);
int[] phoneScores= calculateAttScore(phoneList,distinctPhoneList,20);
int[] birthdayScores = calculateAttScore(birthdayList,distinctBirthdayList, 10);
int[] cityScores = calculateAttScore(cityList, distinctCityList, 2);
int[] genderScores = calculateAttScore(genderList, distinctGenderList, 1);
int[] countryScores = calculateAttScore(countryList, distinctCountryList, 2);
int[] zipcodeScores = calculateAttScore(zipcodeList, distinctZipcodeList, 3);
int[] ipScores = calculateAttScore(ipList, distinctIPList, 10);

int size = recordList.size();
System.out.println(fullNameScores.length);
int[] totalScores = new int[size];
for(int i=0;i totalScores[i]=fullNameScores[i]+
emailScores[i]+
address1Scores[i]+
phoneScores[i]+
birthdayScores[i]+
cityScores[i]+
genderScores[i]+
countryScores[i]+
zipcodeScores[i]+
ipScores[i];
if(totalScores[i]>=20)
System.out.println(totalScores[i] + "----------------"+fullNameList.get(i)+"----------------"+emailList.get(i)
+"----------------"+address1List.get(i)+"----------------"+
phoneList.get(i)+"----------------"+birthdayList.get(i));
}

System.out.println("Total execution time:"+(System.currentTimeMillis()-startTime));
}
static Connection initConnect(){
String driver = "com.mysql.jdbc.Driver";// 驅動程序名


String url = "jdbc:mysql://10.0.4.162/test"; // URL指向要訪問的數據庫名facerobot

String user = "betbrain"; // MySQL配置時的用戶名

String password = "betbrain"; // MySQL配置時的密碼
Connection conn =null;
try {

Class.forName(driver); // 加載驅動程序


conn = DriverManager.getConnection(url, user, password);
// 連接數據庫
}catch (Exception e) {
e.printStackTrace();
System.out.println("Connecting database error!");
}
return conn;
}
static int[] calculateAttScore(List attList,List distinctList,int hash){
int [] scores = new int[attList.size()];
for(int i=0;i if(scores[i]!=0 || attList.get(i)==null)
continue;
for(int j=0;j if(attList.get(i).equals(distinctList.get(j)))
scores[i]=hash;
}
}
return scores;
}
static List getAttList(Statement statement,String sql,String attName1,String attName2) throws Exception{

ResultSet rs= statement.executeQuery(sql);
List attValueList = new ArrayList();
String attValue = null;
while(rs.next()){
if(attName2!=null && attName1!=null){
attValue = rs.getString(attName1) +rs.getString(attName2);

attValueList.add(attValue);
}
else if(attName1!=null){
attValue = rs.getString(attName1) ;

attValueList.add(attValue);
}
}
return attValueList;
}
}

//package test.vo;


public class Record {
private String fullName;
private String email;
private String address1;
private String zipCode;
private String city;
private String country;
private String gender;
private String phone;
private String birthday;
private String ipStr;

public String getFullName() {
return fullName;
}

public String getEmail() {
return email;
}

public String getAddress1() {
return address1;
}

public String getCity() {
return city;
}

public String getCountry() {
return country;
}

public String getGender() {
return gender;
}

public String getPhone() {
return phone;
}

public String getBirthday() {
return birthday;
}

public String getZipCode() {
return zipCode;
}

public Record(String fullName, String email, String address1,
String zipCode, String city, String country, String gender,
String phone, String birthday,String ipStr) {
super();
this.fullName = fullName;
this.email = email;
this.address1 = address1;
this.zipCode = zipCode;
this.city = city;
this.country = country;
this.gender = gender;
this.phone = phone;
this.birthday = birthday;
this.ipStr = ipStr;
}

public String getIpStr() {
return ipStr;
}


}

[@more@]

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

卫辉市| 九龙城区| 大港区| 常州市| 通江县| 西丰县| 洛南县| 芦山县| 神农架林区| 屏南县| 华蓥市| 平遥县| 荥经县| 怀来县| 哈尔滨市| 平利县| 沧州市| 利川市| 乐业县| 文化| 辽阳县| 泗阳县| 建瓯市| 嘉定区| 宁波市| 海林市| 邢台市| 吐鲁番市| 康乐县| 荣昌县| 上饶市| 繁峙县| 房产| 安塞县| 德安县| 阜新市| 手机| 泰兴市| 金沙县| 铁岭市| 河南省|