aspose file tools*
The moose likes JDBC and the fly likes java.sql.SQLException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.sql.SQLException " Watch "java.sql.SQLException " New topic
Author

java.sql.SQLException

mikey anderson
Greenhorn

Joined: Mar 11, 2008
Posts: 2
im a n00b or should i say that i havnt programmed in a few years...

The environment:
1) linux fedora core 5
2) Apache Tomcat/5.5.15
3) mysql> select version();
+-----------+
| version() |
+-----------+
| 5.0.18 |
+-----------+
4) jdbc driver: /var/lib/tomcat5/common/lib/mysql-connector-java-5.1.6-bin.jar

Here is the code that I use to check for tomcat --> mysql connectivity (from an ebook):

<%@page contentType="text/html"%>
<html>
<head><title>Update Table Form</title></head>
<body>

<form action="UpdateTableAction.jsp" method="post" name="update">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="" size="40"/></td>
</tr>
<tr>
<td>LinkText</td>
<td><input type="text" name="linktext" value="" size="50"/></td>
</tr>
<tr>
<td>Link</td>
<td><input type="text" name="link" value="" size="50"/></td>
</tr>
<tr><td> Action </td>
<td>
<input type="radio" name="Action" value="update" /> Update
<input type="radio" name="Action" value="delete" /> Delete
<input type="radio" name="Action" value="new" checked /> New
</td>
</tr>
<tr><td><input type="SUBMIT" value="Submit" /> </td>
</tr>
</table>
</form>
</body>
</html>
Below, code for the actual connection: (when submit is clicked inside UpdateTableAction.jsp a connection is instantiated by UpdateTableAction.jsp)

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

<%
/* Step 1) Get the data from the form */
String ls_name = request.getParameter("name");
String ls_link = request.getParameter("link");
String ls_linktext= request.getParameter("linktext");
String ls_action = request.getParameter("Action");

/* Step 2) Initialize Variables */
String ls_result = "Database was updated";
String ls_query = "";
String ls_dburl = "jdbc:mysql://localhost:3306/xmlbook";
String ls_dbdriver = "org.gjt.mm.mysql.Driver";

/* Step 3) Create a query */
if (ls_action.equals("new"))
{ ls_query = " insert into BannerAds (name,link,linktext)";
ls_query += " values (";
ls_query += "'" + ls_name + "',";
ls_query += "'" + ls_link + "',";
ls_query += "'" + ls_linktext + "')";
}

if (ls_action.equals("delete"))
{
ls_query = " delete from BannerAds where name = ";
ls_query += "'" + ls_name + "'";
}

if (ls_action.equals("update"))
{ ls_query = " update BannerAds";
ls_query += " set link= " + "'" + ls_link + "',";
ls_query += " set linktext= " + "'" + ls_linktext + "'";
ls_query += " where name = " + "'" + ls_name + "'";
}

/* Step 4) Make a database connection */
Connection l_dbconn = null;

try
{
Class.forName(ls_dbdriver);
/* getConnection(URL,User,Pw) */
l_dbconn = DriverManager.getConnection(ls_dburl,"thomas","secret") ;

/*create a SQL Statement */
Statement l_statement = l_dbconn.createStatement();

l_statement.execute(ls_query);
}
catch (ClassNotFoundException e)
{ ls_result = " Error creating database drive class!";
ls_result += " <br/>" + e.toString();
}
catch (SQLException e)
{ ls_result = " Error processing the SQL!...";
ls_result += " <br/>" + e.toString();
// ls_result += " <br/>" + e.toString() + "<br/>" + e.printStackTrace();
}
finally
{
/* We must close the database connection now */
try
{ if (l_dbconn != null)
{ l_dbconn.close(); }
}
catch (SQLException e)
{ ls_result = "Error in closing connection.";
ls_result += " <br/>" + e.toString();
}
}
%>

<html>
<head><title>Updating a Database</title></head>
<body>

The following query was submitted:
<br/><br/>
<%=ls_query%>
<br/><br/>

The Result was:
<br/><br/>
<%=ls_result%>
<br/><br/>

<a href="UpdateTableForm.jsp">Enter another value</a>

</body>
</html>

Here is the output from // from the bare bones exception:

The following query was submitted:

insert into BannerAds (name,link,linktext) values ('site1`','www.jspinsider.com','JSP site')

The Result was:

Error processing the SQL!...
java.sql.SQLException: Unexpected exception encountered during query.

Enter another value

