how can i prevent duplicate records using servlets and MVC model?

how can i prevent duplicate records using servlets and MVC model?

hai,

I need a program to insert values into database at the same time it shows error msg on response page while we are giving duplicate entry of a primary key.i want this program in MVC model.can anyone help me...

Thanks&Regards P.Divya

View Answers

April 27, 2011 at 5:21 PM

1)form4.jsp:

<html>
<form method="post" action="../InsertServlet">
<table>
<tr><td>Id:</td><td><input type="text" name="id"></td></tr>
<tr><td>Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>Address:</td><td><input type="text" name="address"></td></tr>
<tr><td>Contact No:</td><td><input type="text" name="contact"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>

2)InsertServlet.java:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InsertServlet extends HttpServlet{ 
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
        res.setContentType("text/html");
        int count=0;
        PrintWriter out = res.getWriter();
        int id=Integer.parseInt(req.getParameter("id"));
        String name=req.getParameter("name");
        String address=req.getParameter("address");
        int phone=Integer.parseInt(req.getParameter("contact"));
        String email=req.getParameter("email");
        try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from employee where id='"+id+"'");
        while(rs.next()){
        count++;
        }
        if(count>0){
        out.println("<font color=red>Error: Id already exists</font>");

        }
        else{
         int i=st.executeUpdate("insert into employee(id,name,address,contactNo,email) values("+id+",'"+name+"','"+address+"',"+phone+",'"+email+"')");
        out.println("<font color=green>Data is successfully inserted!</font>");
        }
        }
        catch(Exception e){
        System.out.print(e);
                }       
            }
        }









Related Tutorials/Questions & Answers:
how can i prevent duplicate records using servlets and MVC model?
how to validate duplicate records in struts1
Advertisements
how to validate duplicate records in struts1
How to prevent adding duplicate items to the shopping cart
duplicate records inserting
using jsp and servlets andin Mvc architecture
How a simple "Hello World" can be displayed using Spring 3.2 MVC?
JDBC : Duplicate records in a table
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
how do i provide down a pdf document fecility on my web page using jsp and servlets?
how can we prevent back option after log out.
Mysql Find Duplicate Records
How can I execute a PHP script using command line?
how can i achieve multiple inheritance in java without using inheritance ?
How I can filling pdf file that crated by livecycle using itext
How can i draw a line using java swings
How can i draw a line using java swings
How i can send testing mail on my id using java?
ModuleNotFoundError: No module named 'modelr'
ModuleNotFoundError: No module named 'modelr'
How can I generate diff image using Jmagick?
Need to Remove Duplicate Records from Excel Sheet
how can i draw a table using applet or swings - Java Beginners
How i can send mail by using jsp.............. - JavaMail
how can manages records in jsp
save multiple records into database using jsp/servlet mvc
How to export web page to excel using java or jsp or servlets
How can I access databse through JSP. I am using postgresql-8.4.4-1-windows as database and jboss-4.0.5.GA as server.
how to display duplicate elements with out using collection Frame work?
ModuleNotFoundError: No module named 'modelo'
ModuleNotFoundError: No module named 'modelo'
ModuleNotFoundError: No module named 'modelx'
ModuleNotFoundError: No module named 'modelx'
ModuleNotFoundError: No module named 'modelx'
how to create a reminder app using threads in Servlets?
prevent mutli-click using struts tokens.
How to delete excel file records using Store Procedure?
How to validate form using Spring MVC?
How to prevent from navigating to ck buttonnext page when user reaches login page using back button
How to prevent from navigating to a page after reaching Login page using back button?
User Registration Form Using JSP(JspBeans) after that how i can insert in database
ModuleNotFoundError: No module named 'modelo-visao'
how can i create a discussion forum?
How can i Change Button Label Value Randomly in VIRTUAL KEYBOARD using JAVA or Net Beans ???
How can i Change Button Label Value Randomly in VIRTUAL KEYBOARD using JAVA or Net Beans ???
ModuleNotFoundError: No module named 'spyder-modelx'
ModuleNotFoundError: No module named 'spyder-modelx'

Ads