Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Database (http://www.go4expert.com/forums/database-forum/)
-   -   database in jsp plzz help..!! (http://www.go4expert.com/forums/database-jsp-plzz-help-t20575/)

tahir90 2Jan2010 18:19

database in jsp plzz help..!!
 
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..!!!

techgeek.in 2Jan2010 21:25

Re: database in jsp plzz help..!!
 
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

nimesh 2Jan2010 21:27

Re: database in jsp plzz help..!!
 
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.

nimesh 2Jan2010 21:31

Re: database in jsp plzz help..!!
 
Quote:

Originally Posted by techgeek.in (Post 62604)
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.

techgeek.in 2Jan2010 21:35

Re: database in jsp plzz help..!!
 
Quote:

Originally Posted by nimesh (Post 62607)
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....

nimesh 2Jan2010 21:44

Re: database in jsp plzz help..!!
 
even your point is correct. But I looked on the error desc

alssadi 19Dec2010 21:37

Re: database in jsp plzz help..!!
 
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


All times are GMT +5.5. The time now is 19:24.