You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2007/01/11 03:15:50 UTC

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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] a Collection of beans to store sql data

Posted by Len Popp <le...@gmail.com>.
On 1/10/07, Michael Ni <mi...@hotmail.com> 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

Posted by Michael Ni <mi...@hotmail.com>.
hmmm there is a sqlexception in my catch block, unless i'm doing it wrong.

mike


>From: Christopher Schultz <ch...@christopherschultz.net>
>Reply-To: "Tomcat Users List" <us...@tomcat.apache.org>
>To: Tomcat Users List <us...@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: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>

_________________________________________________________________
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] a Collection of beans to store sql data

Posted by Michael Ni <mi...@hotmail.com>.
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 <ch...@christopherschultz.net>
>Reply-To: "Tomcat Users List" <us...@tomcat.apache.org>
>To: Tomcat Users List <us...@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: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>

_________________________________________________________________
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org