database in jsp plzz help..!!

Discussion in 'Database' started by tahir90, Jan 2, 2010.

  1. tahir90

    tahir90 New Member

    Joined:
    Dec 20, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://hidden-science.blogspot.com/ and http://www.atfreeforum.com/discussions/
    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.handleJspException(JspServletWrapper.java:505)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.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(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.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 a moderator: Jan 3, 2010
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    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
     
  3. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    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.
     
  4. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    If the problem was this, the error should not be on line 43.
     
  5. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    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....
     
  6. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    even your point is correct. But I looked on the error desc
     
  7. alssadi

    alssadi Banned

    Joined:
    Dec 11, 2010
    Messages:
    41
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Creative director & web developer
    Location:
    Dubai
    Home Page:
    http://uaeinfographics.blogspot.com/
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice