Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 4th, 2003, 07:46 PM
Registered User
Join Date: Aug 2003
Location: Zion, IL, USA.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to wslyhbb Send a message via MSN to wslyhbb Send a message via Yahoo to wslyhbb
Default help needed with a SQL select statement problem

I am using Java to access information in a MySQL database, I created the following PreparedStatement:
PreparedStatement selectCurrentValue = databaseConnection.prepareStatement("SELECT ? FROM cardCatalog WHERE callNumber = ?");
Then I do (where "title" is the name of a column in the table, and callNumber.getText(), callNumber is a JTextField variable, .getText() gets the text from the text field in my GUI:
selectCurrentValue.setString(1, "title");
selectCurrentValue.setString(2, callNumber.getText());
ResultSet rs = selectCurrentValue.executeQuery();
      Vector values = new Vector(1);
      // create a vector to hold the results
      Vector vector = new Vector(1);
      // iterate through records
         // make sure values is empty
         // iterate through columns
         for(int i = 1; i <= 1; i++)
         // trim vector to size
         // add vector to vector
      // trim vector to size
      String currentValue = ((Vector)resultVector.elementAt(0)).elementAt(0).t oString();
After all of this executes, currentValue is "title", but that is not what should happen, it should have retrieve the information from column named title in the database. However, I can execute:
PreparedStatement selectCurrentValue = databaseConnection.prepareStatement("SELECT title FROM cardCatalog WHERE callNumber = ?");
selectCurrentValue.setString(1, callNumber.getText());
and gets the current value out of the result set as I did above I get "Java Security Handbook" which is the value stored in column title in the database for the call number I enter. Therefore, the callNumber.getText(); statement is obviously executing correctly, but why the other statement returns whatever string I pass the first question mark, I do not understand. What do I need to do to get it to work the way it should? What am I doing wrong? The goal is that I could pass it the column "title" or "author" or any column I want to get the current value in the database of. If anyone can help, it would be greatly appreciated. Thanks in advanced.
  #2 (permalink)  
Old August 14th, 2003, 07:30 AM
Authorized User
Join Date: Jun 2003
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to jwenting Send a message via MSN to jwenting

Quite simple. You are returning a fixed string with the value "title" for each row in the database where your call number is the one you entered.

You should do something like:
    PreparedStatement selectCurrentValue = databaseConnection.prepareStatement("SELECT * FROM cardCatalog WHERE callNumber = ?");
    selectCurrentValue.setString(1, callNumber.getText());
    ResultSet rs = selectCurrentValue.executeQuery();
     String title = null;
        title = rs.getString(title);
     else title = "";
     String currentValue = title;
no need for Vectors here, your code is overly complicated as you'll only ever want a single result (and expect no more than 1).

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Construct a SQL Select Statement ricespn Beginning VB 6 1 May 28th, 2007 07:57 AM
SQL select statement problem shrisangeeta Classic ASP Databases 3 June 15th, 2006 10:28 AM
YYYY and MM in access' SQL select statement Condor76 Access VBA 1 February 15th, 2006 02:47 PM
SQL Select problem (subquery needed??) deian SQL Language 7 February 3rd, 2004 02:40 PM
ASP sql select statement scorpio Classic ASP Databases 5 July 30th, 2003 06:48 AM

All times are GMT -4. The time now is 07:27 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.