2
votes

I need to import csv into access database using java. I tried using the following code

my code:

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

}

But it throws error as "net.ucanaccess.jdbc.UcanaccessSQLException: unexpected token: INTO required: FROM".

2

2 Answers

2
votes

The two problems with trying to use

SELECT ... INTO NewTableName FROM [Text; ...].[csvFileName]

in this context are:

  1. SELECT ... INTO NewTableName FROM OldTableName is an Access SQL construct that UCanAccess does not support (at least not at the moment), and

  2. ... FROM [Text; ...].[csvFileName] is an ODBC "trick" and UCanAccess does not use ODBC.

However, UCanAccess uses HSQLDB and HSQLDB offers support for reading CSV files like so:

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

so you could use two connections,

  • one jdbc:ucanaccess connection to the Access database, and

  • another jdbc:hsqldb connection to the CSV file,

and then insert the rows from the CSV file into a table in the Access database.

1
votes

You have mis-typed the query here,

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

should be ,

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