Catching jsp/tomcat/java SQL error

I have been trying to get the sample code to work from an old book. I was finally able to get it to run under tomcat 4.1.31 but there is one bug I just cannot figure out how to handle. I am a beginner to Java/JSP/Tomcat and have about 16+ hours invested in this problem so far.

The delFruit.jsp calls methods from FruitConnect.java.

I found that if the fruit chosen for deletion was not involved in a referential integrity link to a FruitMonth file the code works fine. But if it is linked the delFruit.jsp just displays a blank screen and nothing is deleted. It is fine for it not to be deleted but I cannot find anyway to handle it. I tried to add the checkFruitForSale bandaid below but it causes me a blank screen as well.

Any help would be appreciated.

It would be great if I could just see what is happening when FruitConnect is processing code. I get no errors, just a blank screen. I'm going to try using NetBeans and their debugger next.

Here is delFruit:

<%@ page import="java.sql.*" %>

<jsp:include page="adminHeader.html" flush="true" />

<jsp:useBean id="fruitConnect" class="fruit1.FruitConnect" />

<%

String msg ="The following fruits have been deleted:<br> ";

//Build the list of fruits to delete

String delFruit[] = request.getParameterValues("delFruit");

//WE WILL ONLY GET CHECKBOXES THAT WERE CHECKED

String fruitList ="";

if (delFruit !=null){

int len = delFruit.length;

fruitConnect.dbConnect();

for (int i = 0; i < len; i++){

String passfruit = delFruit[i];

if (fruitConnect.checkFruitForSale(passfruit)){}

else{

fruitList = fruitList +"'" + delFruit[i] +"',";

msg = msg + delFruit[i] +"<br>";

out.print(msg);

}

out.print(msg);

}

fruitList = fruitList +"'nonsense'";

if (fruitConnect.deleteFruit(fruitList) < 1){

msg ="There was an error deleting the fruits from the database.";

}

else{

msg ="Deletion was successful";

}

fruitConnect.dbDisconnect();

}

else{

msg ="You must select some fruit to delete.";

}

%>

HERE IS FruitConnect.java

package fruit1;

import java.sql.*;

