简体   繁体   English

Tomcat,结果集中没有数据?

[英]Tomcat, no data from Result set?

I'm having trouble getting any sense back from my mySQL database. 我无法从mySQL数据库获得任何信息。 I'm passing it a valid query that should return a single row. 我传递给它一个有效的查询,该查询应返回一行。 However it only ever returns me 0, the intial value of int results 但是它只返回0,即int results的初始值

public String getResultSet(String query) throws SQLException{
            ResultSet rs = null;
            int results=0;
            try {
                con = ds.getConnection();
                Statement stmt = (Statement) con.createStatement();
                rs = stmt.executeQuery(query);

                    while(rs.next()){                   
                        results = rs.getInt("location_id");             
                    }           

                } catch (Exception ex ){               
            } finally {
                if (con != null) con.close();
            }           
            return Integer.toString(results);           
        }

No errors are being thrown, could some advise what I'm doing wrong? 没有抛出任何错误,有人可以建议我在做什么错吗?

TIA TIA

It looks like you have a catch block for Exception with an empty body. 看起来您有一个带有空主体的Exception捕获块。 This will catch and silently ignore any SQL etcetera exceptions. 这将捕获并无提示地忽略任何SQL etcetera异常。 Then you will drop out of the try/catch, and return results ... which will still be zero. 然后,您将退出try / catch,并返回results ...仍为零。


What you are doing is "squashing" the exception; 您正在做的是“挤压”异常; ie catching it and throwing it away without doing any recovery or error reporting. 即捕获它并丢弃它,而无需进行任何恢复或错误报告。 This is bad practice . 这是不好的做法 Worse still, you are doing this for Exception . 更糟糕的是,您正在为Exception执行此操作。 That means you are doing it not only for the checked exceptions that you (possibly) expect to occur, but also for a whole range of unchecked exceptions that you cannot anticipate. 这意味着您不仅要对(可能)希望发生的检查异常进行检查,而且还要对无法预料的整个非检查异常进行检查。

No exceptions are being thrown because you are catching them all without printing them. 没有异常被抛出,因为您在不打印它们的情况下捕获了所有异常。 If there are any, you'd never see them because your catch block is empty. 如果有的话,您将永远不会看到它们,因为catch块为空。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM