Let’s see an example that I have given the clarity: how to insert and fetch data from database using JSP
Required files:
let we have Oracle10gXE database and we are using Type-4(Thin) driver for JDBC.
CREATE TABLE STUDENTS ( FNAME VARCHAR2(100), LNAME VARCHAR2(100), EMAIL VARCHAR2(100), ADDRESS VARCHAR2(100), MOBILE VARCHAR2(100), GENDER VARCHAR2(100), REG_NO VARCHAR2(100), BRANCH VARCHAR2(100) );
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Index Page</title> </head> <body> <center> <form name="myform" method="post" action="Registration Process.jsp""> <table border="1" width="30%" cellpadding="5"> <thead> <tr> <th colspan="2"><h2>Registration Form</h2></th> </tr> </thead> <tbody> <tr> <td>First Name</td> <td><input type="text" name="fname" value="" required/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lname" value="" required /></td> </tr> <tr> <td>Email</td> <td><input type="email" name="email" value=""required/></td> </tr> <tr> <td>Address</td> <td><input type="text" name="address" value="" required /></td> </tr> <tr> <td>Mobile</td> <td><input type="text" name="mobile" value="" required /></td> </tr> <tr> <td>Gender </td> <td> <input type="radio" name="gender" value="male"> Male<br> <input type="radio" name="gender" value="female"required> <br> </tr> <tr> <td>Registration No</td> <td><input type="text" name="reg_no"></td></tr> <tr> <td>Branch</td> <td> <input type="text" name="branch"></td> </tr> <tr> <td><input type="submit" value="Submit" /></td> <td><input type="reset" value="Reset" /></td> </tr> <tr> <td colspan="2">Already registered? <a href="Slogin.jsp">Login Here</a></td> </tr> </tbody> </table> </form></center> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import ="java.sql.*"%> <%@ page import ="oracle.jdbc.driver. *"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here </head> <body> <% String s1=request.getParameter("fname"); String s2=request.getParameter("Iname"); String s3=request.getParameter("email"); String s4=request.getParameter("address"); String s5=request.getParameter("mobile"); String s6=request.getParameter("gender"); String s7=request.getParameter("reg_no"); String s8=request.getParameter("branch"); int k=0; try { // to load and register the driver Class.forName("oracle.jdbc.driver.Oracle Driver"); //Establish the Connection Connection con=DriverManager.getConnection("jdbc:oracle: thin: @localhost:1521:xe","system","oracle"); //Creae the statement object PreparedStatement ps=con.prepareStatement("insert into students values(?,?,?,?,?,?,?,?)"); ps.setString(1,s1); ps.setString(2,s2); ps.setString(3,53); ps.setString(4,54); ps.setString(5,55); ps.setString(6,56); ps.setString(7,57); ps.setString(8,58); //Execute the query k=ps.execute Update(); if(k>0) { response.sendRedirect("success.jsp"); } response.sendRedirect("Student Registration.jsp"); } } catch(Exception e) { e.printStackTrace(); } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <center><h1>Successfully saved!</h1></center> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import ="java.sql.*"%> <%@ page import ="oracle.jdbc.driver.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Student Database</title> </head> <body> <BR><BR> <center>STUDENT REGISTRATION DATABASE</center> <% Class.forName("oracle.jdbc.driver. OracleDriver"); Connection connection = DriverManager.getConnection "jdbc:oracle:thin: @localhost:1521:xe", "system", "oracle"); Statement statement = connection.createStatement(); ResultSet rs= statement.executeQuery("select * from students"); %> <br><br><br><br> <center><TABLE BORDER="1"> <TR> <TH>First Name</TH> <TH>Last Name</TH> <TH>Email</TH> <TH>Address</TH> <TH> Mobile No</TH> <TH>Gender</TH> <TH>Registration No</TH> <TH>Branch</TH> </TR> <% while(rs.next(){ %> <TR> <TD> <%= rs.getString(1) %> </TD> <TD> <%= rs.getString(2) %> </TD> <TD> <%= rs.getString(3) %> </TD> <TD> <%= rs.getString(4) %> </TD> <TD> <%= rs.getString(5) %> </TD> <TD> <%= rs.getString(6) %> </TD> <TD> <%= rs.getString(7) %> </TD> <TD> <%= rs.getString(8) %> </TD> </TR> <%} %> </TABLE></center> <BR> </body> </html>
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