By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

how to display duplicate entry

P: 2
hii im learning jsp and html with sql and im beginer on it,
i have the table andcodes below in mysql and jsp, im working on html and jsp with netbeans IDE 3.6. of course if i try to insert any thing into that table 2. time, i get a error message: java.sql.SQLException: Invalid argument value: Duplicate entry......

and i want to display that duplicate value with some warning message (value is already exist or something like that)
can any one help me on it, how to do that?????
regards

1-sql codes;
Expand|Select|Wrap|Line Numbers
  1. create table team
  2.             (team_id int not null AUTO_INCREMENT,
  3.             team_fahrer_name VARCHAR (80),
  4.             team_arbeiter_name VARCHAR (80),
  5.             team_fahrzeug_kenntzeichen VARCHAR (80),
  6.             team_beschreibung VARCHAR (80),
  7.             PRIMARY KEY (team_id),
  8.             UNIQUE KEY team_fahrer_name (team_fahrer_name),
  9.             UNIQUE KEY team_arbeiter_name (team_arbeiter_name),
  10.             UNIQUE KEY team_fahrzeug_kenntzeichen (team_fahrzeug_kenntzeichen))TYPE = INNODB;
  11.  
2-javabean;
Expand|Select|Wrap|Line Numbers
  1. public void neuenteam_einfuegen(){
  2.  
  3.         String fahrer_name = this.getfahrer_name();
  4.         String arbeiter_name = this.getarbeiter_name();
  5.         String fahrzeug_kennzeichen = this.getfahrzeug_kenntzeichen();
  6.         String fahrt_typ = this.getfahrt_typ();
  7.  
  8.             dbop= new DBOperationen();
  9.             dbop.dbverbindung();
  10.         String sql="insert into team (team_fahrer_name, team_arbeiter_name, team_fahrzeug_kenntzeichen, team_beschreibung) values ";
  11.         sql+="(\""+fahrer_name+"\",\""+arbeiter_name+"\",\""+fahrzeug_kenntzeichen+"\",\""+fahrt_typ+"\");";
  12.         System.out.println(sql);
  13.         dbop.executeupdate(sql);
  14.  
Feb 21 '09 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 785
just make a try-catch statement around your "dbop.executeupdate(sql); " statement. Inside the catch-part, print your warning message.

Be aware that all different errors are thrown that you need to care about in your catch-statement, like the database is down, or the network connection is blocked etc.). So just to test for duplicate primary keys, there is another way:
1.) lock the database table for writing
2.) inquire if one of the primary keys is already there (select * from team where team_fahrer_name=... or team_arbeiter_name=... or ...).
3.) if you get some records (=results) back, then print a warning only. Else, that means if there is no record returned, execute your update-statement.
4.) unlock database
Feb 23 '09 #2

P: 2
@chaarmann
thanx thanx thanx,
solved the problem
Feb 24 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.