一个Maven仓库上的Excel、CSV文件的开源JDBC驱动程序

3

是否有一个开源的Excel/CSV/XML文件JDBC驱动程序可以在Maven存储库中获得?JDBC-ODBC桥接机制非常繁琐,不支持DataSource(可选特性未实现异常)。读/写能力是必要的,但如果没有更好的选择,只读也可以。


1
为什么要使用JDBC?它对于文件写入来说相当笨重和繁琐。选择下面的建议之一,比如OpenCSV,它提供了更合适的抽象层级。 - GaryF
2个回答

2

CsvJdbc是一个用于读取逗号分隔值文件的Java数据库驱动程序。

http://csvjdbc.sourceforge.net/

Maven仓库:

<dependency>
  <groupId>net.sourceforge.csvjdbc</groupId>
  <artifactId>csvjdbc</artifactId>
  <version>1.0.9</version>
</dependency>

使用示例:

import java.sql.*;

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

      // Create a connection. The first command line parameter is
      // the directory containing the .csv files.
      // A single connection is thread-safe for use by several threads.
      Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + args[0]);

      // Create a Statement object to execute the query with.
      // A Statement is not thread-safe.
      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 to a CSV file with the same format
      // using CsvJdbc helper function
      boolean append = true;
      CsvDriver.writeToCsv(results, System.out, append);

      // Clean up
      conn.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}

-1

OpenCSV

http://opencsv.sourceforge.net/

<dependency>
    <groupId>net.sf.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>2.0</version>
</dependency>

阅读

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

 CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
 // feed in your array (or convert your data to an array)
 String[] entries = "first#second#third".split("#");
 writer.writeNext(entries);
 writer.close();

虽然这是一个很好的工具,但它并没有真正回答问题,特别是涉及到Excel文件的时候... - Lukas Eder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接