database in jsp plzz help..!!

tahir90's Avatar, Join Date: Dec 2009
Light Poster
Iam new to jsp and trying to make connection with database in jsp iam using the following code :

Code:
<%

String firstName = request.getParameter("txtName");
String lastName = request.getParameter("txtLastName");
String gender = request.getParameter("listGender");
String address = request.getParameter("txtAdd");
String country = request.getParameter("txtCountry");
String contact = request.getParameter("txtContact");
String email = request.getParameter("txtEmailID");
String userName = request.getParameter("txtUname");
String password = request.getParameter("txtPass");

Connection con =null;

try {

	//Driver Loading
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Or any other driver

	
	//Defie the connection URL
	String url = "jdbc:odbc:cmobs";


	//Establish the connection
	con = DriverManager.getConnection(url);

	//create the statement
	Statement st = con.createStatement();
	
	//Step 6: Execute the query
	String sql = "insert into Info(FIRSTNAME,LASTNAME,GENDER,DOB,COUNTRY,ADDRESS,,CONTACT,EMAIL,USERNAME,PASSWORD) VALUES('"+firstName+"','"+lastName+"','"+gender+"','"+country+"','"+address+"','"+email+"','"+userName+"','"+password+"')";
	
	st.executeUpdate(sql);
}

catch(Exception ex)
{
	//out.println("Unable to load the driver class!"+ ex);
	con.close();
}



%>
but having error :

type Exception report

message

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

exception

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

40: catch(Exception ex)
41: {
42: //out.println("Unable to load the driver class!"+ ex);
43: con.close();
44: }
45:
46:


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

root cause

java.lang.NullPointerException
org.apache.jsp.DB_jsp._jspService(DB_jsp.java:96)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

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

I will be thankful to all of you if you help me and edit my codeto perfection plzz help..!!!

Last edited by shabbir; 3Jan2010 at 10:16.. Reason: Code blocks
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
yah i got ur problem...in jsp u have to use "PreparedStatement" alwayz instead of only "statement"..i m sending u a sample program..try to understand this...
http://rapidshare.com/files/329297343/jsp.rar.html

one more thing..before understanding the above code make sure that u know about the "Statement", "PreparedStatement" i.e u shud have complete knowledge about JDBC
0
nimesh's Avatar, Join Date: Apr 2009
Invasive contributor
this thread should appear in jsp forum.

One cause that I think might be that the code is unable to make the connection to the database and going into catch block, and you are giving the command to close the connection.

Do this:
make the if condition before closing the database connection, that only if the connection is open close the con.
0
nimesh's Avatar, Join Date: Apr 2009
Invasive contributor
Quote:
Originally Posted by techgeek.in View Post
yah i got ur problem...in jsp u have to use "PreparedStatement" alwayz instead of only "statement"..i m sending u a sample program..try to understand this...
http://rapidshare.com/files/329297343/jsp.rar.html

one more thing..before understanding the above code make sure that u know about the "Statement", "PreparedStatement" i.e u shud have complete knowledge about JDBC
If the problem was this, the error should not be on line 43.
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
Quote:
Originally Posted by nimesh View Post
If the problem was this, the error should not be on line 43.
actually i din go with the error i simply had gone with the concept...as far as i know only "PreparedStatement" is used in case of JSP... "Statement" wont work for insert,update and delete...yah u can use only "Statement" for select statement....
0
nimesh's Avatar, Join Date: Apr 2009
Invasive contributor
even your point is correct. But I looked on the error desc
0
alssadi's Avatar
Banned
did u set the write environment , cuz its realy important , there are things to get the connection right from what ever database you use , u need to set the nessasary path in you system inevironment , and u may need a JDBC files to get you connection done , + you need the drivers for JDBC to connect .

i advice you to use JDeveloper for orcel it helps , oh i forgot to say u need to create a class , and one more thing u need to set the write libraries like J2EE , JSP Runtime , and SQLDriver for connections

hope this helps

regards alssadi