CsvJdbc - Java 访问csv 文件的的JDBC驱动

来源:互联网 发布:数据分析师证书分几级 编辑:程序博客网 时间:2024/05/22 06:41
CSV(Comma Separated Value),Foxmail 和 Outlook 导出的地址本都可以是 csv 文件。CsvJdbc 提供了Java 访问 csv 文件的的 JDBC 驱动,它其实是把一个 csv 文件当做一个数据库表来操作,提供简单的查询。

另外,在 CsvJDBC 的基础上衍生了另一个开源项目,XLSJDBC,看名字就知道,XLSJDBC 是访问 xls 文件的 JDBC 驱动。

CsvJdbc 和 XLSJDBC 都只提供只读的方式,意味着只能SELECT,不能 UPDATE 和 DELETE。
以下是使用的例子:

import java.sql.*;

public class DemoDriver
{
  public static void main(String[] args)
  {
    try
    {
      // load the driver into memory
      Class.forName("org.relique.jdbc.csv.CsvDriver");

      // create a connection. The first command line parameter is assumed to
      //  be the directory in which the .csv files are held
      Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + args[0] );

      // create a Statement object to execute the query with
      Statement stmt = conn.createStatement();

      // Select the ID and NAME columns from sample.csv
      ResultSet results = stmt.executeQuery("SELECT ID,NAME FROM sample");

      // dump out the results
      while (results.next())
      {
        System.out.println("ID= " + results.getString("ID") + "   NAME= " + results.getString("NAME"));
      }

      // clean up
      results.close();
      stmt.close();
      conn.close();
    }
    catch(Exception e)
    {
      System.out.println("Oops-> " + e);
    }
  }
}

转自:http://www.sentom.net

<script type="text/javascript">&lt;!--google_ad_client = "pub-4465827781731051";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel ="";//--&gt;</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4465827781731051&amp;dt=1125637635921&amp;format=468x60_as&amp;output=html&amp;ad_type=text_image&amp;nloc=http://blog.csdn.net/fckeditor/editor/fckeditor.html?InstanceName=Editor_Edit_FCKEditor&amp;Toolbar=Default&amp;u_h=768&amp;u_w=1024&amp;u_ah=740&amp;u_aw=1024&amp;u_cd=16&amp;u_tz=540&amp;u_his=9&amp;u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="65535"></iframe>