1

每当我尝试检索 Access db (.mdb) 中名为“Comment”的列的值时,都会出现以下错误。

Offence: copying data
ERROR - Offence TABLE
java.sql.SQLSyntaxErrorException: ORA-00904: "Comment": invalid identifier

这是我的代码:

public static void copyOffenceTable()
    {
        PreparedStatement updateOffenceTypeTable  = null;

        //Set up "insert" string
        String insertString = "INSERT INTO Offence "
                + "(id, OffenceName, status, OffenceSuburb, OffenceDate, FirstName, LastName, \"Comment\", OffenderAddress, OffenderSuburb, OffenderPostcode, OffenderState, OffenderEmail, LicenceNumber, DOB, StaffIssuerID, OffenderPhone) "                                          
                + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        String MSAccessQuery = "SELECT * FROM Offence";

        try
        {
            //create a query object for MSAccess
            dbmsMSAccess.DBMSStatement = dbmsMSAccess.DBMSConnection.createStatement();
            //Query the MSAccess database for extraction
            dbmsMSAccess.DBMSResultSet = dbmsMSAccess.DBMSStatement.executeQuery(MSAccessQuery);
            //make query object for Oracle
            dbmsOracle.DBMSOracleStatement = dbmsOracle.DBMSOracleConnection.createStatement();

            dbmsOracle.DBMSOracleStatement.execute("TRUNCATE TABLE Offence");

            dbmsOracle.DBMSOracleConnection.setAutoCommit(false);

            updateOffenceTypeTable = dbmsOracle.DBMSOracleConnection.prepareStatement(insertString);

            System.out.println("Offence: copying data");

            while(dbmsMSAccess.DBMSResultSet.next())
            {
                updateOffenceTypeTable.setInt(1, dbmsMSAccess.DBMSResultSet.getInt("id") );
                updateOffenceTypeTable.setString(2, dbmsMSAccess.DBMSResultSet.getString("OffenceName") );
                updateOffenceTypeTable.setString(3, dbmsMSAccess.DBMSResultSet.getString("Status") );
                updateOffenceTypeTable.setString(4, dbmsMSAccess.DBMSResultSet.getString("OffenceSuburb") );
                updateOffenceTypeTable.setDate(5, dbmsMSAccess.DBMSResultSet.getDate("OffenceDate") );
                updateOffenceTypeTable.setString(6, dbmsMSAccess.DBMSResultSet.getString("FirstName") );
                updateOffenceTypeTable.setString(7, dbmsMSAccess.DBMSResultSet.getString("LastName") );
                updateOffenceTypeTable.setString(8, dbmsMSAccess.DBMSResultSet.getString("COMMENT") );
                updateOffenceTypeTable.setString(9, dbmsMSAccess.DBMSResultSet.getString("OffenderAddress") );
                updateOffenceTypeTable.setString(10, dbmsMSAccess.DBMSResultSet.getString("OffenderSuburb") );
                updateOffenceTypeTable.setInt(11, dbmsMSAccess.DBMSResultSet.getInt("OffenderPostcode") );
                updateOffenceTypeTable.setString(12, dbmsMSAccess.DBMSResultSet.getString("OffenderState") );
                updateOffenceTypeTable.setString(13, dbmsMSAccess.DBMSResultSet.getString("OffenderEmail") );
                updateOffenceTypeTable.setInt(14, dbmsMSAccess.DBMSResultSet.getInt("LicenceNumber") );
                updateOffenceTypeTable.setDate(15, dbmsMSAccess.DBMSResultSet.getDate("DOB") );
                updateOffenceTypeTable.setInt(16, dbmsMSAccess.DBMSResultSet.getInt("StaffIssuerID") );
                updateOffenceTypeTable.setInt(17, dbmsMSAccess.DBMSResultSet.getInt("OffenderPhone") );

                updateOffenceTypeTable.executeUpdate();
            }

            System.out.println("Offence: ACCESS DATA COPIED TO ORACLE\n");
        }
        catch(Exception X)
        {
            System.out.println("ERROR - Offence TABLE");
            X.printStackTrace();
        } 
    }

有没有办法通过jdbc引用也是关键字的列?

我试过在撇号和引号之间加上“评论”。

有什么建议么?

编辑:这些值被放入一个具有假定相同表的 oracle 数据库中。

4

5 回答 5

0

无法在查询级别重命名您的列?就像是 select comment as my_comment from blabla

您还可以使用其索引到达该字段

于 2013-05-20T12:03:46.967 回答
0

最后,这些建议的方法都不适合我,但是值得一试!

我最终不得不进入数据库并获得将字段从评论更改为评论的许可。

感谢所有发帖的人,我真的很感激!

于 2013-05-20T13:20:50.557 回答
0

使用反引号(或反引号)来转义有问题的列的名称,例如`\"Comment\"`

String insertString = "INSERT INTO Offence "
+ "(id, OffenceName, status, OffenceSuburb, OffenceDate, FirstName, LastName, `\"Comment\"`, OffenderAddress, OffenderSuburb, OffenderPostcode, OffenderState, OffenderEmail, LicenceNumber, DOB, StaffIssuerID, OffenderPhone) "                                          
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
于 2013-05-20T12:38:57.243 回答
0

尝试使用一些括号:

 String insertString = "INSERT INTO Offence "
                + "(id, OffenceName, status, OffenceSuburb, OffenceDate, FirstName, LastName, [Comment], OffenderAddress, OffenderSuburb, OffenderPostcode, OffenderState, OffenderEmail, LicenceNumber, DOB, StaffIssuerID, OffenderPhone) "                                          
                + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

编辑:对于 oracle,如果我们信任这个主题:https ://stackoverflow.com/a/1162391/291541

解决方案是双引号,但您必须注意大小写。请检查您的专栏是否是评论而不是示例评论。

于 2013-05-20T12:01:21.747 回答
0

请删除查询中的双引号

   String insertString = "INSERT INTO Offence "
                + "(id, OffenceName, status, OffenceSuburb, OffenceDate, FirstName, LastName, Comment, OffenderAddress, OffenderSuburb, OffenderPostcode, OffenderState, OffenderEmail, LicenceNumber, DOB, StaffIssuerID, OffenderPhone) "                                          
                + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
于 2013-05-20T12:02:01.467 回答