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

Ads

 
 
 

Share on Google+Share on Google+

sridivya pathaneni
how can i prevent duplicate records using servlets and MVC model?
1 Answer(s)      6 years and a month ago
Posted in : JSP-Servlet

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

Ads
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);
                }       
            }
        }

Ads









Related Tutorials/Questions & Answers:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training fat image from db fat image from db fat image from db fat image from db unable to get value returned from javascript variable in page How to change image size before adding to mysql using jsp Design a registration.jsp page with input for user registration details Cart Quantity jsp servlet coding issue servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? image pdf marking pdf marking Multiple image upload using JSP Read code from excel sheet and upload into database using JSP Student java about search engine Convert BLOB to PDF and save it to a location how to copy files from remote to local machine using SFTP SSL in java? Like Team viewer Socket and Servert -Jsp how to copy files from remote location to local using java? How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java how to create SOAP based web service in java? how to fetch data from mysql database table and draw a bar chart on that data using in jsp how to create users in LDAP using java? how to authenticate user using TIVOLI api in java? how to create users using IBM Tivoi API in jsp/servelet? Java/J2ee Please explain this code as many packages and classes are unknown to me .! how to download the uploaded folder files using jsp Login Query image is display from path of mysql database please reply the code to store dynamic fields in database? Reloading issue in jsp How to get Button Id ? calling one jap page from another jsp page login login Problem to get connection from DAO class to JDBC Page Loading Time Fix table's column's name row(1st row of the table) so that it does not move up when the table is scrolled up to view more rows below Sending hidden values

Ads

 
Advertisement null

Ads