• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simply Query Problem

 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Malith Yapa
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic