简体   繁体   中英

Tomcat, no data from Result set?

I'm having trouble getting any sense back from my mySQL database. 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

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

It looks like you have a catch block for Exception with an empty body. This will catch and silently ignore any SQL etcetera exceptions. Then you will drop out of the try/catch, and return results ... which will still be zero.


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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