Home > Net >  SQL Query works in SQLDeveloper but won't run in JDBC
SQL Query works in SQLDeveloper but won't run in JDBC

Time:12-17

My query is "SELECT mbr,ime,prz FROM radnik WHERE mbr IN (SELECT DISTINCT sef FROM radnik)" , and it does the job in SQLDeveloper. However when I run it in Eclipse i get the error "SQL command not properly ended". The issue persists even after removing the semicolon from the query. Here's the code:


public class Upit2_Sefovi {

    public static void main(String[] args) {
        
        try (Connection connection = ConnectionUtil_Basic.getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(generateQuery());
                ResultSet resultSet = preparedStatement.executeQuery()) {
            
            while (resultSet.next()) {
                int mbr = resultSet.getInt(1);
                String ime = resultSet.getString(2);
                String prezime = resultSet.getString(3);
                int brRadnika=ExecuteFun.getRadnici(mbr);

                System.out.printf("%-4d %-8s %-8s %-4d\n", mbr, ime, prezime,brRadnika);
            }
                
            
            
        }catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    private static String generateQuery() {
        String query = "select mbr,ime,prz"
                  " from radnik"
                  "where mbr in (select distinct sef from radnik)";
        return query;
    }
    
    

}




Here's the stacktrace:

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

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:886)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1175)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1296)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3657)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1495)
    at upit2.Upit2_Sefovi.main(Upit2_Sefovi.java:16)

CodePudding user response:

You need a space between radnik and where.

        String query = "select mbr,ime,prz"
                  " from radnik"
                  " where mbr in (select distinct sef from radnik)";
  • Related