简体   繁体   English

查询在SQLDeveloper中有效,但在JDBC中无效

[英]Query works in SQLDeveloper but not in JDBC

I have the following query: 我有以下查询:

SELECT MAX(ID) FROM INGRESO_SALIDA_BUQUES WHERE ID_BUQUE = 1;

which works when I execute it in SQLDeveloper. 当我在SQLDeveloper中执行它时,它起作用。 It returns one row and just one column: the biggest ID where ID_BUQUE = 1. 它返回一行,只有一列:最大的ID,其中ID_BUQUE = 1。

Now, I'm using jdbc to connect to the database and do the same operation. 现在,我正在使用jdbc连接到数据库并执行相同的操作。 Here is the code: 这是代码:

String sql = "SELECT MAX(ID) AS MAXIMO FROM INGRESO_SALIDA_BUQUES ";
    sql += "WHERE ID_BUQUE = " + registroSalida.getIdBuque();

PreparedStatement prepStmt = conn.prepareStatement(sql);
    recursos.add(prepStmt);
    ResultSet rs = prepStmt.executeQuery();

The las line of code is the one that throws the error. las的代码行是引发错误的代码。

The result from the eclipse console (For your reference "Nombre de columna no válido" means invalid column name): eclipse控制台的结果(供您参考的“ Nombre de columna noválido”表示无效的列名):

18:40:27,712 ERROR [stderr] (default task-2) SQLException:Nombre de columna no válido
18:40:27,712 ERROR [stderr] (default task-2) java.sql.SQLException: Nombre de columna no válido
18:40:27,713 ERROR [stderr] (default task-2)    at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3724)
18:40:27,713 ERROR [stderr] (default task-2)    at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2799)
18:40:27,713 ERROR [stderr] (default task-2)    at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:498)

I tried deleting all rows except 1, in order to see hat would happen if I change MAX(ID) TO ID . 我尝试删除除1之外的所有行,以查看如果将MAX(ID)更改为ID会发生问题。 This query works. 该查询有效。 I tried again MAX(ID) with just one row and it failed again. 我仅用一行再次尝试了MAX(ID),但再次失败了。

The table has was created with this code: 该表已使用以下代码创建:

CREATE TABLE INGRESO_SALIDA_BUQUES (
    ID NUMBER,
    ID_BUQUE NUMBER NOT NULL,
    FECHA_INGRESO DATE NOT NULL,
    FECHA_SALIDA DATE,
    CONSTRAINT PK_ISB PRIMARY KEY (ID),
    CONSTRAINT FK_ISB_ID_BUQUE FOREIGN KEY (ID_BUQUE) REFERENCES BUQUE(ID));

Do you know how to solve this? 你知道如何解决吗?

recursos is just just an ArrayList: recursos只是一个ArrayList:

private ArrayList<Object> recursos;

And in the constructor I initialize it. 然后在构造函数中将其初始化。 recursos = new ArrayList(); recursos = new ArrayList();

I use it to save the resources necesary to the SQL statement, in order to close them later. 我用它来保存SQL语句所需的资源,以便以后关闭它们。

The method to close them is this: 关闭它们的方法是这样的:

public void cerrarRecursos() {
    for(Object ob : recursos){
        if(ob instanceof PreparedStatement)
            try {
                ((PreparedStatement) ob).close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }
}

The stack trace shows the error is coming from 堆栈跟踪显示错误来自

oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:498)

... so it's being raised from an rs.getString() call, not the prepStmt.executeQuery() you think it's coming from. ...因此它是通过rs.getString()调用而不是您认为它来自的prepStmt.executeQuery()引发的。

Since you said it works if you change you query to get ID instead of MAX(ID) , code you haven't shown is doing rs.getString("ID") - otherwise it would still have failed after that change. 既然您说过,如果您更改查询以获取ID而不是MAX(ID) ,那么它是rs.getString("ID") ,那么您未显示的代码正在执行rs.getString("ID") -否则更改后它仍然会失败。 If you use a column name in the getString then it has to match the column alias from the query. 如果在getString使用列名,则它必须与查询中的列别名匹配。

You can either change your result set handling to do rs.getString("MAXIMO") to use the alias you've shown in your query, or rs.getString(1) to use positional notation; 您可以将结果集处理更改为使用rs.getString("MAXIMO")来使用查询中显示的别名,也可以使用rs.getString(1)来使用位置表示法。 or change the query alias from MAXIMO to ID . 或将查询别名从MAXIMO更改为ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java JDBC-无效字符,将打印查询放入SQLDeveloper中并可以使用 - Java JDBC - invalid character, put printing query and using it in SQLDeveloper works SQL 查询在 SQLDeveloper 中有效,但在 JDBC 中无法运行 - SQL Query works in SQLDeveloper but won't run in JDBC 本机查询可以使用sqlDeveloper,但无法使用休眠模式 - Native query works sqlDeveloper but fails using hibernate 相同的连接字符串在 sqldeveloper 中工作正常,但是从独立的 JDBC 程序抛出 IO 异常 - Same connection string works fine in sqldeveloper, but, throws IO exception from a standalone JDBC program 多次更新仅适用于 jdbc 中的一个查询 - Multiple update only works on one of the query in jdbc 使用 JDBC 的不明确列,但查询在数据库中工作正常 - Ambiguous column using JDBC but query works fine in database 查询在SQL * Plus中有效,但在JDBC中失败,并带有ORA-00911异常 - A query works in SQL*Plus but fails in JDBC with an ORA-00911 exception SQL查询在phpmyadmin中工作,但在使用jdbc和java时不能 - SQL query works in phpmyadmin but not when using jdbc and java JDBC存储映射中的转换错误,但查询在Management Studio中工作正常 - Conversion error in jdbc store mapping but query works fine in Management Studio Rawsql中的关系运算符无效,相同的语句在sqldeveloper中有效 - Invalid relational operator in rawsql, same statement works in sqldeveloper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM