please explain how to validate form input string with database n also how its notify that entered data exists already .please reply soon
hi friend,
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!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>Registration Form</title>
</head>
<body>
<form action="#">
<center><h2>Registration Form</h2></center>
<table>
<tr>
<td>Student ID</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>Student Name</td>
<td><input type="text" name="sName"/></td>
</tr>
<tr>
<td>Phone No</td>
<td><input type="text" name="phno"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/record";
String user = "root";
String password = "root";
String id = request.getParameter("id");
String name = request.getParameter("sName");
String phno = request.getParameter("phno");
String sql = "insert into student2(id, sName, phno) values(?,?,?)";
String sql2 = "select * from student2 where phno = ?";
Connection con = null;
PreparedStatement ps = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
Continue...
if(request.getParameter("id") != null)
{
int sId = Integer.parseInt(id);
int sPhno = Integer.parseInt(phno);
try
{
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
ps1 = con.prepareStatement(sql2);
ps1.setInt(1, sPhno);
rs = ps1.executeQuery();
if(rs.next())
{
int ssId = rs.getInt("id");
int ssPhno = rs.getInt("phno");
String ssName = rs.getString("sName");
if(((sId == ssId && sPhno == ssPhno) || (name.equals(ssName) && sPhno == ssPhno)) || (name.equals(ssName) && sId == ssId))
{
out.println("Record Already Existed");
}
else
{
ps = con.prepareStatement(sql);
ps.setInt(1,sId);
ps.setString(2,name);
ps.setInt(3,sPhno);
boolean bol = ps.execute();
out.println("Data Added Successfully");
}
}
else
{
ps = con.prepareStatement(sql);
ps.setInt(1,sId);
ps.setString(2,name);
ps.setInt(3,sPhno);
boolean bol = ps.execute();
out.println("Data Added Successfully");
}
}
catch(Exception e)
{
out.println("SQL Error : "+ e);
}
}
%>
</body>
</html>
Thanks.