http://sourceforge.net/projects/csvjdbc/
这个工具包是只读的,没写操作。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class TestCsvJdbcConn {
/**
* CSV JDBC驱动
*/
private static final String CSV_JDBC_DRIVER = "org.relique.jdbc.csv.CsvDriver";
/**
* jdbc连接csv的 Header
*/
private static final String CSV_JDBC_HEADER = "jdbc:relique:csv:";
/**
* separator 参数设置: CSV 文件中数据分割符
*/
private static final String CSV_PROP_SEPARATOR = "separator";
/**
* separator 参数设置: 首行包含数据否
*/
private static final String CSV_PROP_SUPHEADER = "suppressHeaders";
/**
* fileExtension 参数设置: 文件类型
*/
private static final String CSV_PROP_FILEEXTEN = "fileExtension";
/**
* charset 参数设置: 字符集
*/
private static final String CSV_PROP_CHARSET = "charset";
/**
* 使用CSV JDBC驱动解析CSV文件
* parse
* @param csvDirectory String CSV文件所在目录
* @param csvName String CSV文件名(不包含文件类型)
*/
public final static void parse(final String csvDirectory, final String csvName) {
try {
// 加载CSV-JDBC驱动
Class.forName(CSV_JDBC_DRIVER);
// 解析CSV前的一些准备工作:解析参数设置
final Properties props = new java.util.Properties();
// 该CSV的数据是由','分隔
props.put(CSV_PROP_SEPARATOR, ",");
// 首行(去掉上面头行后的第一行)包含数据
props.put(CSV_PROP_SUPHEADER, "false");
// 要解析的文件类型
props.put(CSV_PROP_FILEEXTEN, ".csv");
// 字符集
props.put(CSV_PROP_CHARSET, "UTF-8");
// 创建一个connection. The first command line parameter is assumed to
// be the directory in which the .csv files are held
final Connection conn = DriverManager.getConnection(CSV_JDBC_HEADER
+ csvDirectory, props);
// create a Statement object to execute the query with
final Statement stmt = conn.createStatement();
// Select the ID and NAME columns from sample.csv
final ResultSet results =
stmt.executeQuery("SELECT "
+ " ipAddress macAddress userName "
+ " FROM "
+ csvName );
int i = 0;
while (results.next()) {
i++;
System.out.println(results.getString("ipAddress"));
}
// clean up
results.close();
stmt.close();
conn.close();
} catch (final ClassNotFoundException e) {
e.printStackTrace();
} catch (final SQLException e) {
e.printStackTrace();
}
}
}
官网例子: https://sourceforge.net/p/csvjdbc/website/ci/master/tree/www/index.md
CsvJdbc is a read-only JDBC driver that uses Comma Separated Value (CSV) files
or DBF files as database tables. It is ideal for writing data import programs
or analyzing log files.
The driver enables a directory or a ZIP file containing CSV or DBF files to be
accessed as though it were a database containing tables. However, as there is
no real database management system behind the scenes, not all JDBC
functionality is available.
The CsvJdbc driver is used just like any other JDBC driver:
csvjdbc.jar
and add it to the Java CLASSPATH.org.relique.jdbc.csv.CsvDriver
)DriverManager
to connect to the database (the directory or ZIP file)ResultSet
import java.sql.*;
import org.relique.jdbc.csv.CsvDriver;
public class DemoDriver
{
public static void main(String[] args) throws Exception
{
// 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();
}
}
转自博客:https://blog.csdn.net/ahhsxy/article/details/5974692?locationNum=3&fps=1