oracle 当我尝试执行此 JDBC 查询时,为什么会出现此“SQLSyntaxErrorException:ORA-00933:SQL 命令未正确结束”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28544688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:44:20  来源:igfitidea点击:

Why I obtain this "SQLSyntaxErrorException: ORA-00933: SQL command not properly ended" when I try to perform this JDBC query?

javasqloraclejakarta-eejdbc

提问by AndreaNobili

I have some problem trying to implement a simple JDBC query into a Java application.

我在尝试在 Java 应用程序中实现简单的 JDBC 查询时遇到了一些问题。

So I have the following query:

所以我有以下查询:

SELECT   D.*
FROM   coda_tx c, documenti_tx d
WHERE   C.FK_TIPO_DOC = 99
        AND C.FK_STATO = 1
        AND C.FK_PIVA_MITTENTE = '05779711000'
        AND C.PK_CODA = D.PFK_CODA
        AND C.CANALE='STA'

If I run it into Oracle SQL Developerit run well and I obtain 2 records as result.

如果我将它运行到Oracle SQL Developer 中,它运行良好,结果我获得了 2 条记录。

So I have to implement this query into a DAO class of my application in which I definied the following method:

所以我必须将此查询实现到我的应用程序的 DAO 类中,我在其中定义了以下方法:

public void getListaFatturePDF(String partitaIva) {
    System.out.println("INTO ottieniListaFatturePDF()");

    Blob blobPdf;
    String sql;

    StringBuffer sb = new StringBuffer();
    sb.append("SELECT D.*");
    sb.append("FROM coda_tx c, documenti_tx d");
    sb.append("WHERE C.FK_TIPO_DOC = 99");
    sb.append("AND C.FK_STATO = 1");
    sb.append("AND C.PK_CODA = D.PFK_CODA");
    sb.append("AND C.CANALE='STA'");
    sb.append("AND C.FK_PIVA_MITTENTE = '");
    sb.append(partitaIva);
    sb.append("';");

    sql = sb.toString();

    try {
        statment = connection.createStatement();
        ResultSet rs = statment.executeQuery(sql);
        System.out.println("ResultSet obtained");
    } catch (SQLException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

}

The problem is that when I try to perform the previous method is thrown the following SQLException:

问题是当我尝试执行前面的方法时抛出以下SQLException

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:210)
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:30)
    at oracle.jdbc.driver.T4CStatement.executeForDescribe(T4CStatement.java:762)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
    at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:1309)
    at oracle.jdbc.driver.OracleStatementWrapper.executeQuery(OracleStatementWrapper.java:422)
    at DAO.FatturaDAO.getListaFatturePDF(FatturaDAO.java:43)
    at mainPkg.Main.main(Main.java:74)

Why? I think that maybe is something wrong in the SQL syntax but I am not sure about it (because if I perform the query into Oracle SQL Developerit works fine) What am I missing? How can I fix it?

为什么?我认为这可能是 SQL 语法有问题,但我不确定(因为如果我在Oracle SQL Developer 中执行查询它工作正常)我错过了什么?我该如何解决?

Tnx

田纳西州

回答by daZza

executeQuery()automatically adds a semicolon to a statement when executing it.

executeQuery()执行语句时自动为语句添加分号。

Change the line sb.append("';");to sb.append("'");.

将该行更改sb.append("';");sb.append("'");

Also you'll need to add spaces at the end or at the beginning of each line, your statements are invalid otherwise.

此外,您还需要在每行的末尾或开头添加空格,否则您的语句无效。

回答by David Levesque

Add a white space at the end of each line so that the keyword on the next line is not clumped with it, e.g.:

在每一行的末尾添加一个空格,以便下一行的关键字不会与它聚在一起,例如:

sb.append("SELECT D.* ");

instead of

代替

sb.append("SELECT D.*");

and also remove the trailing semicolon.

并删除尾随的分号。