使用java将数据从csv文件导入访问数据库

时间:2014-10-30 09:16:54

标签: java csv ms-access-2007 ucanaccess

我需要使用java将csv导入访问数据库。我尝试使用以下代码

我的代码:

  public static void main (String args[])
{
    String dbFileSpec = "C:\\Documents and Settings\\admin\\My Documents\\NetBeansProjects\\AutomateExcelDatabase\\Centre.accdb";
 //   String accessTableName = "Centre";
    String csvDirPath = "C:\\Documents and Settings\\admin\\My Documents\\NetBeansProjects\\AutomateExcelDatabase";
    String csvFileName = "myjdbcfile.csv";
    try (Connection conn = DriverManager.getConnection(
            "jdbc:ucanaccess://" + dbFileSpec
    //        + ";newdatabaseversion=V2007"
    )) {
        try
        {
            String strSQL = "SELECT * INTO " + dbFileSpec + " FROM [Text;HDR=YES;DATABASE=" + csvDirPath + ";].[" + csvFileName + "]";
            System.err.println("SQL --> "+strSQL);
            PreparedStatement selectPrepSt = conn.prepareStatement(strSQL);
            boolean result = selectPrepSt.execute();
            System.out.println("result = " + result);
        }
        catch(SQLException ex)
        {
            System.err.println("Error --->"+ex.toString());
        }
        conn.commit();
        conn.close();
    } catch (SQLException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

但它会抛出错误,因为" net.ucanaccess.jdbc.UcanaccessSQLException:意外令牌:INTO必需:FROM"。

2 个答案:

答案 0 :(得分:2)

尝试使用

的两个问题
SELECT ... INTO NewTableName FROM [Text; ...].[csvFileName]

在这方面是:

  1. SELECT ... INTO NewTableName FROM OldTableName是UCanAccess不支持的Access SQL构造(至少目前不支持),

  2. ... FROM [Text; ...].[csvFileName]是一个ODBC"技巧"并且UCanAccess不使用ODBC。

  3. 但是,UCanAccess使用HSQLDB,HSQLDB支持读取CSV文件,如下所示:

    final String csvFolder = "C:/__tmp/zzzTest/";
    final String csvFileName = "myjdbcfile.csv";
    final String csvDbName = "hsqldbTemp";
    try (Connection hconn = DriverManager.getConnection(
            "jdbc:hsqldb:file:" + csvFolder + "/" + csvDbName, 
            "SA", 
            "")) {
        try (Statement s = hconn.createStatement()) {
            s.executeUpdate("CREATE TEXT TABLE fromcsv (id int, textcol varchar(50))");
            s.executeUpdate("SET TABLE fromcsv SOURCE \"" + csvFileName + "\" DESC");
            try (ResultSet rs = s.executeQuery("SELECT * FROM fromcsv")) {
                while (rs.next()) {
                    System.out.println(rs.getString("textcol"));
                }
            }
            s.executeUpdate("SHUTDOWN");
            File f = null;
            f = new File(csvFolder + "/" + csvDbName + ".properties");
            f.delete();
            f = new File(csvFolder + "/" + csvDbName + ".script");
            f.delete();
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
    

    所以你可以使用两个连接,

    • 与Access数据库的一个jdbc:ucanaccess连接,以及

    • 与CSV文件的另一个jdbc:hsqldb连接

    然后将CSV文件中的行插入Access数据库中的表。

答案 1 :(得分:1)

您在此处输入了错误的查询,

  String strSQL = "SELECT * INTO " + dbFileSpec + " FROM
 [Text;HDR=YES;DATABASE=" + csvDirPath + ";].[" + csvFileName + "]";

应该是,

 String strSQL = "SELECT *" + dbFileSpec + " FROM [Text;HDR=YES;DATABASE=" + csvDirPath + ";].[" + csvFileName + "]";