Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Submit comments in database when user clicks on submit button

Related Tutorials


 
 

Share on Google+Share on Google+

Submit comments in database when user clicks on submit button

Advertisement
This is detailed java code to show how to submit comments from a jsp page and insert it to the MySql database when user clicks the submit button of the page.

Submit comments in database when user clicks on submit button

     

This is detailed java code to show how to submit comments from a jsp page and insert it to the MySql database when user clicks the submit button of the page.

Create a database: First create a database named "usermaster" in MySql and table named "user_master" in same database by SQL query given below:

 

 

 

create table user_master (
   user_id int not null auto_increment,
   name varchar(20),
   comment text(200),
   primary key(user_id)
);

Now create the JSP page for the user to place the comment and submit the comment. For this we have created first_page.jsp as below.

first_page.jsp

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<html> 
    <head> 
        <title>Create table in mysql database using jsp</title>
    </head> 
    <body bgcolor="#ffffcc">
     <form action="ConnectJspToMysql.jsp" method="get">
      <TABLE style="background-color: #ECE5B6;" WIDTH="40%">
         <tr>
           <td >Student Id</td>
             <td ><input type="text" name="id" SIZE="35"></td>
              </tr>
              <tr>
                <td >Student Name</td>
                <td ><input type="text" name="name" SIZE="35"></td>
              </tr>
                <tr width="100%">
                  <td >Comments<br><font color="blue">
                  (max 200 chars)</font></td>
                  <td ><TEXTAREA NAME="comment" ROWS="5" COLS="26">
                  </TEXTAREA></td>
              </tr>
                <tr><td></td>
                 <td><input type="reset" name="reset"
                  value="reset">&nbsp;&nbsp;
                <input type="submit" value="submit"></td></tr>
            </TABLE>
        </form>
    </body> 
</html>

Save this code as a .jsp file named "first_page.jsp" in your application directory in tomcat server and run this jsp page with url "http://localhost:8080/user/first_page.jsp" in address bar of the browser.

Now create another JSP "ConnectJspToMysql.jsp" which retrieves the comment written by the user in the previous page and insert it to the database. To connect with the database you need database driver jar file in the lib directory of your application directory in Tomcat.

ConnectJspToMysql.jsp

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<html> 
<head> 
    <title>data successfully saved</title>
</head> 
<body>
<% // get parameters from the request
   String id = request.getParameter("id");
   String name = request.getParameter("name");
   String comment = request.getParameter("comment");
   Statement statement = null;
            
   // declare a connection by using Connection interface 
   Connection connection = null;
   try {
   /* Create string of connection url within specified format 
   with machine name, 
   port number and database name. Here machine name id 
   localhost and database name is usermaster. */
   String connectionURL = "jdbc:mysql://localhost:3306/usermaster";

   // Load JBBC driver "com.mysql.jdbc.Driver" by newInstance() method. 
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   /* Create a connection by using getConnection() 
   method that takes parameters of string type connection url, user 
   name and password to connect to database. */
   connection = DriverManager.getConnection(connectionURL, "root", "root");
   statement = connection.createStatement();
   String QueryString = "INSERT INTO user_master
   (user_id,name,comment) VALUES ('" + 
   id + "','" + name + "','" + comment + "')";
   int UQ = statement.executeUpdate(QueryString);
   if (UQ > 0) {
%><font color="green" size="5" >Congratulations !</font>
<h2><p>Your comment is submitted successfully.<br></p></h2>
<TABLE style="background-color: #ECE5B6;" WIDTH="30%">
    <tr>
    
    <tr>
        <td>User Id</td>
        <td><%= id%></td>
    </tr>
    <tr>
        <td>Name</td>
        <td><%=name%></td>
    </tr>
    <tr><td></td><td align="right">
            <A HREF="first_page.jsp">
            <font size="4" color="blue">go to home page</font></A>
        </td>
    </tr>
</TABLE>   
<%	}
} catch (Exception ex) {
%>
<FONT size="+3" color="red"></b>
    <%
                out.println("Unable to connect to database.");
            } finally {
                statement.close();
                connection.close();
            }
    %>
</FONT>
</body> 
</html>

Save this code as a .jsp file named "ConnectJspToMysql.jsp" in your application directory in Tomcat.

To start Tomcat server run startup.bat file from directory Tomcat-6.0.16/bin/startup.bat. To run this application open browser and type http://localhost:8080/user/first_page.jsp in address bar and open this jsp page.

Fill all the fields in the form, student id must be integer number then click submit button. If all the values are submitted successfully in database, this page will call another jsp page that shows following  message on the page..

If found any error to submit data in database shows following  error message....

Download source code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 7, 2008

Related Tutorials

Discuss: Submit comments in database when user clicks on submit button   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Tejinder Cheema
October 5, 2012
connectivity of submit button to the database

Hi there, I have got how to develop html page based web form using Dreamweaver CS6, but I am unable to get information about how to connect the submit button on this web form to any database. Let me know which database creation is easy to develop and connect to the submit button. So please suggest me how I can get solution of my said problem. Thanks and Waiting for your reply, Tejinder Cheema
Jeff
November 29, 2013
exception

Hi, I have read thru your post: http://www.roseindia.net/jsp/submit-comments-database.shtml I have tried it myself,and i found below exception,may i know how can i solve it?I am beginner to jsp,hope you can help me out.Thanks! org.apache.jasper.JasperException: java.lang.NullPointerException org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause java.lang.NullPointerException org.apache.jsp.test.ConnectToMysql_jsp._jspService(ConnectToMysql_jsp.java:130) 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:386) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
DMCA.com