java jackcess 操作 access

  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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值