code in jdbc which checks whether the data is already present in mysql db

Hello All,I'm a newbie to mysql and jdbc...I want to check whether the data already exists in the table.The data is obtained from UI.If the data is not present I need to insert the data into database,pl help me h to do it.Thank you for your time
[267 byte] By [erikaa] at [2007-11-26 13:55:15]
# 1
Please be more specific. What are you having trouble with?Getting data out of the result set? Inserting data? etc?
zadoka at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thank you for your reply.I'm developing a UI using struts which will get the emailid from the user,I've set emailid as primary key in mysql db.So if the same emailid is given in the database it'll throw error.This can be viewed in mysql command line client.

But how to do this writing code.I need to check whether the same emailid exists in db,if it doesn't I should insert it in the user_table otherwise in the existing_user_table.

I know its simple ,pl help me

erikaa at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> But how to do this writing code.I need to check

> whether the same emailid exists in db,if it doesn't I

> should insert it in the user_table otherwise in the

> existing_user_table.

Sounds simple. Select to see if that id exists. If it doesn't then insert.

What part are you having problems with? You need to be specific about the code you are having trouble with. (I don't want to explain everything if you understand how to do 90% of it).

zadoka at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
Basically you use a SELECT statement to get the records you think might be in the database. If it returns one or more records then you know they are in the database. If it returns zero records then you use an INSERT statement to insert the data.
DrClapa at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

thanks for your time..I know that it will return zero if there are no records,i can understand the whole thing but 2 write the code is the problem,what code should i write to know whether it returns zero,ism having problem there..I'm 95% over but this 5% is driving me nuts..its simple..but I'm not able 2 do it...pl help me

erikaa at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
........ResultSet rs = stmt.executeQuery("select * from table where emailid='mail_id'");while(rs.next()){// Your code if email id already exists...}Is this what u r asking for?
kinnareea at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

yes,my project will be over if I'm done with this...If my email_id already exists I need to insert into existing_user table..if the email id is new i should insert into the user_table...i need the code..I'm using jdbc in struts frame work .this is my first project...pl help me..I'm a newbie to all this

erikaa at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

> yes,my project will be over if I'm done with

> this...If my email_id already exists I need to insert

> into existing_user table..if the email id is new i

> should insert into the user_table...i need the

> code..I'm using jdbc in struts frame work .this is my

> first project...pl help me..I'm a newbie to all this

i m not much familiar with struts but as far as JDBC is concerned next() returns a boolean value... so u can just check using this method that your email id already exists or not and if it already exists u just insert it into your existing table...

.....

.....

ResultSet rs = stmt.executeQuery("select emailid from table where mailid='mailid_user_enters'");

if(rs.next())

{

//if mail id already present

stmt.executeUpdate("insert into ...."); // insert into already existing_user table

}

else

{

//if mail id doesn't exist

stmt.executeUpdate("insert into ..."); // insert into the user-table

}

i m not very clear what u exactly r asking for but may b this can help u...

kinnareea at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
Yes that would work fine...ThanksUday
uday.dva at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
Thank you Kinnare..the code works...I did so many complicate things to check this...Thanks a lotThank you all who spared your valuable timeThank you all
erikaa at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11
Thanks Kinnare,Thanks for the help and support...The code works fine....Thank you all for the valuable time spared
erikaa at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12
ya the code works...thank you all fro you valuable time...
erikaa at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 13
its ok... anytime.. :-)
kinnareea at 2007-7-8 1:34:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...