aspose file tools
The moose likes JDBC and the fly likes Connecting with MYSQL Database Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply 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: 102

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: 16483
    
    2

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: 102

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: 16483
    
    2

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: 102

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: 16483
    
    2

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: 102

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: 16483
    
    2

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

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Connecting with MYSQL Database
 
Similar Threads
exception
Transactions and Threading
problem with bulk update and auto_increment
Need to update date from jsp to mysql database.
dealing with non ascii characters