Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Gud mng.My doubt is i am updating the data in the table called userinfo by inserting the data into the SenderId column from the web application.Whenever i am inserting the SenderId through the webapplication if the entered SenderId is already there in Database then it has to show a message as SenderId already exists and it is not going to take the duplicate values.Can you please give me any answer. I am using Servlets and JSP's.Waiting for the reply.



Thanks In Advance.
Posted

1 solution

You should definitely use automatic fields in the database and get them to be updated automatically...

Your design could allow that two people would access the database at the same time and then try to insert the same data. And you don't have any way to avoid that except the one described before...

Use primary/secondary keys, and autonumeric fields in your database; in that way you will avoid this kind of situations.

If you have problems doing that you should tell us the kind of database you are using and therefore we would be ready to help...

Good luck.

* IMPROVEMENT 1:
The idea is that you should not introduce the key manually in order to avoid duplicate values.
You should check if the data introduced is already there and if it is not, then you should INSERT it normally but without the senderId.
The MySQL database (if using AUTO-INCREMENT) will make that for you.

Take a look at this link[^] where you will find some extra details about using AUTO-INCREMENT and a sample of the scenario I'm talking about.

* IMPROVEMENT 2:
Take a look at this link[^].

Hope this helps.
 
Share this answer
 
v3
Comments
komali Guntur 13-Jul-11 3:13am    
Thank You Mr Joan Murt
But my problem is not with the database in database it is updating the value with the given value from the application.After that whenever i am trying to enter the SenderId to the another user through the application then it has to take the all the SenderId's from the database and compare the entered value with all the existed SenderId's in the database if nothing is match then no problem it will update.But if any thing is match then it has to display the message as SenderId already exists.And i am using Mysql 5.1 DB.Am i provided the correct information or not i dont know i am new to java and forums also.If you require any more clarification i am sending my servlet class just go through it.I think you can understand.

public class AddSenderId extends HttpServlet {

public static Logger log ;
String message=null;
public AddSenderId() { }


public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Transaction tx = null;
HttpSession sesn = null;
try {
log = Logger.getLogger(this.getClass());
PrintWriter out = response.getWriter();// this can be removed.
sesn = request.getSession(false);

int userid = Integer.parseInt(request.getParameter("userid"));
String senderId = request.getParameter("senderid");
String remarks = request.getParameter("remarks");
List <userinfo> senderid_list = new UserDao().getSenderID();
System.out.println(senderid_list.size());
for(int i=0;i
Joan M 13-Jul-11 3:23am    
Take another look to my answer where I've posted a link and new information that I think it can help you... ;)
komali Guntur 13-Jul-11 3:42am    
SenderId i defined as String type to the string type Auto Increment is allowed
Joan M 13-Jul-11 3:48am    
I don't think so... you should redefine it as an int... as AUTO-INCREMENT it increases by one each time...
komali Guntur 13-Jul-11 3:53am    
No yar My requirement is it must be string type because who ever is sending the message... that person name we can give as SenderId.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900