今天经理给了我一个三十多M的sql文件,让我测试数据定位的问题。按照惯例,我使用navicat for
sqlite创建一个表,然后将sql文件导入。我然后去干其他事儿了,大约过了一个多小时,我想数据应该导入的差不多了吧。我打开一看,汗,死在那儿了。我关掉软件又重新导入一遍,还是那个德行。又得知经理曾经自己也导过,没有成功。看来,用工具导入的方法行不通了。
但是,想想就十多万条数据,就是十多万条insert sql语句,有那么难吗?于是,我想还是自己写一个程序导入吧。虽然中间也遇到一些小插曲,但是还是成功地把数据导进去了。
package com.geoway.pad.common.tool; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * @author likehua * @note SQLite建库以及批量入库 * */ public class BatchTool{ //ddl private static String ddl="CREATE TABLE IF NOT EXISTS pbeijing_point (OBJECTID INTEGER,NAME TEXT,ADDRESS TEXT,PHONE TEXT,FAX TEXT,TYPE TEXT,CITYCODE TEXT,URL TEXT,EMAIL TEXT,NAME2 TEXT,X INTEGER,Y INTEGER)"; Connection jCon=null; //get connection public synchronized Connection getConnection(){ if(jCon==null){ // json= Statement state=null; try { Class.forName("org.sqlite.JDBC"); jCon=DriverManager.getConnection("jdbc:sqlite:c:\\newD.db"); state=jCon.createStatement(); state.executeUpdate(ddl); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return jCon; } //创建500个线程 ExecutorService service=Executors.newFixedThreadPool(500); //读取sql文件 每五百个insert 语句由一个线程批量操作 public void readBatchSQL(InputStream is) throws IOException{ BufferedReader bufferReader=new BufferedReader(new InputStreamReader(is,"UTF-8")); String line; String one=""; int tag=0; String batchSql=""; while((line=bufferReader.readLine())!=null){ one+=line; if(one.indexOf(";")!=-1){ batchSql+=one; one="";//reset tag++; }; //符合条件 开辟一个线程 if(tag!=0&&tag/500!=0){ service.execute(new SQLiteBatchHandler(batchSql)); batchSql="";//reset tag=0;//reset } } //最后执行 剩余的sql if(batchSql.length()>0){ System.out.println("finalSQL:"+batchSql); Runnable r=new SQLiteBatchHandler(batchSql); service.execute(r); }; try { //关闭线程池 this.service.shutdown(); this.service.awaitTermination(1, TimeUnit.HOURS);<BR> getConnection().close();<BR> } catch (InterruptedException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }; /** * @note 分割sql * */ private static String[] splitSQL(String batchSQl){ if(batchSQl!=null){ return batchSQl.split(";"); }; return null; } /** * @note 执行批量更新操作 * 由于connection.comit 操作时 如果存在 statement没有close 就会报错 因此将此方法加上同步 。 * */ private synchronized void exucteUpdate(String batch){ Statement ste=null; Connection con=null; try{ con=getConnection(); con.setAutoCommit(false); ste=con.createStatement(); String[] sqls=this.splitSQL(batch); for(String sql:sqls){ if(sql!=null){ ste.addBatch(sql); }; }; ste.executeBatch();<BR> ste.close(); con.commit();//提交 }catch(Exception e){ e.printStackTrace(); System.out.println("执行失败:"+batch); try { con.rollback();//回滚 } catch (SQLException e1) { e1.printStackTrace(); } }finally{ if(ste!=null){ try { ste.close(); } catch (SQLException e) { e.printStackTrace(); } } } } /** * @author likehua * @note 入库线程 * */ private class SQLiteBatchHandler implements Runnable{ private String batch; public SQLiteBatchHandler(String sql){ this.batch=sql; }; @SuppressWarnings("static-access") @Override public void run() { try { Thread.currentThread().sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } if(this.batch.length()>0){ exucteUpdate(batch); }; } } /** * @author likehua * @note 主函数入口 * */ public static void main(String[] args) throws FileNotFoundException, IOException{ BatchTool s=new BatchTool(); s.readBatchSQL(new FileInputStream(new File("c:\\poi.sql"))); } }
以上就是深入Sqlite多线程入库的问题的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)