jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Problems with ManagedConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problems with ManagedConnection " Watch "Problems with ManagedConnection " New topic

Problems with ManagedConnection

Rafael Marquez

Joined: Dec 02, 2005
Posts: 1
Hi there:
I'm a novice in Java and my application keeps crashing with the same error. It looke I'm not closing something, and I just can't find the fix.
This is a sample of the code. Please, please someone tell me what is wrong with it:

<%@ page import = "javax.naming.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.lang.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "javax.sql.*"%>

String sMode = "";
String sSiteID = "";
String sAreaID = "";
String sAreaName = "";
String sJSP_Action= "";

if (request.getParameter("MODE") != null) {
sMode= request.getParameter("MODE").toString();
}else {
sMode= "0";

if (request.getParameter("fld_SiteID") != null) {
sSiteID= request.getParameter("fld_SiteID").toString();
}else {
sSiteID= "0";

if (request.getParameter("fld_AreaID") != null) {
sAreaID= request.getParameter("fld_AreaID").toString();
}else {
sAreaID= "0";

if (request.getParameter("txt_AreaName") != null) {
sAreaName= request.getParameter("txt_AreaName").toString();
}else {
sAreaName= "";

if (request.getParameter("JSP_ACTION") != null) {
sJSP_Action= request.getParameter("JSP_ACTION").toString();
}else {
sJSP_Action= "";

InitialContext ctxt = new InitialContext();
DataSource ds = (DataSource) ctxt.lookup("java EMO_CARACAS");
Connection con = ds.getConnection();
Connection con0 = ds.getConnection();
Connection con1 = ds.getConnection();
Statement stmt = null;
ResultSet rs = null;
String sSQL = "";
int rows= 0;

try {

if (sJSP_Action.equals("Aceptar") || sJSP_Action.equals("Click_Accept")){
if (sMode.equals("ADDAREA")){
sSQL = "INSERT INTO tbl_Areas (SiteID, AreaName)";
sSQL = sSQL + " VALUES(" + sSiteID + ",'" + sAreaName + "')";
sSQL = "UPDATE tbl_Areas ";
sSQL = sSQL + " SET AreaName='"+ sAreaName+ "'";
sSQL = sSQL + " WHERE ID="+ sAreaID;
stmt= con.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();

}else if (sJSP_Action.equals("Borrar") || sJSP_Action.equals("Click_Delete")){
sSQL = "DELETE FROM tbl_Areas WHERE ID=" + sAreaID;
stmt= con.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();

sSQL = "DELETE FROM tbl_Rooms WHERE AreaID=" + sAreaID;
stmt= con0.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con0!=null) con0.close();

sSQL = "DELETE FROM tbl_Elements WHERE AreaID=" + sAreaID;
stmt= con1.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con1!=null) con1.close();

} finally {
response.sendRedirect("survey_areas_list.jsp?SiteID=" + sSiteID);

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

Welcome to JavaRanch! What is the error it is crashing with?

One thing that jumps out at me is that you are using three connections instead of 1. Try simplifying the code to reuse the same connection within the page.

Also, see if you can simplify the page (like doing one query, then two queries.) This will help us all further narrow down the problem.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Just to add...

Yes the actual error message would be good.

There are number of strange elements in your code. Like the ResultSet rs which is NEVER used as far as I can see. You do a lot of if(rs!=null) but I can't see any reason for it not to be null.

I also find it odd that you use three seperate connections but reuse the statement. I don't think any of this will cause an error but it sure is odd.

I also notice you seem to have try with no catch but a finally. It might be nice if you caught the exception and did something... printing it out would be good for starters but in future you could take other appropriate action.
I agree. Here's the link: http://aspose.com/file-tools
subject: Problems with ManagedConnection
It's not a secret anymore!