Here is the output from // from the detailed exception:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 3 in the jsp file: /chapter1/UpdateTableAction.jsp
Generated servlet error:
The operator + is undefined for the argument type(s) String, void

org.apache.jasper.servlet.JspServletWrapper.handleJspException (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.servlet.JspServletWrapper.service (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.servlet.JspServlet.serviceJspFile (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.servlet.JspServlet.service (jasper5-compiler-5.5.15.jar.so)
javax.servlet.http.HttpServlet.service (tomcat5-servlet-2.4-api-5.5.15.jar.so)

root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 3 in the jsp file: /chapter1/UpdateTableAction.jsp
Generated servlet error:
The operator + is undefined for the argument type(s) String, void


org.apache.jasper.compiler.DefaultErrorHandler.javacError (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.compiler.ErrorDispatcher.javacError (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.compiler.JDTCompiler.generateClass (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.compiler.Compiler.compile (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.compiler.Compiler.compile (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.compiler.Compiler.compile (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.JspCompilationContext.compile (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.servlet.JspServletWrapper.service (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.servlet.JspServlet.serviceJspFile (jasper5-compiler-5.5.15.jar.so)
org.apache.jasper.servlet.JspServlet.service (jasper5-compiler-5.5.15.jar.so)
javax.servlet.http.HttpServlet.service (tomcat5-servlet-2.4-api-5.5.15.jar.so)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.

Line number "3" is at "<%" in the jsp --> "UpdateTableAction.jsp"

Below are the first three lines in "UpdateTableAction.jsp":

1) <%@page import = "java.sql.*" %>
2)
3) <%


So where am i going wrong? javascript: x()
Btw... here is the connect string: l_dbconn = DriverManager.getConnection(ls_dburl,"thomas","secret")
where String ls_dburl = "jdbc:mysql://localhost/xmlbook";
I have also created user thomas w pw on mysql... with @localhost and @localhost.localdomain etc...
I have also used: l_dbconn = DriverManager.getConnection(ls_dburl,"root","")
[ March 11, 2008: Message edited by: mikey anderson ]

this area is blank by design...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

Mikey,
Welcome to JavaRanch!

Note that it is considered good practice to keep Java code, especially database code, out of the JSP. Instead it should go in a Java class or servlet. This makes the code easier to read, maintain, debug, troubleshoot, etc.

The first step is to figure out which line is throwing the Exception. The error message says:
An error occurred at line: 5 in the jsp file: /chapter1/JDBCcheck.jsp
Generated servlet error:
The operator + is undefined for the argument type(s) String, void

Can you post line the first 5 lines of that JSP and highlight line 5?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
mikey anderson
Greenhorn

Joined: Mar 11, 2008
Posts: 2
Hi Jeanne,

"it is considered good practice to keep Java code, especially database code, out of the JSP. Instead it should go in a Java class or servlet."
Normally I would do everything via a "stored procedure" and post the "rs". But here I just wanted to check to see if I could do it the hard way so to speak..

Anyway...
I messed up the output in the original post (I have since edited it. I used a different jsp to check for connection than the one i was talking about in the "original" post) I have since rerun the original jsp and the same error occurs except on line #3 instead of line #5.

Line number "3" is at "<%" in the jsp --> "UpdateTableAction.jsp"

Below are the first three lines in "UpdateTableAction.jsp":

1) <%@page import = "java.sql.*" %>
2)
3) <%

Also certain "environment variables" like "CLASSPATH" and "CATALINA_HOME" etc. are conspicuously absent when queried from the command terminal (i.e. when issuing the command "printenv") Yet during boot time when the "tomcat" service is started they are momentarily populated.., go figure.. (I have looked into catalina.policy and catalina.properties and perhaps the JVM populates the env vars... at boot time.


My suspicion is that there are some config files that need to be tweaked: like web.xml, server.xml and context.xml as well as catalina.* perhaps their default cofigs are not allowing jsp processing to proceed unimpeded. For whatever reason i am at a standstill...

mikee.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

Mikey,
Java code calling a stored procedure is still JDBC code and better not in a stored procedure.

You are still getting a compiler error which means it isn't even getting to run your code (rendering the config files a moot point for now.) My recommendation is to debug by seeing how much of the file you can remove and still get the error in the same place. This will help narrow down the error greatly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.sql.SQLException
 
Similar Threads
Cannot retrieve mapping for action /content/AddLinks
have problem with multiple forms on a jsp
Problem in inserting data into access database through jsp file
Problem in inserting data into access database through jsp file
two dropdown menus