执行INSERT语句HSQL Java的异常

时间:2014-02-22 21:41:59

标签: java eclipse hsqldb sql-insert

我有这个简单的代码,它让我发疯了......

public void insertVectorEstacionario() {
    DBManager dbM = DBManager.instance();
    if(N == 3) {
        dbM.insert("INSERT INTO Markov(IdIndicador, FechaDesde, FechaHasta, Pv, Pa, Pr, Pn) VALUES(" + 
                Integer.toString(idIndicador) + "," + "#" + inicio_periodo.toString() + "#" + "," + "#" + fin_periodo.toString() + "#" + "," +
                Double.toString(vector_estacionario[0]) + "," + Double.toString(vector_estacionario[1]) + "," + Double.toString(vector_estacionario[2]) + "," + Double.toString(0.0) + ")");

    }

    if(N == 4) {
        dbM.insert("INSERT INTO Markov(IdIndicador, FechaDesde, FechaHasta, Pv, Pa, Pr, Pn) VALUES(" + 
        Integer.toString(idIndicador) + "," + "#" + inicio_periodo.toString() + "#" + "," + "#" + fin_periodo.toString() + "#" + "," +
        Double.toString(vector_estacionario[0]) + "," + Double.toString(vector_estacionario[1]) + "," + Double.toString(vector_estacionario[2]) + "," + Double.toString(vector_estacionario[3]) + ")");
    }
}

现在,当N = 4时执行的第二个insert语句运行得很好,问题是第一个语句(当N = 3时)抛出一个嵌套的Exception,如下所示:

net.ucanaccess.jdbc.UcanaccessSQLException: unexpected token: Z_0536632167783088E required: )
at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:110)
at com.operativa.quartilegenerator.datamodel.DBManager.insert(DBManager.java:71)
at com.operativa.quartilegenerator.GenerarMarkov.insertVectorEstacionario(GenerarMarkov.java:85)
at com.operativa.quartilegenerator.QuartileGUI$16$1.doInBackground(QuartileGUI.java:969)
at com.operativa.quartilegenerator.QuartileGUI$16$1.doInBackground(QuartileGUI.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLSyntaxErrorException: unexpected token: Z_0536632167783088E required: )
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at net.ucanaccess.jdbc.Execute.executeWrapped(Execute.java:62)
at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:121)
at net.ucanaccess.jdbc.Execute.execute(Execute.java:52)
at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:107)
... 10 more
Caused by: org.hsqldb.HsqlException: unexpected token: Z_0536632167783088E required: )
at org.hsqldb.error.Error.parseError(Unknown Source)
at org.hsqldb.ParserBase.unexpectedTokenRequire(Unknown Source)
at org.hsqldb.ParserBase.readThis(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionWithContext(Unknown Source)
at org.hsqldb.ParserDQL.readRow(Unknown Source)
at org.hsqldb.ParserDQL.XreadContextuallyTypedTable(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 16 more

两个陈述几乎相同,只改变了第四个参数。我尝试了一切,但不能使这项工作。提前感谢您提供的任何帮助。

WITH PREPARE_STATEMENT:

public void insertVectorEstacionario() {
    DBManager dbM = DBManager.instance();
    PreparedStatement n3 = null;

    String strn3 = String.format("INSERT INTO Markov(IdIndicador, FechaDesde, FechaHasta, Pv, Pa, Pr, Pn) VALUES(" + 
            Integer.toString(idIndicador) + "," + "#" + inicio_periodo.toString() + "#" + "," + "#" + fin_periodo.toString() + "#" + "," +
            Double.toString(vector_estacionario[0]) + "," + Double.toString(vector_estacionario[1]) + "," + Double.toString(vector_estacionario[2]) + "," + "0.823749287592)");

    if(N == 3) {

        try {
            n3 = dbM.getConnection().prepareStatement(strn3);
            n3.execute();
        } catch(Exception e){e.printStackTrace();}
    }

}

这就是堆栈:

net.ucanaccess.jdbc.UcanaccessSQLException: unexpected token: Z_8901313574667937E required: )
at net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:455)
at com.operativa.quartilegenerator.GenerarMarkov.insertVectorEstacionario(GenerarMarkov.java:101)
at com.operativa.quartilegenerator.QuartileGUI$16$1.doInBackground(QuartileGUI.java:969)
at com.operativa.quartilegenerator.QuartileGUI$16$1.doInBackground(QuartileGUI.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLSyntaxErrorException: unexpected token: Z_8901313574667937E required: )
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:453)
... 9 more
Caused by: org.hsqldb.HsqlException: unexpected token: Z_8901313574667937E required: )
at org.hsqldb.error.Error.parseError(Unknown Source)
at org.hsqldb.ParserBase.unexpectedTokenRequire(Unknown Source)
at org.hsqldb.ParserBase.readThis(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionWithContext(Unknown Source)
at org.hsqldb.ParserDQL.readRow(Unknown Source)
at org.hsqldb.ParserDQL.XreadContextuallyTypedTable(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 12 more

1 个答案:

答案 0 :(得分:0)

以上面显示的方式生成的sql语句可以包含科学记数法,如下所示:

insert into T1(xxx) values(4.0E-16);

并且UCanAccess无法识别此数字格式。 您应该使用带参数值的预准备语句或正确格式化双值(例如,使用DecimalFormat)。