Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

Re: web application - student need help

Darek

2007-01-05

Replies:

Michael Ni wrote:
>
> public ResultSet getData(String queryStr) throws Exception
>   {
>
>      try {
>         DBConstants db = new DBConstants();
>         Class.forName(db.getDrivername());
>         Connection conn;
>         conn =
> DriverManager.getConnection("jdbc:microsoft:sqlserver://" +
> db.getHostname() + "","" + db.getUsername() + "","" + db.getPassword()
> + "");
>
>
>         Statement stmt = conn.createStatement();
>         ResultSet rs = stmt.executeQuery(queryStr);
>         return rs;
>      }
>      catch(Exception e) {
>         e.printStackTrace();
>         System.out.println("getData error");
>         throw new Exception();
>      }
>   }
>
Add

|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) {}
  }|

And return an object rather than ResultSet.

Read about connection pooling, here is an example:
http://www.onjava.com/pub/a/onjava/2006/04/19/database-connection-pooling-with-tomcat.html?page=1

--
Darek Czarkowski
www.infinitesource.ca
darekc at infinitesource dot ca


---------------------------------------------------------------------
To start a new topic, e-mail: users@(protected)
To unsubscribe, e-mail: users-unsubscribe@(protected)
For additional commands, e-mail: users-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.