Connecting JSP To Mysql Database Lesson
Wall Script
Follow Me:
Tuesday, September 16, 2008

Connecting JSP To Mysql Database Lesson

My brother Ravi Tamada request one mail about his college presentation. He is planning to do web dynamic project. So i am giving small explanation about JSP (Java Server Pages) to Mysql Connection structure, Tomcat directory structure and simple database examples.

Login.html


Code :
<body>
<form action="login.jsp" method="post">

User name :<input type="text" name="usr" />
password :<input type="password" name="pwd" />
<input type="submit" />

</form>
</body>

Reg.html



code:
<form action="reg.jsp" method="post">

Email :<input type="text" name="email" />
First name :<input type="text" name="fname" />
Last name :<input type="text" name="lname" />
User name :<input type="text" name="userid" />
password :<input type="password" name="pwd" />
<input type="submit" />

</form>

Mysql Create Database Test:
Mysql no doubt about it best open source database http://mysql.com/


Create Table Users:


login.jsp


<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String userid=request.getParameter("user");
session.putValue("userid",userid);
String pwd=request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/
test","root","root");
Statement st= con.createStatement();
ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");
if(rs.next())
{
if(rs.getString(2).equals(pwd))
{
out.println("welcome"+userid);

}
else
{
out.println("Invalid password try again");
}
}
else
%>


reg.jsp


<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String user=request.getParameter("userid");
session.putValue("userid",user);
String pwd=request.getParameter("pwd");
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","root");
Statement st= con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
'"+lname+"','"+email+"')");


%>


welcome.jsp


<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String user=session.getValue("userid").toString();
%>
Registration is Successfull. Welcome to <%=user %>

Tomcat Directory Structure
Tomcat open source web server you can download from this link http://tomcat.apache.org/



Run Your Project
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


