2011-01-02 21 views
1

MavenリポジトリでオープンソースのExcel/CSV/XMLファイルJDBCドライバが利用できますか? JDBC-ODBCブリッジ機構は非常に扱いにくく、DataSourceをうまくサポートしていません(オプション機能は実装されていない例外です)。読み取り/書き込み能力は不可欠ですが、それ以上のものがなければ読み取り専用機能があります。Mavenリポジトリ上のExcel用のオープンソースJDBCドライバ、Mavenリポジトリ上のCSVファイル

+1

?かなり重く、ファイルに書き込むのは面倒です。より適切なレベルの抽象化を提供するOpenCSVのような、以下の提案の1つを行ってください。 – GaryF

答えて

-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(); 
+0

これは優れたツールですが、特にExcelファイルが関係している場合は、質問に実際には答えません。 –

1

CsvJdbcは、カンマ区切り形式のファイルを読み取るためのJavaデータベースドライバです。

http://csvjdbc.sourceforge.net/

Mavenのレポ:

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

使用例:JDBCを使用する理由

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(); 
    } 
    } 
} 
関連する問題