473,396 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to identify whether or not particular ID exists or not in database?

Gobindap
I am using Jtable to display and edit data. ID, name and marks columns are required to display here. I want both to edit existing value and insert new value in it. If certain ID exists, I need to update and if not I need to insert new record.

For this how to detect whether or not certain ID already exists there in database and some not? What type of code should I user here?

Please help me to overcome my problem.
Apr 11 '13 #1

✓ answered by r035198x

Different databases have their SQL specific ways of handling this e.g MySQL has an
Expand|Select|Wrap|Line Numbers
  1. ON DUPLICATE KEY UPDATE
clause which is not standard SQL . So you can either

1.) Use a database specific clause.
2.) Try an update first then if the update statement returns zero records then you run an insert. If you use this approach you should find ways of synchronizing the logic so that you don't have two threads trying to insert or update the same record.
3.) You can use JPA instead of direct JDBC. This would require a bit of reading. JPA shields you from working with SQL directly and it generates the right SQL for the right database.

5 3137
r035198x
13,262 8TB
Different databases have their SQL specific ways of handling this e.g MySQL has an
Expand|Select|Wrap|Line Numbers
  1. ON DUPLICATE KEY UPDATE
clause which is not standard SQL . So you can either

1.) Use a database specific clause.
2.) Try an update first then if the update statement returns zero records then you run an insert. If you use this approach you should find ways of synchronizing the logic so that you don't have two threads trying to insert or update the same record.
3.) You can use JPA instead of direct JDBC. This would require a bit of reading. JPA shields you from working with SQL directly and it generates the right SQL for the right database.
Apr 11 '13 #2
Thank you thank you thank you sir very much.

Would you mind sir, can you give me links for documents to study "ON DUPLICATE KEY UPDATE" and JPA.
I don't know how to use ON DUPLICATE KEY UPDATE and what is JPA.
Apr 11 '13 #3
r035198x
13,262 8TB
The two are not related. As I said above. ON DUPLICATE KEY UPDATE is a MySQL only clause so you only use it if you are using MySQL server database. If you use it then you use it with plain JDBC not with JPA.

JPA is an approach to data access that uses java objects to model database tables. If you are new to Java then learn JDBC first and only learn JPA when you have had lots of experience it.

I would suggest you use approach (2) from the suggestions I gave. The one where you run an update first and check the return value of execute update. If executeUpdate returns zero then no records where updated so you can run the insert. Also, if you are using the database to generate Ids for your records then you know if a record is new or not by checking if it has an Id or not.
Apr 11 '13 #4
Thank you very much. I tried your second suggestion and that solved my problem completely.

Thank you very much
Apr 17 '13 #5
r035198x
13,262 8TB
Well done! Don't forget the warning about two people doing the check at the same time. They will both try an update and get zero back at almost the same time then they will both try an insert which could duplicate the data or result in one of them failing. You can solve that by synchronizing the code that does the update and select so that only one thread will run it at a time.
Apr 17 '13 #6

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

Similar topics

2
by: OvErboRed | last post by:
Hi, I'm trying to determine whether a given URL exists. I'm new to Python but I think that urllib is the tool for the job. However, if I give it a non-existent file, it simply returns the 404 page....
3
by: Kelmen Wong | last post by:
Greeting, Anyone know any tool to identify whether an assembly is in Debug or Release? I can use "Dependency Walker" from VS 6. How about VS .NET? Got anything tool like that?
1
by: Swarna | last post by:
Hi all, I am trying to find out whether a particular directory is present on a remote machine or not from my local Python script in Linux . Can anyone help me with the command that i need to...
3
by: Gareth Tonnessen | last post by:
I need to have a clean way to determine whether a query exists without trying to open it and getting an error message. Is there a simple way to determine whether an object exists in a database?...
14
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
1
by: Hatrim | last post by:
How to identify whether the xml document is well-formed?
5
by: Revathi Balakrishnan | last post by:
i have used the below code to find out , whether the below named "rightnav" iframe is existing. But when i use this code it throws object required error. Expecting your help on this <script...
8
by: srig | last post by:
hi.. i hav created a form which gets empid as input and retrieve data from database and display information about employee.now wen i get empid as input i should check with d table tat empid shold...
1
by: devil of love | last post by:
write a program to identify whether a character entered by a user is a vowel or a consonant
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.