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

Simply Query Problem

Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
Hello.

I am new to JDBC and have recently been able to successfully connect to an SQL database with a MySQL driver.

However I am confused as to how to extract the data from the database.

I created an object (as I had no idea what you get back) and then set the result of a query to equal this object.

However I got some massive linked list of nodes and none of it made any sence.

All I have is a single database with two names "bob" and "oscar" in it.

I used the query:

ResultSet result = select.executeQuery("SELECT name FROM Darren WHERE name=\"" + "Bob" + "\"");


Can some one please tell me what I am doing wrong.

All I want to do is ideally get this query to retun a String with the word "Bob" in it for me.

Thank you very much.

Darren
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

ResultSets are *sets* of objects and do not point to a record but a *set* of records. Think of it like opening a pointer to a shelf in a library. Whether there's one record or hundreds, initially the result set only points to the shelf.

In order to view the contents of the shelf you need to iterate over it using result.next(). Each time this is called the pointer is moved from book to book in order they were retrieved from the database and if result.next() ever returns false it means there are no more books. For your situation, if you are expecting exactly one result than you should have code like this:

[ January 18, 2007: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Malith Yapa
Ranch Hand

Joined: Jan 17, 2007
Posts: 45
You don't do all the flashy work like this
\"" + Bob + ""\
.

This would work fine

You dont have to use an escape character.

SQL data is case sensitive..
[ January 19, 2007: Message edited by: Malith Yapa ]
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
That makes sence now.

Cheers for the help, that was nicly explained for someone who has never done this before.

Cheers for the help on the returning the name bit, I did not realise that I did not need the " and / bits.

Darren
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Originally posted by Malith Yapa:
SQL data is case sensitive..

Actually, I believe case sensitivity is a feature in some databases and by default you can specify matches as being case sensitive and case insensitive.

Either way, the better form would be: For the most part "OR" should be avoided at all costs in SQL since they can seriously hurt performance if used poorly in joins.
[ January 20, 2007: Message edited by: Scott Selikoff ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simply Query Problem