Go Back   SitePoint Forums > Forum Index > Program Your Site > Java and JSP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 7, 2009, 17:08   #1
dMans90
SitePoint Member
 
Join Date: Nov 2009
Posts: 9
Problem with JSP File

Hello,

I am having a problem with the code posted below. When I try to run the jsp file in firefox I get this error:

org.apache.jasper.JasperException: An exception occurred processing JSP page /JSP9.jsp at line 29

26:
27: String name=request.getParameter("name");
28: //stmt.executeUpdate("insert into Rot(name) value('"+name+"')");
29: rst=stmt.executeQuery("select name from Rot");
30:
31:
32: %>


Any help would be appreciated. Rot is the name of a test database on my system created by MySQL. I am also running Tomcat on my computer.








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


<%
String driver = "org.gjt.mm.mysql.Driver";
Class.forName(driver).newInstance();


Connection con=null;
ResultSet rst=null;

Statement stmt=null;

try{
String url="jdbc:mysql://localhost/David?user=deepak&password=deepak";


con=DriverManager.getConnection(url);
stmt=con.createStatement();
}catch(Exception e){
System.out.println(e.getMessage());
}

if(request.getParameter("action") == null){


String name=request.getParameter("name");
//stmt.executeUpdate("insert into Rot(name) value('"+name+"')");
rst=stmt.executeQuery("select name from Rot");


%>



<html>
<body>
<center><h2>Books List</h2>
<table border="1" cellspacing="0" cellpadding="0">

<tr><td><b>Id</b></td><td><b>Name<b></td></tr>
<%
int no=1;

while(rst.next()){

%>

<tr><td><%=no%></td>
<td><%=rst.getString("name")%></td></tr>

<%
no++;
}

rst.close();
stmt.close();
con.close();



%>

</table>
</center>
</body>
</html>
<%}else{%>

return 1;


<%}%>
dMans90 is offline   Reply With Quote
Old Nov 10, 2009, 05:00   #2
zink
SitePoint Enthusiast
 
zink's Avatar
 
Join Date: Jul 2006
Location: UK
Posts: 73
Move your catch block so you get the exception displayed in the browser... This will help you see what the problem is...

I commented this line out too for now... "if(request.getParameter("action") == null){"

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


<%
String driver = "org.gjt.mm.mysql.Driver";
Class.forName(driver).newInstance();


Connection con=null;
ResultSet rst=null;

Statement stmt=null;

try{
String url="jdbc:mysql://localhost/David?user=deepak&password=deepak";


con=DriverManager.getConnection(url);
stmt=con.createStatement();
}

//if(request.getParameter("action") == null){


String name=request.getParameter("name");
//stmt.executeUpdate("insert into Rot(name) value('"+name+"')");
rst=stmt.executeQuery("select name from Rot");

catch(Exception e){
System.out.println(e.getMessage());
}

%>
zink is offline   Reply With Quote
Old Nov 16, 2009, 18:31   #3
dMans90
SitePoint Member
 
Join Date: Nov 2009
Posts: 9
Thanks for the help zink but I still can't seem to get this one. I will post the changes to the code I have made

%@ page language="java" import="java.sql.*"%>
<%@page contentType="text/html" %>


<%
String driver = "org.gjt.mm.mysql.Driver";
Class.forName(driver).newInstance();


Connection con=null;
ResultSet rst=null;

Statement stmt=null;

try{
String url="jdbc:mysql://localhost/David?user=deepak&password=deepak";


con=DriverManager.getConnection(url);
stmt=con.createStatement();


String name=request.getParameter("name");
stmt.executeUpdate("insert into Rot(name) values('"+name+"')");
rst=stmt.executeQuery("select name from Rot");



}
catch(Exception e){
System.out.println(e.getMessage());
}
%>

<html>
<body>
<%while(rst.next()){%>
<p><%=rst.getString("name")%></p>
<%}%>
</body>
</html>
dMans90 is offline   Reply With Quote
Old Nov 17, 2009, 02:11   #4
rozner
SitePoint Wizard
 
rozner's Avatar
 
Join Date: Oct 2002
Location: Paris
Posts: 1,029
Code:
}
catch(Exception e){
System.out.println(e.getMessage());
}
This will print the error message to the console. You're better off to do "e.printStackTrace()" which will print the stack trace to the console. But you can also print the error to the browser by "out.println(e.getMessage())". If you want the stack trace in the browser you need to do this:
Code:
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
out.print(sw.getBuffer().toString());
Very ugly looking though and exactly why you shouldn't be accessing a database from a JSP in the first place. Look into using a servlet. But the above should at least give you some information about the error.
rozner is offline   Reply With Quote
Old Nov 28, 2009, 12:26   #5
ge0man
SitePoint Member
 
Join Date: Oct 2009
Posts: 20
You should be SELECTING from a table not a database.

but yeh, you should def be throwing a runtime exception to see the actual exception.
ge0man is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 18:44.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved