tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Darek Czarkowski <dczarkow...@infinitesource.ca>
Subject Re: web application - student need help
Date Fri, 05 Jan 2007 22:02:49 GMT
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@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message