This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to a db with a servlet or bean

 
Gary Guion
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Marcos Maia
Ranch Hand
Posts: 977
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you shoul consider using a connectionpool class and get a connection from that.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic