当前位置: 首页 > 工具软件 > Jackcess > 使用案例 >

jackcess,ucanaccess,java,处理大数据时效率的优化

陶星辰
2023-12-01

jackcess,ucanaccess在java8中用来和access文件(.mdb)进行对接。把.mdb文件看做access数据库进行操作。
access数据库优化的很差,在处理大数据时很难和oracle之类的数据库媲美。
一下分享优化方法。
直接上代码。 下面这个类是个工具类,写的不好请理解。
其实优化的主要关键还是利用 jackcess的api addRows。
添加的而对象是list,在操作大数据的时候所有的操作都在list里面进行,处理完毕后再添加到access里面。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;

public class AccessUtil {

   private String FilePath;
   private Table tableName;
   private String index;
   private Database database ;
   public Table getTableName() {
	return tableName;
}
public void setTableName(Table tableName) {
	this.tableName = tableName;
}
 
    public List<String> getColumns() {
    	 
		try {
			 
			Database a=tableName.getDatabase();
		
		} catch ( Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
    	 List<? extends Column> list=tableName.getColumns();
    	 List<String> columnList =new ArrayList<>();
    	 for(Column cc :list){
    		 columnList.add(cc.getName());
    	 }
    	return columnList;
    }
    //添加内容
    @SuppressWarnings("unchecked")
	public  void addRow(Map content){

	    	try {
	    		tableName.addRowFromMap(content);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
    }
  //批量添加内容
    @SuppressWarnings("unchecked")
	public  void addRows(List<Map<String,Object>> list){
	    	try {
	    		tableName.addRowsFromMaps(list);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	    	    }
    @SuppressWarnings("unchecked")
	public void updateRow(String primaryKey,Map content){
    	 
    	try {
			Row row = CursorBuilder.findRow(tableName, Collections.singletonMap(primaryKey, content.get(primaryKey).toString()));
			if(row!=null){
				tableName.deleteRow(row);
				tableName.addRowFromMap(content);
			}else{
				System.out.println("数据未找到");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    }
    
    	public void deleteRow(String primaryKey,Map content){
    	 
    	try {
    		
			Row row = CursorBuilder.findRow(tableName, Collections.singletonMap(primaryKey, content.get(primaryKey).toString()));
			if(row!=null){
				tableName.deleteRow(row);

			}else{
				System.out.println("数据未找到");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    }
    
    /** 
     * 创建表并写入数据 
     * @throws IOException 
     */  
    public  void createTable(String name,List<ColumnBuilder> mm) throws IOException{  
       
        try {  
        	tableName = new TableBuilder(name).addColumns(mm).toTable(database);
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
 
  

public static void main(String[] args) throws Exception {
	


	
}
public static boolean isEmpty(Object object){
	return object==null||object.toString().equals("");
}
public String getIndex() {
	return index;
}
public void setIndex(String index) {
	this.index = index;
}
 


public String getFilePath() {
	return FilePath;
}
public void setFilePath(String filePath) {
	FilePath = filePath;
}
public Database getDatabase() {
	return database;
}
public void setDatabase(Database database) {
	this.database = database;
}

}

 类似资料: