Sunday, 12 July 2015

How to insert data into mysql database using Java-JSP

                        In this post, I have given a program for how to insert data into the mysql table using Java (JSP).
                        Here, frontend and backend programs are in JSP while database used is Mysql. Database name is "test", Database user name is also "test" and password is "123". Table name in which I have inserted data here is syllabusplanpmcd4. Table's configuration is shown in following table:


Database decription for table syllabusplanpmcd4 

                  Go through following programs insert.jsp and insertbackend.jsp. Outputs are shown at the end of this post.

insert.jsp

<html>
<head>
</head>
<body>

<div style="background-color:SlateGray">
<h1>Web Application for Insert Operation</h1>
</div>

     <h1>Enter the details:</h1>
   
    <table>
      <tr>
     
        <td><form method="post" action="insertbackend.jsp">


Unit No.:
                          <select name="unitno">
              <option disabled selected>Option</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
                          <option value="5">5</option>
              <option value="6">6</option>
            </select>
<br>
<br>
   
Topic: <input type="text" name="topic" />
<br>
<br>

Description: <textarea name="description" rows=4 cols=25></textarea>
<br>
<br>

Hours Required: <input type="text" name="hoursrequired" />
<br>
<br>

 <input type="submit" value="Submit" >
           
</form>

<div style="background-color:SlateGray">
Sample JDBC-JSP program for Insert Operation
</div>

</body>
</html>



insertbackend.jsp

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
</head>
<body>
<div style="background-color:SlateGray">
<center>
<h1>Web Application for Insert Operation</h1>
<center>
</div>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/test"
     user="test"  password="123"/>
<center>
<br />
<br />
<br />

<%

   int a=Integer.parseInt(request.getParameter("unitno"));

   String b= request.getParameter("topic");

   String c= request.getParameter("description");

   int d= Integer.parseInt(request.getParameter("hoursrequired"));

%>

<%
try{
         Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "test", "123");
           Statement st=con.createStatement();
          
                String q="insert into syllabusplanpmcd4 values("+a+",'"+b+"','"+c+"',"+d+")";
          
                int i=st.executeUpdate(q);
             
                out.println(
"<b><font size=5>"+"Data is successfully inserted into table syllabusplanpmcd4"+"</font></b>");     
   }
        catch(Exception e){
        System.out.print(e);
        e.printStackTrace();
        }

%>


<br />
<br />
<br />

<form action="insert.jsp">
<input type="submit" value="Click Here to insert another record">
</form>

<div style="background-color:SlateGray">
Sample JDBC-JSP program for Insert Operation
</div>
</body>
</html>



Output:


insert.jsp


insertbackend.jsp


Table in mysql database (syllabusplanpmcd4) after data addition



Check Other Posts on Web Designing

Next: How To Use Multiple Resultset in JAVA / JSP

Previous: How to retrieve data from mysql database in jsp ( JSP-JDBC selecting records from mysql database )

No comments:

Post a comment