java jackcess 操作 access

来源:互联网 发布:南昌金域名都地址 编辑:程序博客网 时间:2024/06/07 10:08
  1. package com;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.sql.SQLException;  
  6. import java.sql.Types;  
  7. import java.util.Collections;  
  8.   
  9. import com.healthmarketscience.jackcess.ColumnBuilder;  
  10. import com.healthmarketscience.jackcess.CursorBuilder;  
  11. import com.healthmarketscience.jackcess.Database;  
  12. import com.healthmarketscience.jackcess.DatabaseBuilder;  
  13. import com.healthmarketscience.jackcess.Row;  
  14. import com.healthmarketscience.jackcess.Table;  
  15. import com.healthmarketscience.jackcess.TableBuilder;  
  16. import com.healthmarketscience.jackcess.util.ImportUtil;  
  17.    
  18. public class Access {  
  19.   
  20.     /** 
  21.      * @param args 
  22.      * @throws IOException  
  23.      */  
  24.     public static void main(String[] args) throws IOException {   
  25.         // getAccessDataTable();  
  26.         //createTable();   
  27.         //createTableRecByCsv() ;  
  28.         deleteRec();  
  29.     }  
  30.     /** 
  31.      * 读取指定表格 
  32.      * @throws IOException 
  33.      */  
  34.     public static void getAccessDataTable() throws IOException{  
  35.         Table table = DatabaseBuilder.open(new File("c:\\data.mdb")).getTable("AspCms_Collect");  
  36.         for(Row row : table) {  
  37.           System.out.println("--ID--" + row.get("CollectID")+"--Name--" + row.get("CollectName"));  
  38.         }  
  39.     }  
  40.     /** 
  41.      * 创建表并写入数据 
  42.      * @throws IOException 
  43.      */  
  44.     public static void createTable() throws IOException{  
  45.          Database db = DatabaseBuilder.create(Database.FileFormat.V2000, new File("c:\\new.mdb"));  
  46.          Table newTable;  
  47.         try {  
  48.             newTable = new TableBuilder("NewTable")  
  49.                .addColumn(new ColumnBuilder("a")  
  50.                           .setSQLType(Types.INTEGER))  
  51.                .addColumn(new ColumnBuilder("b")  
  52.                           .setSQLType(Types.VARCHAR))  
  53.                .toTable(db);  
  54.              newTable.addRow(1"foo");   
  55.         } catch (SQLException e) {  
  56.             // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.     /** 
  61.      *  将CSV文件的内容复制到一个新表: 
  62.      * @throws IOException 
  63.      */  
  64.     public  static void createTableRecByCsv() throws IOException{  
  65.         Database db = DatabaseBuilder.open(new File("c:\\new.mdb"));  
  66.         new ImportUtil.Builder(db, "NewTable22").setDelimiter(",").importFile(new File("c:\\test.csv"));  
  67.         db.close();  
  68.     }  
  69.     /** 
  70.      * 删除一条数据 
  71.      * @throws IOException 
  72.      */  
  73.     public static void deleteRec() throws IOException{  
  74.         Table table = DatabaseBuilder.open(new File("c:\\new.mdb")).getTable("NewTable22");  
  75.         Row row = CursorBuilder.findRow(table, Collections.singletonMap("xh""4"));  
  76.         if(row != null) {  
  77.           System.out.println("Found row where 'a' == 'foo': " + row);  
  78.           table.deleteRow(row);  
  79.         } else {  
  80.           System.out.println("Could not find row where 'a' == 'foo'");  
  81.         }  
  82.     }  
  83.       
  84.       
  85. }