This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Connecting to a db with a servlet or bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Connecting to a db with a servlet or bean" Watch "Connecting to a db with a servlet or bean" New topic
Author

Connecting to a db with a servlet or bean

Gary Guion
Greenhorn

Joined: Jun 21, 2001
Posts: 23
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

Joined: Jan 06, 2001
Posts: 977
I think you shoul consider using a connectionpool class and get a connection from that.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jun 21, 2001
Posts: 23
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
 
subject: Connecting to a db with a servlet or bean