Apache Derby:java.sql.SQLSyntaxErrorException

时间:2014-03-31 16:22:49

标签: java sql csv derby

我刚刚开始了解SQL,并且正在尝试制作一个表格,并在CSV - 文件中填写一些数据。

以下是尝试执行此操作的应用程序:

public sqlThingy() throws Exception {

    File spritesheetsCreate = new File("sql/spritesheetsCreate.sql");
    File spritesheetsData = new File("sql/spritesheetsData.sql");

    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();

    Connection conn = DriverManager
            .getConnection("jdbc:derby:TESTNAME;create=true");

    //CREATE TABLE
    try (PreparedStatement statement = conn
            .prepareStatement(readFile(spritesheetsCreate))) {
        statement.execute();
    }


    //FILL TABLE WITH DATA
    try (PreparedStatement ps = conn.prepareStatement(readFile(spritesheetsData))) {
        try (CSVReader rdr = new CSVReader(new FileReader(new File(
                "res/spritesheets.csv")), ',', '"', 1)) {
            for (String[] row : rdr.readAll()) {
                ps.setString(1, row[0]);
                ps.setString(2, row[1]);
                ps.setInt(3, Integer.valueOf(row[2]));
                ps.execute();
            }
        }
    }
}


private static String readFile(File f) throws Exception {
    String contents = "";
    try (Scanner sc = new Scanner(f)) {
        contents = sc.useDelimiter("\\Z").next();
    }
    return contents;
}

public static void main(String[] args) throws Exception {

    ResourceLoaderSQL test = new ResourceLoaderSQL();

}

我的应用使用的两个SQL文件如下所示:

spritesheetsCreate.sql:

CREATE TABLE spritesheetstable( name VARCHAR(7) NOT NULL PRIMARY KEY , spritesheetlocation VARCHAR(15) , tilesize INTEGER )

spritesheetsData.sql:

INSERT INTO spritesheetstable("name", "spritesheetlocation", "tilesize") VALUES (?,?,?)

CSV - 文件如下所示:

spritesheets.csv:

name,spritesheet-location,tilesize
spot1,location/loc1,10
spot2,location/loc2,20
spot3,location/loc3,30
spot4,location/loc4,40

当这个美丽的运行时,我得到的错误是:

Exception in thread "main" java.sql.SQLSyntaxErrorException: 'name' is not a column in table or VTI 'APP.SPRITESHEETSTABLE'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement42.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver42.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at game.io.ResourceLoaderSQL.<init>(ResourceLoaderSQL.java:36)
at game.io.ResourceLoaderSQL.main(ResourceLoaderSQL.java:83)
Caused by: java.sql.SQLException: 'name' is not a column in table or VTI 'APP.SPRITESHEETSTABLE'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 16 more
Caused by: ERROR 42X14: 'name' is not a column in table or VTI 'APP.SPRITESHEETSTABLE'.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.compile.ResultColumn.bindResultColumnByName(Unknown Source)
at org.apache.derby.impl.sql.compile.ResultColumnList.bindResultColumnsByName(Unknown Source)
at org.apache.derby.impl.sql.compile.InsertNode.bindStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 10 more

所以有人能发现这里发生了什么吗?我无法看到解决方案。

1 个答案:

答案 0 :(得分:5)

引用表名称使它们区分大小写。

您正在使用不带引号的字段名创建表,这会使列名称不区分大小写(和大写),但您在查询中使用小写引用它们,因此查询无法找到它们。

删除两个地方的引用,或者在创建表和查询中始终引用名称。