Re: [OT] a Collection of beans to store sql data
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Bob, Bob Hall wrote: You missed the essence of Chris's suggestion: 3. Throw an exception in your catch(SQLException) block. In other words, your catch block would swallow the SQLException should one occur. Just to be clear, I usually don't advocate the swallowing of an exception. I typically encourage the use of a wrapped exception, like this: catch (SQLException sqle) { throw new ApplicationSpecficException(Cannot query db, sqle); } Where the original exception is kept around, and even shows up in the stack trace as the root cause. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFpk7w9CaO5/Lv0PARAkBoAKCMNUGNE9bUc6kQ5q3mJvUfTt58KQCfXF1N LoAz/ziIrMX3iWIILCB1v5w= =0itK -END PGP SIGNATURE- - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [OT] a Collection of beans to store sql data
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Michael, Michael Ni wrote: My problem is i get PersonNew.java:48: missing return statement } [snip] public Collection getPersondata( String alias, String password ) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; Collection retValue = new ArrayList(); String query = SELECT * FROM person WHERE alias = ?, password = ?; try { conn = DBConnection.getDBConnection(); stmt = conn.prepareStatement( query ); stmt.setString( 1, alias ); stmt.setString( 2, password ); rs = stmt.executeQuery(); while (rs.next()) { PersonalInfo beanrow = new PersonalInfo(); beanrow.setAlias(rs.getString(alias)); beanrow.setPassword(rs.getString(password)); retValue.add(beanrow); } return retValue; } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try {if (rs != null) rs.close();} catch (SQLException e) {} try {if (stmt != null) stmt.close();} catch (SQLException e) {} try {if (conn != null) conn.close();} catch (SQLException e) {} } } The problem is that you have a code path that can exit your method without returning a value (which is a no-no). If a SQLException is thrown inside your try block, it will be caught, logged, and then the method exists with no return value. You have several options: 1. Put a catch-all return at the very end of the method (after the finally block) 2. Put a return in your catch(SQLException) block. 3. Throw an exception in your catch(SQLException) block. I tend to favor #3 since a SQLException usually indicates a real problem rather than something that is recoverable, but this may not be true under your particular circumstances. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFpZ3V9CaO5/Lv0PARAuNvAKC4+g9iHyn6U3m88e+hgBJfQ87WjgCeJAV9 sDq1+7kNLRWpyZrZE1roQ14= =/ZcX -END PGP SIGNATURE- - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [OT] a Collection of beans to store sql data
thanks for the quick reply, by the way everyone is telling me to make my functions return objects instead of resultset. why is returning resultset bad? From: Christopher Schultz [EMAIL PROTECTED] Reply-To: Tomcat Users List users@tomcat.apache.org To: Tomcat Users List users@tomcat.apache.org Subject: Re: [OT] a Collection of beans to store sql data Date: Wed, 10 Jan 2007 21:15:50 -0500 -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Michael, Michael Ni wrote: My problem is i get PersonNew.java:48: missing return statement } [snip] public Collection getPersondata( String alias, String password ) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; Collection retValue = new ArrayList(); String query = SELECT * FROM person WHERE alias = ?, password = ?; try { conn = DBConnection.getDBConnection(); stmt = conn.prepareStatement( query ); stmt.setString( 1, alias ); stmt.setString( 2, password ); rs = stmt.executeQuery(); while (rs.next()) { PersonalInfo beanrow = new PersonalInfo(); beanrow.setAlias(rs.getString(alias)); beanrow.setPassword(rs.getString(password)); retValue.add(beanrow); } return retValue; } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try {if (rs != null) rs.close();} catch (SQLException e) {} try {if (stmt != null) stmt.close();} catch (SQLException e) {} try {if (conn != null) conn.close();} catch (SQLException e) {} } } The problem is that you have a code path that can exit your method without returning a value (which is a no-no). If a SQLException is thrown inside your try block, it will be caught, logged, and then the method exists with no return value. You have several options: 1. Put a catch-all return at the very end of the method (after the finally block) 2. Put a return in your catch(SQLException) block. 3. Throw an exception in your catch(SQLException) block. I tend to favor #3 since a SQLException usually indicates a real problem rather than something that is recoverable, but this may not be true under your particular circumstances. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFpZ3V9CaO5/Lv0PARAuNvAKC4+g9iHyn6U3m88e+hgBJfQ87WjgCeJAV9 sDq1+7kNLRWpyZrZE1roQ14= =/ZcX -END PGP SIGNATURE- - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] _ The MSN Entertainment Guide to Golden Globes is here. Get all the scoop. http://tv.msn.com/tv/globes2007/?icid=nctagline2 - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [OT] a Collection of beans to store sql data
On 1/10/07, Michael Ni [EMAIL PROTECTED] wrote: thanks for the quick reply, by the way everyone is telling me to make my functions return objects instead of resultset. why is returning resultset bad? Because of this line: try {if (rs != null) rs.close();} catch (SQLException e) {} Your function is closing the result set. If you return a closed ResultSet, what good is it? You must close the connection when you're finished with it (so that connection pooling will work). Before you close the connection, you must close the ResultSet. Before you close the ResultSet, you must get the data out of it. -- Len
Re: [OT] a Collection of beans to store sql data
hmmm there is a sqlexception in my catch block, unless i'm doing it wrong. mike From: Christopher Schultz [EMAIL PROTECTED] Reply-To: Tomcat Users List users@tomcat.apache.org To: Tomcat Users List users@tomcat.apache.org Subject: Re: [OT] a Collection of beans to store sql data Date: Wed, 10 Jan 2007 21:15:50 -0500 -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Michael, Michael Ni wrote: My problem is i get PersonNew.java:48: missing return statement } [snip] public Collection getPersondata( String alias, String password ) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; Collection retValue = new ArrayList(); String query = SELECT * FROM person WHERE alias = ?, password = ?; try { conn = DBConnection.getDBConnection(); stmt = conn.prepareStatement( query ); stmt.setString( 1, alias ); stmt.setString( 2, password ); rs = stmt.executeQuery(); while (rs.next()) { PersonalInfo beanrow = new PersonalInfo(); beanrow.setAlias(rs.getString(alias)); beanrow.setPassword(rs.getString(password)); retValue.add(beanrow); } return retValue; } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try {if (rs != null) rs.close();} catch (SQLException e) {} try {if (stmt != null) stmt.close();} catch (SQLException e) {} try {if (conn != null) conn.close();} catch (SQLException e) {} } } The problem is that you have a code path that can exit your method without returning a value (which is a no-no). If a SQLException is thrown inside your try block, it will be caught, logged, and then the method exists with no return value. You have several options: 1. Put a catch-all return at the very end of the method (after the finally block) 2. Put a return in your catch(SQLException) block. 3. Throw an exception in your catch(SQLException) block. I tend to favor #3 since a SQLException usually indicates a real problem rather than something that is recoverable, but this may not be true under your particular circumstances. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFpZ3V9CaO5/Lv0PARAuNvAKC4+g9iHyn6U3m88e+hgBJfQ87WjgCeJAV9 sDq1+7kNLRWpyZrZE1roQ14= =/ZcX -END PGP SIGNATURE- - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] _ Find sales, coupons, and free shipping, all in one place! MSN Shopping Sales Deals http://shopping.msn.com/content/shp/?ctid=198,ptnrid=176,ptnrdata=200639 - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]