dcsimg
Meeting trouble when inserting a record into dababase using JDBC.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   George_Simple
Posted On:   Saturday, March 22, 2003 09:27 PM

Hi, everyone! If I want to insert a record in a table of a database, but I do not know whether there is already a primary key existing in the database (I mean the value of the record which I want to insert conflicting, i.e. duplicating, with one of the value of primary key in the database). So, I have two solutions: 1. check whether there is already a primary key which confilcting with the value I want to insert, if not, insert into database. 2. do not do any check before and insert the record directly, and catch SQLException. But they both have shortcomings. Solution 1 will lost time (must checking first) and solution 2 will throw a SQLException, and from the Exception, I do not know how c   More>>
			
Hi, everyone!


If I want to insert a record in a table of a database, but I do not know whether there is already a primary key existing in the database (I mean the value of the record which I want to insert conflicting, i.e. duplicating, with one of the value of primary key in the database). So, I have two solutions:

1. check whether there is already a primary key which confilcting with the value I want to insert, if not, insert into database.

2. do not do any check before and insert the record directly, and catch SQLException.


But they both have shortcomings. Solution 1 will lost time (must checking first) and solution 2 will throw a SQLException, and from the Exception, I do not know
how can I infer it is a primary key conflicting exception.
(I do not know how to distinguish it from other type of SQLException, for example, database name error exception or SQL statement syntax error).


I do not know whether I have made myself understood.
Who have better solutions?


Thanks in advance,
George
   <<Less

Re: Meeting trouble when inserting a record into dababase using JDBC.

Posted By:   Jeanne_Boyarsky  
Posted On:   Sunday, March 23, 2003 10:29 AM

George,

As you pointed out, the first solution is cleaner. Most databases create indexes on the primary key automaticaly. So there shouldn't be a big performance impact by checking first.
About | Sitemap | Contact