• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Simply Query Problem

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic