• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connecting to a db with a servlet or bean

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the following code the most effective way to connect to a db or would a java bean be better?
import java.sql.*;
import javax.servlet.http.*;
public class ConnectionBean implements HttpSessionBindingListener {
private Connection connection;
private Statement statement;
private static final String driver="org.gjt.mm.mysql.Driver";
private static final String dbURL="jdbc:mysql://bang.zeroslope.com/project";
private static final String login="coursepoller";
private static final String password="course";
public ConnectionBean() {
try {
Class.forName(driver);
connection = DriverManager.getConnection(dbURL,login,password);
statement = connection.createStatement();
} catch (ClassNotFoundException e) {
System.err.println("ConnectionBean: driver unavailable: "+e);
connection = null;
} catch (SQLException e) {
System.err.println("ConnectionBean: driver not loaded: "+e);
connection = null;
}
}
public Connection getConnection() {
return connection;
}
public ResultSet executeQuery(String sql) throws SQLException {
return statement.executeQuery(sql);
}
public int executeUpdate(String sql) throws SQLException {
return statement.executeUpdate(sql);
}
public void valueBound(HttpSessionBindingEvent event) {
System.err.println("ConnectionBean: in the valueBound method");
try {
if (connection == null | | connection.isClosed()) {
connection = DriverManager.getConnection(dbURL,login,password);
statement = connection.createStatement();
}
} catch (SQLException e) { connection = null; }
}
public void valueUnbound(HttpSessionBindingEvent event) {
try {
connection.close();
} catch (SQLException e) { }
finally {
connection = null;
}
}
protected void finalize() {
try {
connection.close();
} catch (SQLException e) { }
}
public static void main(String[] args) {
ConnectionBean connectionBean1 = new ConnectionBean();
}
}
 
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you shoul consider using a connectionpool class and get a connection from that.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can say that again. I shudder to think what would happen to a high-volume site if every user session owned its very own database connection. If you're up to it, retrieve a javax.sql.DataSource from the JNDI context. The connections you get from the DataSource should be pooled. You will have to set up the database connection in your application server, and include a resource reference in your WEB-INF/web.xml file.
- Peter
 
Gary Guion
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, this gives me a better idea, but what I still wonder is should I write a bean as opposed to the class/servlet above. Would a bean be more efficient?
Thanks,
Gary
reply
    Bookmark Topic Watch Topic
  • New Topic