为什么我得到这个“SQLSyntaxErrorException:ORA-00933:SQL命令未正确结束”当我尝试执行此JDBC查询时? [英] Why I obtain this "SQLSyntaxErrorException: ORA-00933: SQL command not properly ended" when I try to perform this JDBC query?

查看:1407
本文介绍了为什么我得到这个“SQLSyntaxErrorException:ORA-00933:SQL命令未正确结束”当我尝试执行此JDBC查询时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以我有以下查询:

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'

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

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

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

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

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.
    }

}

问题是当我尝试执行上一个方法抛出以下 SQLException

The problem is that when I try to perform the previous method is thrown the following 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)

为什么?我认为SQL语法可能有问题,但我不确定(因为如果我执行查询到 Oracle SQL Developer 它工作正常)我缺少什么?我该如何解决?

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 Developer it works fine) What am I missing? How can I fix it?

Tnx

推荐答案

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

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

更改行 sb。追加(';); sb.append(');

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

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

这篇关于为什么我得到这个“SQLSyntaxErrorException:ORA-00933:SQL命令未正确结束”当我尝试执行此JDBC查询时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