aspose file tools*
The moose likes JDBC and the fly likes Connecting with MYSQL Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting with MYSQL Database" Watch "Connecting with MYSQL Database" New topic
Author

Connecting with MYSQL Database

Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Hello, Im trying to do this:

I have application connecting and getting data from Mysql database.

And i have problem to do this:



Ok, it works, but only if there is field_name2 with valueX

So when there isnt i would execute new statement with INSERT.

I know how to get data from mysql and so on, i just would request some help with already mentioned problem, check if field with given value exist, if yes update it with X in field Y, if not insert new ready entry with proper needed data.

Thanks in advance!

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Mathew Mintalm wrote:check if field with given value exist, if yes update it with X in field Y, if not insert new ready entry with proper needed data.


Yes, that's correct, that's how you would do that. Did you have some question about it?
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Thanks for reply

so i have to connect with database and use select statement, with try and catch exception, and in catch exception i should execute void which will create new entry (in case of exception)

?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

I don't see where exceptions would come into the process. Do a SELECT which looks for the record which may or may not be there; if that query returns zero rows then it isn't there, so do an INSERT. If the query returns a row, then do an UPDATE.
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Ok i guess i understand, could you please only show me this part of the code:

Do a SELECT which looks for the record which may or may not be there; if that query returns zero rows


?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

I'm sorry... do you really not know how to execute a SELECT statement and read a row from it? That's usually the first example in any of the tutorials I ever saw.
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

I said i know, but i couldnt really example what did you mean with my previously quoted sentence.

do you meant something like that?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

No. You actually have to try to read a row from the ResultSet.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting with MYSQL Database