csvjdbc——JDBC方式访问csv文件或dbf文件

介绍
CsvJdbc是一个只读JDBC驱动程序,使用csv文件或DBF文件作为数据库表。
通过csvjdbc驱动程序,可以访问包含CSV或DBF文件的目录或ZIP文件,就像访问包含表的数据库一样。
由于没有真正的数据库管理系统,并非所有JDBC功能都可用。

用法
CsvJdbc驱动程序的使用与其他任何JDBC驱动程序一样:
1)下载csvjdbc.jar并将其添加到Java CLASSPATH。
2)加载驱动程序类(其全名是org.relique.jdbc.csv.CsvDriver)
3)使用DriverManager连接到数据库(目录或ZIP文件)
4)创建一个语句对象
5)使用语句对象执行SQL SELECT查询
6)查询的结果是一个ResultSet

范例


```java
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 to directory given as first command line
    // parameter. Driver properties are passed in URL format
    // (or alternatively in a java.utils.Properties object).
    //
    // A single connection is thread-safe for use by several threads.
    String url = "jdbc:relique:csv:" + args[0] + "?" +
      "separator=;" + "&" + "fileExtension=.txt";
    Connection conn = DriverManager.getConnection(url);

    // 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();
  }
}

官方网站
http://csvjdbc.sourceforge.net/
GitHub:https://github.com/jprante/jdbc-driver-csv

发布了105 篇原创文章 · 获赞 552 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/yuan1164345228/article/details/104315561