Tech ARP Forums

Go Back   Tech ARP Forums > Software Discussion > General Software
Register
FAQ Members List Calendar Arcade Mark Forums Read

Google Web www.techarp.com forums.techarp.com

General Software This is the forum for general discussions about software.

Reply
 
LinkBack Thread Tools
Old 7th Nov 2005, 05:55 PM   #1 (permalink)
I'm a regular
 
Join Date: 12 Aug 2005
Posts: 269
Reputation: 122
vien will become famous soon enoughvien will become famous soon enough
Rep Power: 5
Default Not Exists SQL statement error

I came across this error when trying to use the "NOT EXISTS" statement in my SQL query. Crap, I have double checked with the codes I found on the internet but still to no avail

this is the error generated by Eclipse, FYI I'm implementing MySQL inside my Java program:
Quote:
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where not exists (select * from commitee_details where peoplewise_id = 112233) v' at line 1
This is the code that causes the error:
Quote:
String sql2 = "insert into commitee_details (peoplewise_id, name) where not exists (select * from commitee_details where peoplewise_id = 112233) values (112233, 'jim');" ;
stmt.execute(sql2);
__________________
-if love is like butterfly, then unwanted love is like a bunch of flies-
vien is offline   Reply With Quote
SPONSOR

Old 9th Nov 2005, 11:16 AM   #2 (permalink)
I'm a regular
 
Join Date: 12 Aug 2005
Posts: 269
Reputation: 122
vien will become famous soon enoughvien will become famous soon enough
Rep Power: 5
Default

Used a different approach to avoid the NO EXISTS sql error. well at least I'm still getting the results I wanted but use a couple lines of code instead of one

I'm trying to insert values into a table where the insertion will include a Primary Key value. Therefore the program needs to check whether the PK value has existed inside the table or not, else it will cause an Duplicate value error. These are the codes I used:
Quote:
//checks whether the peoplewise_id already exists inside commitee_details table
String sql5 = "select peoplewise_id from commitee_details where peoplewise_id = 112233;";

ResultSet result = stmt.executeQuery(sql5);

if (result.next() != true) {
//if no rows are returned from the sql5 statement then do the insert statement below
String sql2 = "insert into commitee_details (peoplewise_id, name)values (112233, 'jim');";
stmt.execute(sql2);
}
__________________
-if love is like butterfly, then unwanted love is like a bunch of flies-
vien is offline   Reply With Quote
Old 20th Nov 2005, 08:25 PM   #3 (permalink)
I'm a regular
 
fyire's Avatar
 
Join Date: 15 Jan 2004
Location: Somewhere out there
Posts: 350
Reputation: 15
fyire is on a distinguished road
Rep Power: 5
Default

u can try to just insert into the table without checking anyways. U'll get an SQLException raised when there's a PK conflict. when you get an exception, here's how you handle it:

SQLException has a method that allows u to get the exception code for the exception raised. this is vendor specific, so you will nee dto check the mysql documentation on the error codes for their JDBC driver. if it the error code returned matches the error code for PK conflict, then you know that the error is caused by that, so you can handle it accordingly.

alternatively, you can use hibernate (www.hibernate.org) do handle ya database handling work without you needing to do a single line of SQL
fyire is offline   Reply With Quote
Old 23rd Nov 2005, 09:48 AM   #4 (permalink)
I'm a regular
 
Join Date: 12 Aug 2005
Posts: 269
Reputation: 122
vien will become famous soon enoughvien will become famous soon enough
Rep Power: 5
Default

I did enter all the codes above into try catch block, if I don't Eclipse will generate a compile error anyway I can't have SQLExceptions else I will not be able to do all inserts required. So I really must check before inserting into the table.

Is hibernate a freeware? I'm doing it for my company so I can't use products without license
__________________
-if love is like butterfly, then unwanted love is like a bunch of flies-
vien is offline   Reply With Quote
Old 25th Nov 2005, 09:21 AM   #5 (permalink)
I'm a regular
 
fyire's Avatar
 
Join Date: 15 Jan 2004
Location: Somewhere out there
Posts: 350
Reputation: 15
fyire is on a distinguished road
Rep Power: 5
Default

Quote:
Originally Posted by vien
I did enter all the codes above into try catch block, if I don't Eclipse will generate a compile error anyway I can't have SQLExceptions else I will not be able to do all inserts required. So I really must check before inserting into the table.

Is hibernate a freeware? I'm doing it for my company so I can't use products without license

actually, that's the beauty of try/catch blocks. in the try, do place the code that might fail (such as the insertion that may have the PK violation), then you clean up any messes caused by errors inside the catch block, and in the finally block, that's where it all comes to an end, such as commiting the series of sql statements that's been done and so on.

yup, hibernate is free
fyire is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +8. The time now is 07:52 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 1998-2007 Tech ARP. All rights reserved.