JDBC in JSP(Java Server Pages)

Hi friends, in this tutorial we will see that how to write JDBC code in a JSP file. Let’s take an example for clarity.

Let we have a registration form that is registration.jsp file. When the user will enter data in registration form, data will insert into registration table. For this we have to develop another JSP file let process.jsp file which will consider as business logic. Here another jsp file(welcome1.jsp) will give response.



Here we are using MySQL as our database environment.

In MySQL environment we will create a table, let the table name is registration table that we have created in employeedb database.(employeedb is our database name).


create table employeedb.registration
(
name varchar(200) not null,
contact varchar(200) not null,
email varchar(200) primary key,
address varchar(200) not null
)

registration.jsp
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center><h1>Registration Form</h1>
<form action="process.jsp">
<table>
    <tr>
        <td>Name</td>
        <td><input type="text"name="name"></td>
    </tr>
    <tr>
        <td>Contact</td>
        <td><input type="text"name="contact"></td>
    </tr>
    <tr>
        <td>Email</td>
        <td><input type="email"name="email"></td>
    </tr>
    <tr>
        <td>Address</td>
        <td><input type="text"name="address"></td>
    </tr>
    <tr>
        <td><input type="submit"value="register"></td>
    </tr>
</table>
</form>
</center>
</body>
</html>


process.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    String s1=request.getParameter("name");
    String s2=request.getParameter("contact");
    String s3=request.getParameter("email");
    String s4=request.getParameter("address");

    int k=0;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employeedb","root","silan");

        PreparedStatement ps=con.prepareStatement("insert into registration values(?,?,?,?)");
        ps.setString(1,s1);
        ps.setString(2,s2);
        ps.setString(3,s3);
        ps.setString(4,s4);
        k=ps.executeUpdate();
        if(k>0)
        {
            response.sendRedirect("welcome.jsp");
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

%>
</body>
</html>

welcome.jsp
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    out.print("registration is successful");
%>
</body>
</html>

Then in web.xml file we will set registration.jsp file as our welcome file.

Now we will run, then we will get the following output:


img

Then we will check our database.
We will execute the following sql select statement, that is:
Select * from employeedb.registration


img
Project Structure:

img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext