Java – Why I obtain this “SQLSyntaxErrorException: ORA-00933: SQL command not properly ended” when I try to perform this JDBC query


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

So I have the following query:

FROM   coda_tx c, documenti_tx d
        AND C.FK_STATO = 1
        AND C.FK_PIVA_MITTENTE = '05779711000'
        AND C.CANALE='STA'

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

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 = '");

    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:

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

    at oracle.jdbc.driver.T4CTTIoer.processError(
    at oracle.jdbc.driver.T4CTTIoer.processError(
    at oracle.jdbc.driver.T4C8Oall.processError(
    at oracle.jdbc.driver.T4CTTIfun.receive(
    at oracle.jdbc.driver.T4CTTIfun.doRPC(
    at oracle.jdbc.driver.T4C8Oall.doOALL(
    at oracle.jdbc.driver.T4CStatement.doOall8(
    at oracle.jdbc.driver.T4CStatement.doOall8(
    at oracle.jdbc.driver.T4CStatement.executeForDescribe(
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(
    at oracle.jdbc.driver.OracleStatement.executeQuery(
    at oracle.jdbc.driver.OracleStatementWrapper.executeQuery(
    at DAO.FatturaDAO.getListaFatturePDF(
    at mainPkg.Main.main(

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?


Best Solution

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

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

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