82 comments:

  1. wat about the login.html and reg.html for these........sri?

    ReplyDelete
  2. i want to have those yaar............

    plzz post those first sri......

    ReplyDelete
  3. .how to connect these jsp to html pages.........

    ReplyDelete
  4. hie srinivas,........plz explain how jsp and html pages get connected........plz post yaar.....!!

    ReplyDelete
  5. login.html and reg.html Code posted......

    ReplyDelete
  6. Simply stated, JDBC makes it possible to write platform independent Java programs that can be used to manipulate the data in a wide range of SQL databases without the requirement to modify and/or recompile the Java programs when moving from platform to platform or from DBMS to DBMS. MySQL is available for a wide variety of platforms.

    ReplyDelete
  7. very good this post. thanks

    ReplyDelete
  8. srinivas ur post was excellent..can you help me out with this..this user login when submit must go to another page that displays another html form.

    ReplyDelete
  9. @Harini

    login.jsp include redirect .html

    request.getRequestDispatcher("/xyz.html");

    ReplyDelete
  10. Where is Download button :)

    ReplyDelete
  11. Thanks this Connecting JSP To Mysql Database Lesson very useful to me!!!!!!

    ReplyDelete
  12. Hi Srinivas, I refer ur example for my application but i face on problem means getting an error so plz help me.
    The exception is follow:

    org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    plz plz help me

    Regards,
    Mahesh

    ReplyDelete
  13. hi sir,
    how to run this project in glassfish server.
    i got the error as resources not available can u explan it clearly pls

    ReplyDelete
  14. WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Login' did not find a matching property.
    org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-bio-8080"]
    org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
    .apache.catalina.startup.Catalina load
    INFO: Initialization processed in 355 ms
    Mar 13, 2012 11:48:26 AM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Catalina
    Mar 13, 2012 11:48:26 AM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.25
    Mar 13, 2012 11:48:26 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]
    Mar 13, 2012 11:48:26 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["ajp-bio-8009"]
    Mar 13, 2012 11:48:26 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 207 ms
    Mar 13, 2012 11:48:34 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [jsp] in context with path [/Login] threw exception [An exception occurred processing JSP page /login.jsp at line 8

    5: String userid=request.getParameter("user");
    6: session.putValue("userid",userid);
    7: String pwd=request.getParameter("pwd");
    8: Class.forName("com.mysql.jdbc.Driver");
    9: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dba");
    10: Statement st= con.createStatement();
    11: ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");


    Stacktrace:] with root cause
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.apache.jsp.login_jsp._jspService(login_jsp.java:71)
    at org.apache.jasper.runtime.HttpJspBase.servicee)


    I m Facing this error anyone say me solutions...

    ReplyDelete
  15. hi, i highly appreciate the way you explain this connection but can you please explain how i can add date field and checkbox to my database and link it to a jsp page, the same way you explain the login process.

    thanks

    ReplyDelete
  16. Thank you very much!

    ReplyDelete
  17. I have a problem in the code in line of connection
    DriverManager.getConnection("jdbc:mysql://localhost:3306..
    tell me the problem srinivas..

    ReplyDelete
    Replies
    1. my problem also not connected data base sir plz replay

      Delete
  18. To people getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver you should download the jdbc mysql jar and add it to lib folder on your tomcat server.

    Prashant Tyagi should see if his database is on and on port 3306. Mysql default port is 3386..

    Glad to be of assistance. :)

    ReplyDelete
  19. org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 25 in the jsp file: /reg.jsp
    String literal is not properly closed by a double-quote
    22: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3386/test1");
    23: Statement st=con.createStatement();
    24: ResultSet rs;
    25: int i=st.executeUpdate("insert into user values ('"+user+"','"+pwd+"','"+fname+"',
    26: '"+lname+"','"+email+"')");
    27:
    28:

    ReplyDelete
  20. Can you solve my problem? after clicking submit button I get this:
    " SEVERE: PWC6117: File "C:\...\build\web\login.jsp" not found "

    login.jsp is in the same folder with login.html

    ReplyDelete
  21. Create a Table in Mysql database through SQL Query in JSP,this Connecting JSP To Mysql Database Lesson very useful, I need a program to provide download option for pdf file on my web page,the pdf file contains retrieved data from mysql table. I need this program by using jsp-servlets. any one can help me please

    ReplyDelete
  22. nice code thanks

    ReplyDelete
  23. wjhen i click on the submit button it goes to the login.jsp but it displays a blank page.

    and yes i have used setAttribute instead of getValue

    ReplyDelete
  24. DUDE

    I DID A MISTAKE IN CODING

    In Login.html
    name="usr"
    login.jsp
    request.getParameter("user")

    usr & user?

    ReplyDelete
  25. nice presentation.......

    ReplyDelete
  26. Hey srinivas ,
    how to assign a session variable to a normal variable,n print ??

    And String user=session.getValue("userid").toString(); tis code not wrkin....

    ReplyDelete
  27. thanks a lot very nicely explained

    ReplyDelete
  28. hi..Nice explanation.but how can i use both insert and retrive query in same jsp file ..Plz help me..im new to JSP

    ReplyDelete

  29. I GOT THIS ERROR..MAY I HAVE SOLUTION FOR THIS....

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

    9: String userid=request.getParameter("user");
    10: session.putValue("user_id",userid);
    11: String pwd=request.getParameter("pwd");
    12: Class.forName("com.mysql.jdbc.Driver");
    13: Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3386/tests","root","deepti");
    14: Statement st= con.createStatement();
    15: ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");


    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

    ReplyDelete
  30. @Deepti

    Copy servlet.jar file into lib folder

    ReplyDelete
  31. Hello srinivas,
    I have added servlet.jar file into lib folder.. but again , same problem is here...
    and
    session.putValue("user_id",userid);
    i got a warning in this line...

    ReplyDelete
  32. I need to diplay login.jsp in login.html page only by clicking button please help me.

    ReplyDelete
  33. helped me lot 2 understand some basics

    ReplyDelete
  34. its nice working for me

    ReplyDelete
  35. Simple and easy way to understand...thanks I got what I wanted... excellent..!

    ReplyDelete
  36. type Exception report

    message

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

    exception

    org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 15 in the jsp file: /reg.jsp
    String literal is not properly closed by a double-quote
    12: "root","tiger");
    13: Statement st= con.createStatement();
    14: ResultSet rs;
    15: int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
    16: '"+lname+"','"+email+"')");
    17:
    18:


    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.

    what is the error???

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

    exception

    org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 15 in the jsp file: /reg.jsp
    String literal is not properly closed by a double-quote
    12: "root","tiger");
    13: Statement st= con.createStatement();
    14: ResultSet rs;
    15: int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
    16: '"+lname+"','"+email+"')");
    17:
    18:


    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.

    how to solve it

    ReplyDelete
  38. We compiled login.jsp and we have got the following errors. Kindly help me out.

    compile-single-jsp:
    init:
    deps-module-jar:
    deps-ear-jar:
    deps-jar:
    library-inclusion-in-archive:
    library-inclusion-in-manifest:
    compile:
    Created dir: C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src
    Created dir: C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\classes
    Compiling 1 source file to C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\classes
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: illegal start of expression
    } catch (Throwable t) {
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: 'catch' without 'try'
    } catch (Throwable t) {
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: ')' expected
    } catch (Throwable t) {
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: not a statement
    } catch (Throwable t) {
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: ';' expected
    } catch (Throwable t) {
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:81: 'finally' without 'try'
    } finally {
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:35: 'try' without 'catch' or 'finally'
    try {
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:85: reached end of file while parsing
    }
    8 errors
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\nbproject\build-impl.xml:597: The following error occurred while executing this line:
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\nbproject\build-impl.xml:589: The following error occurred while executing this line:
    C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\nbproject\build-impl.xml:285: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 11 seconds)

    ReplyDelete
  39. i want to crate login page and connnect badabase match username and password then open wellcome page in jsp. using jsp & servlet

    ReplyDelete
  40. Got a error like this


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

    25: String state=request.getParameter("cstate");
    26: String coun=request.getParameter("country");
    27:
    28: Class.forName("com.mysql.jdbc.Driver");
    29: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
    30: Statement st= con.createStatement();
    31: ResultSet rs;


    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


    root cause

    javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.verifyreg_jsp._jspService(verifyreg_jsp.java:110)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


    root cause

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Class.java:186)
    org.apache.jsp.verifyreg_jsp._jspService(verifyreg_jsp.java:92)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


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

    ReplyDelete
  41. thanks for the tutorial. i don't understand how it selects the database "test"

    ReplyDelete
  42. Thank u soo much srinivas it works for me...
    But in login.jsp code :

    out.println(...) not working..

    btw all its awesome! Thanks a Lot

    ReplyDelete
  43. I find this is great intro to JSP login with Mysql and Tomcat, I worked out the blank login.jsp page as Pankaj wrote earlier there.

    What to do once register is done though, wouldn't it be useful to send back to the login page?

    Also I wonder about the Welcome.jsp use as it's not linked to any of the other files..
    I want to implement functions to allow user to input data in other tables so I need to know who is logged in to identify those inputs.

    How can 'log out' function be implemented to complete the system?

    Thanks for your input

    ReplyDelete
  44. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    getting this error ...

    ReplyDelete
  45. Thank you it was of really great help
    kudos
    NB if you have done everything and your site is giving sql errors please uplaod the mysql jar file in the libraries

    ReplyDelete
  46. very excellent site i never seen that . for me need
    accurately available in this site thank u anna by anand salem tamilnadu

    ReplyDelete
  47. Hi, This conntent is Realy good and well structured including the images. my name is JC from venezuela, im a Network Enginer and im starting in web app´s. My congratulations.

    ReplyDelete
  48. plz help me once i click on the login button
    the the code of login.jsp is displayed and the file doesnt runs

    ReplyDelete
  49. Those who who had the issues with the ..
    "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"

    errors and so on..

    YOu need to place the "mysql-jdbc.jar" file to the "/WEB-INF/lib" directory.

    restart tyour app server and try again..

    Also there is a typo error in login.html ..repalce "usr" with "user" ..

    ReplyDelete
  50. how you create above diagrams? using any software tool .....plz tell i want use in my paper persentaion PPT...

    ReplyDelete
  51. kudos !! nice tutorial here...
    thnxx

    ReplyDelete
  52. I GOT THIS ERROR:
    org.apache.jasper.JasperException: An exception occurred processing JSP page /project/insert.jsp at line 18

    15: user="root" password=""/>
    16:
    17:
    18:
    19: INSERT INTO Booking VALUES ('<%= request.getParameter("CompanyName") %>','<%= request.getParameter("BikeName")%>','<%= request.getParameter("CustomerName") %>','<%= request.getParameter("CustomerAddress") %>','
    20:
    21:

    ReplyDelete
  53. hello sir i am using mqsql 5.1 version . you dont tell how to connect with jsp to database. you tell to me connection procedure sir please.

    ReplyDelete
  54. hi i am ashok how to connect to jsp servlet and javabean

    ReplyDelete
  55. Hi I am shivshankar,
    Thank you for this tutorial but i got an error like following


    type Exception report

    message javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'

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

    exception

    org.apache.jasper.JasperException: javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    root cause

    javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.reg_jsp._jspService(reg_jsp.java:86)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    root cause

    com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
    com.mysql.jdbc.Connection.execSQL(Connection.java:3118)
    com.mysql.jdbc.Statement.executeUpdate(Statement.java:1313)
    com.mysql.jdbc.Statement.executeUpdate(Statement.java:1232)
    org.apache.jsp.reg_jsp._jspService(reg_jsp.java:77)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.

    ReplyDelete
  56. thank you sir for giving this information.........

    ReplyDelete
  57. Simple and effective!

    ReplyDelete
  58. what is username and pasword????

    ReplyDelete
  59. org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 23 in the jsp file: /login.jsp
    Syntax error on token "else", EmptyStatement expected after this token
    20: out.println("Invalid password try again");
    21: }
    22: }
    23: else
    24: %>
    HOW TO SOLVE THIS ERROR????

    ReplyDelete
  60. Hello Srinivas,

    I am big fan of your tutorial. I have started to learn JSP+Servlet. I am having problem to insert auto increment primary key. see my below code in two different format.

    Code-1
    -----------
    Class.forName(JDBC_DRIVER);
    // Open a connection
    out.println("Connecting to a selected database...");
    con = DriverManager.getConnection(DB_URL, USER, PASSWORD);
    out.println("Connected database successfully...");

    // Execute a query
    st = con.createStatement();
    sql = "insert into users (name,email,password) values ('" + name + "','" + email + "','" + password + "')";
    out.println(sql);
    int i = st.executeUpdate(sql);
    if (i > 0) {
    out.println("Inserted!!");
    }

    Code-2
    -----------
    Class.forName(JDBC_DRIVER);
    con = DriverManager.getConnection(DB_URL, USER, PASSWORD);
    PreparedStatement ps;
    st = con.createStatement();
    sql = "insert into users (name,email,password) values (?,?,?)";
    ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
    ps.setString(1, name);
    ps.setString(2, email);
    ps.setString(3, password);
    ps.executeUpdate();

    ReplyDelete
  61. keep files like this.
    program files->
    Apache tomcat software foundation->
    Tomcat7.0->
    webapps->create new folder(like test)
    keep files(.jsp and .html) in this folder->
    WEB-INF->
    .xml(web.xml)
    lib->.jar file(mysql-connector-java-5.1.18-bin)

    ReplyDelete
  62. could any one resolve my problem in the above program.....

    type Exception report

    message

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

    exception

    org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

    root cause

    javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
    org.apache.jsp.class1.login_jsp._jspService(login_jsp.java:94)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

    root cause

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Class.java:190)
    org.apache.jsp.class1.login_jsp._jspService(login_jsp.java:70)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

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

    ReplyDelete
  63. plz post some other projects also like students' database for a collage

    ReplyDelete
  64. i cant able to connect database with jsp page .The fields which we enter are not getting connected to database so could u plz help me...

    ReplyDelete
  65. Hi

    Anyone who can help with this error, I would appreciate.

    org.apache.jasper.JasperException: An exception occurred processing JSP page /test/reg.jsp at line 10

    7: String fname=request.getParameter("fname");
    8: String lname=request.getParameter("lname");
    9: String email=request.getParameter("email");
    10: Class.forName("com.mysql.jdbc.Driver").newInstance();
    11: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/users",
    12: "root","lehlo");
    13: Statement st= con.createStatement();


    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:405)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    ReplyDelete
  66. thank for the post. :) Good post.
    is very useful and can be understood

    ReplyDelete
  67. where can i run this program

    ReplyDelete
  68. working fine. After registration page is not diverting to welcome.jsp . data is inserted successfully
    and. login.jsp is working fine

    ReplyDelete
  69. your code executed successfully. I put the welcome.jsp code "Registration is Successfull. Welcome to <%=user %>" below reg.jsp to get the desired result

    ReplyDelete
  70. HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    type Exception report

    message The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

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

    exception

    org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:56)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:445)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:117)
    org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:325)
    org.apache.jasper.compiler.TagLibraryInfoImpl.(TagLibraryInfoImpl.java:154)
    org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:410)
    org.apache.jasper.compiler.Parser.parseDirective(Parser.java:475)
    org.apache.jasper.compiler.Parser.parseElements(Parser.java:1410)
    org.apache.jasper.compiler.Parser.parse(Parser.java:138)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
    org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.

    ReplyDelete
  71. my problem is .jsp page not found and at com.dell.LoginServlet.doGet(LoginServlet.java:26)


    solve this problem

    ReplyDelete

Make in India