I have a table in mysql which I can view perfectly using JSP, but there is a problem with inserting into the table which I cannot figure out.

Here is my code:

<%@ include file = "database_ini.jsp"%> 

<%
	if(request.getParameter("insert") != null){
		String name = request.getParameter("name");
		
		String sql = "insert into testdata (name) values ('" + name + "')";
		int a = stmt.executeUpdate(sql);
		out.print("Insert success");
	
			
	}
%>
<html>
	<head>
		<title>Insert</title>
	</head>
	<body>
		<form action = "insert11.jsp" method = "post">
			Name : <input type = "text" name = "name" size = "10">
			<br/>
			<input type = "submit" name = "insert" value = "Insert">
		</form>
		<a href="index11.jsp">index</a>
	</body>
</html>

And, here is my error:

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

5: String name = request.getParameter("name");
6:
7: String sql = "insert into testdata (name) values ('" + name + "')";
8: int a = stmt.executeUpdate(sql);
9: out.print("Insert success");
10:
11:

Any help would be greatly appreciated. Thanks

Recommended Answers

All 9 Replies

It might help to know exactly what exception occurred. But, first and foremost, you should not be doing this type of action in a JSP.

And this "an exception occurred" doesn't help.

It might help to know exactly what exception occurred. But, first and foremost, you should not be doing this type of action in a JSP.

And this "an exception occurred" doesn't help.

Hi, how do I make it show the exact exception that occured?

Why should I not be doing this action in a JSP page? What else would I do it in?

Its a PHP assignment that I did, but I must rewrite all the PHP code into JSP.

Thanks,

have you declared "stmt" anywhere?

JSP "should" be used for the interface, and you should leave the data retrieval, entry etc... to java services, but for a school project I really wouldn't worry about it.

but for a school project I really wouldn't worry about it.

I disagree with that one, School projects are supposed to teach you how you should do things the "RIGHT" way, the way things are supposed to be done.

Even if this is a school project I suggest you at least try to follow to some degree the Model View Controller Architecture discussed in this sticky thread.

Hi, how do I make it show the exact exception that occured?

Post the entire stacktrace that's provided in your logs.

Why should I not be doing this action in a JSP page? What else would I do it in?

Because JSP's are only the "view" portion of MVC. You should not be performing "actions" in the JSP that actually belong to either the control or model layers. Think "Beans" and "Servlets". JSP's should do nothing but display pages.

Its a PHP assignment that I did, but I must rewrite all the PHP code into JSP.

Thanks,

So write it properly. See the link above.

Every thing looks like perfect here in this code.
I would suggest you to use try catch block while inserting or with any database transaction.
Problem can come any where may be in your database, if not null field can left blank. This query failed easily.
///// stmt=conn.createStatement(); done ??

<%
	if(request.getParameter("insert") != null){
		try{
		String name = request.getParameter("name");
		
		String sql = "insert into testdata (name) values ('" + name + "')";
		int a = stmt.executeUpdate(sql);
		out.print("Insert success");
	    }
		catch(Exception e)
		{
		  e.printStackTrace();  /// all error information will print prompt
		}
			
	}
%>

e.printStackTrace();

check error at C:\tomcat\logs\catalina.out file

This will give more deep information of real error which is thrown by JSP page.

Read error. Is this SQLException or any other and search on google

commented: Another **smart** programmer performing DB operations from inside a JSP -1

///// stmt=conn.createStatement(); done ??

Yep, this is written in my database_ini file.

I put the code in you suggested with the e.printStackTrace();
and then viewed the log it created "catalina.2008-12-30.log"

Contents

30-Dec-2008 22:21:44 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_11\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\HP\Digital Imaging\\bin;C:\Program Files\QuickTime\QTSystem\;in;C:\Program Files\Java\jdk1.6.0_11\bin
30-Dec-2008 22:21:45 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
30-Dec-2008 22:21:45 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 9563 ms
30-Dec-2008 22:21:45 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
30-Dec-2008 22:21:45 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.18
30-Dec-2008 22:21:48 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
30-Dec-2008 22:21:48 org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
30-Dec-2008 22:21:48 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/94 config=null
30-Dec-2008 22:21:48 org.apache.catalina.startup.Catalina start
INFO: Server startup in 3041 ms

I still cant see what the problem is here?
Thanks,

That is nice you been able to locate log, but you misinterpreted requirements. You need to post the section after start up once you attempt to insert something as that is causing trouble no start up

lol yeah, if you have something like tail, monitor the log file, and replicate the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.