InsertInto-Database.jsp
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<html>
<head>
<title>Insert into database</title>
</head>
<body>
<table border="1" width="50%">
<tr>
<td width="100%">
<form method="POST" action="InsertDatabaseAction.jsp">
<h2 align="center">Insert Into Database</h2>
<table border="1" width="100%">
<tr>
<td width="50%"><b>First Name:</b></td>
<td width="50%"><input type="text" name="firstname" value=""/> </td>
</tr>
<tr>
<td width="50%"><b>Last Name:</b></td>
<td width="50%"><input type="text" name="lastname" size="21"></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>
</td>
</tr>
</table>
</body>
</html>
InsertDatabaseAction.jsp
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<%
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
java.util.Date date;
String s=null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
String username=request.getParameter("firstname");
String lastname=request.getParameter("lastname");
int val = st.executeUpdate("insert stu_detail values(id,'"+username+"','"+lastname+"')");
con.close();
out.println("successfully insert data into database!");
out.println("</br>");
out.println("<br/><a href=useredit-page.jsp><b>Edit and Delete</b></a>");
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>
useredit-page.jsp
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<html>
<head>
<title>Retrive value from database</title>
</head>
<body>
<table border="0" width="75%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<h2><font color="#FF0033">Retrive data from database</font></h2>
<form method="POST" >
<table border="1" width="100%" cellspacing="0" cellpadding="0" bgcolor="#CCFFCC">
<tr>
<td width="50%"><b>User ID:</b></td>
<td width="50%"><b>First Name</b></td>
<td width="50%"><b>Last Name</b></td>
<td width="50%"><b>Edit</b></td>
<td width="50%"><b>Delete</b></td>
<%
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
String query = "SELECT * FROM stu_detail";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
%>
<tr>
<td width="50%" valign="right"><%=rs.getInt("id")%><br/></td>
<td width="50%"><%=rs.getString("firstname")%><br/></td>
<td width="50%"><%=rs.getString("lastname")%><br/></td>
<td width="50%"><a href="edit.jsp?id=<%=rs.getInt("id")%>"><img src="edit.jpg" border="0"></a></td>
<td width="50%"><a href="RecordDelete.jsp?id=<%=rs.getInt("id")%>"><img src="delete.jpg" border="0"></a></td>
</tr>
<%}
out.println("<br/><a href=insertInto-database.jsp><b>Insert Data page</b></a>");
rs.close();
con.close();
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
edit.jsp
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<html>
<head>
<title>Insert into database</title>
</head>
<body>
<%
String strId =request.getParameter("id");
int id = Integer.parseInt(strId);
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
String query = "SELECT firstname,lastname FROM stu_detail where id="+id;
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
%>
<table border="1" width="50%">
<tr>
<td width="100%">
<form method="POST" action="RecordUpdateAction.jsp">
<input type="hidden" name="id" value="<%=request.getParameter("id")%>">
<h2 align="center">Insert Into Database</h2>
<table border="1" width="100%">
<tr>
<td width="50%"><b>First Name:</b></td>
<td width="50%"><input type="text" name="firstname" value="<%=rs.getString("firstname")%>"/> </td>
</tr>
<tr>
<td width="50%"><b>Last Name:</b></td>
<td width="50%"><input type="text" name="lastname" value="<%=rs.getString("lastname")%>" size="21"></td>
</tr>
</table>
<p><input type="submit" value="Update" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>
</td>
</tr>
</table>
<%}
rs.close();
con.close();
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>
</body>
</html>
RecordUpdateAction.jsp
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<%
String strId =request.getParameter("id");
int id = Integer.parseInt(strId);
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
String username=request.getParameter("firstname");
String jobposition=request.getParameter("lastname");
int in = st.executeUpdate("UPDATE stu_detail SET firstname ='"+username+"', lastname ='"+jobposition+"' where id ="+id);
con.close();
out.println("successfully updated");
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>
--------------------------------------------------------------
RecordDelete.jsp
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<%
String strId =request.getParameter("id");
int id = Integer.parseInt(strId);
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
String username=request.getParameter("firstname");
String jobposition=request.getParameter("lastname");
int in = st.executeUpdate("DELETE FROM stu_detail where id ="+id);
con.close();
out.println("successfully updated");
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>