473,586 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Gobindap
25 New Member
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
5 3141
r035198x
13,262 MVP
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
Gobindap
25 New Member
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 MVP
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
Gobindap
25 New Member
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 MVP
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
4334
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. Aside from grepping this for '404', is there a better way to do this? (Preferrably, there is a solution that can be applied to both HTTP and FTP.)...
3
1832
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
2172
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 issue to os.system in my python script to acheive this? Thanks, for all your time !
3
13670
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? Remove obvious from e-mail address. Gareth Tonnessen LuvTruth@att.netNOJUNK
14
2934
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 'null-terminated-string' to some functions, such as 'atoi'. (We assume that the string was extracted using an array, not string literal) This program shows blank for...
1
882
by: Hatrim | last post by:
How to identify whether the xml document is well-formed?
5
1269
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 lanquage="Javascript"> if(window.document.getElementById('1').title=="rightnav") alert("got it");
8
5109
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 exist. if does not exist i should display a message.. can anyone suggest me d code or idea how can i do it.
1
2279
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
8204
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7965
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6617
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3838
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.