publicclass FruitConnect{

private Driver drv =null;

private Connection conn =null;

private ResultSet rs =null;

private Statement stmt =null;

public FruitConnect(){}

publicvoid dbConnect()throws Exception{

//drv = (Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

//conn = DriverManager.getConnection("jdbc:odbc:Fruit");

/////////////////////////////

try{

drv = (Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

conn = DriverManager.getConnection("jdbc:odbc:Fruit");

}

catch ( SQLException sqlException)

{

sqlException.printStackTrace();

}

finally

{

try{

}

catch (Exception exception)

{

}

}

///////////////////////////////

}

publicboolean getNextItem()throws Exception{

boolean ret = rs.next();

return(ret);

}

public String getItemNameString(String columnName)throws Exception{

String name = rs.getString(columnName);

return(name);

}

publicfloat getItemNameFloat(String columnName)throws Exception{

float name = rs.getFloat(columnName);

return(name);

}

publicboolean selectFruits()throws Exception{

String query ="SELECT Id, Name FROM Fruit";

stmt = conn.createStatement();

rs = stmt.executeQuery(query);

boolean ret =false;

if (rs !=null)

ret =true;

return(ret);

}

publicboolean selectMonths()throws Exception{

String query ="SELECT Id, Name FROM Month";

stmt = conn.createStatement();

rs = stmt.executeQuery(query);

boolean ret =false;

if (rs !=null)

ret =true;

return(ret);

}

publicboolean selectFruitForSale()throws Exception{

String query ="SELECT F.Name AS Fruit, M.Name AS Month, Price, Weight, (Price * Weight) AS TotalPrice FROM FruitMonth AS FM, Fruit AS F, Month AS M WHERE FM.FruitId = F.Id AND FM.MonthId = M.Id ORDER BY M.Id";

stmt = conn.createStatement();

rs = stmt.executeQuery(query);

boolean ret =false;

if (rs !=null)

ret =true;

return(ret);

}

publicint insertFruit(String name)throws Exception{

String query ="INSERT INTO Fruit (Name) VALUES ('" + name +"')";

stmt = conn.createStatement();

int ret = stmt.executeUpdate(query);

return(ret);

}

publicint insertFruitForSale(String insertValues)throws Exception{

String query ="INSERT INTO FruitMonth (FruitId, MonthId, Price, Weight) VALUES (" + insertValues +")";

stmt = conn.createStatement();

int ret = stmt.executeUpdate(query);

return(ret);

}

publicint deleteFruit(String fruitList)throws Exception{

String query ="DELETE FROM Fruit WHERE Name IN (" + fruitList +")";

stmt = conn.createStatement();

int ret = stmt.executeUpdate(query);

return(ret);

}

publicvoid dbDisconnect()throws Exception{

stmt.close();

conn.close();

}

publicboolean checkFruitForSale(String thisfruit)throws Exception{

String query ="SELECT F.Name AS Fruit, M.Name AS Month, Price, Weight, (Price * Weight) AS TotalPrice FROM FruitMonth AS FM, Fruit AS F, Month AS M WHERE FM.FruitId = F.Id AND FM.MonthId = M.Id AND F.Name IN (" + thisfruit +") ORDER BY M.Id";

try{

stmt = conn.createStatement();

rs = stmt.executeQuery(query);

}

catch ( SQLException sqlException)

{

sqlException.printStackTrace();

boolean ret =false;

if (rs !=null)

ret =true;

return(ret);

}

finally

{

try{

}

catch (Exception exception)

{

}

}

boolean ret =false;

if (rs !=null)

ret =true;

return(ret);

}

public String Check1(String columnName)throws Exception{

String name = rs.getString(columnName);

return(name);

}

}

[12675 byte] By [ncsjavaa] at [2007-10-2 17:11:47]
# 1

First thing to check would be the Tomcat log file. Most likely there is an error message printed there:

Look in [TOMCAT]/logs for any files modified recently. Open it, and you should find some hints as to your error.

>It would be great if I could just see what is happening when

>FruitConnect is processing code.

For that sort of thing, you want to use a logger. Either Log4J or the standard Java1.4 logger would help. Basically configure it to log statements to file as your program runs.

> I get no errors, just a blank screen

That could indicate that the page ran enough to fill up the page buffer, and then failed. When you view source, do you see anything on the page at all?

evnafetsa at 2007-7-13 18:26:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Are you sure youre not getting errors, try a

out.println(sqlException),

in your catch block, if you are getting sqlException's you will see the output

Although you dont seem to be using them , your catch Exception exception lines would catch all errors, so again, you would not see output.

You dont see output for errors you are handling with try catch blocks.

angrycata at 2007-7-13 18:26:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

localhost_examples_log confirmed my conjecture that the update was failing

because of the relational link with:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] The record cannot be deleted or changed because table 'FruitMonth' includes related records.

It also looks like my checkFruitForSale(passfruit) is behaving unexpectedly and returning false for a statement that should always be returning true.

I am trying to figure out how to implant the logging you mentioned so I can confirm the value of the passed in "passfruit"

and see what the SQL is actually returning right now.

View source gives me a blank page.

ncsjavaa at 2007-7-13 18:26:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

The simplest way is via System.out.println(), or System.err.println() which should show up in the Tomcat logs.

For larger applications you would want to use a logging component, so you could specify which parts, and how detailed you wanted the logging to be.

If it is just an example, just use System.out.println("Insert your message here").

evnafetsa at 2007-7-13 18:26:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

angrycat mentioned out.println(sqlException)

The java file didn't like that but I got the idea of using the System.out.println

in the regular code as well as the catch code.

Right now I can see from the Tomcat DOS window that my fruit to check variable is passing correctly to the checkFruitForSale() method.

Now I am trying to figure out what the SQL is returning as a result of this initial query.

Once I get this part working I hope to find and handle the deletion crashes because of the SQL exception.

ncsjavaa at 2007-7-13 18:26:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

It works! It works! Thank you all very much.

Putting the System.out.println()'s in the .java code enabled me to print messages to the Tomcat DOS console so I could see the values of what was going on down there.

I still need to figure out how to catch and handle a SQL exception but for now just avoiding them is good enough for a days work.

If my initial query shows that there is a referential link in Access then I return a value to the JSP so it knows not to attempt a deletion.

Thanks again.

ncsjavaa at 2007-7-13 18:26:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
> angrycat mentioned out.println(sqlException)> > The java file didn't like that but I got the idea of> using the System.out.printlnSorry about that, you use out.println() in a JSP, missed the fact it was a class.....
angrycata at 2007-7-13 18:26:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...