Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 4th, 2003, 07:46 PM
Registered User
Join Date: Aug 2003
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.
Old August 14th, 2003, 07:30 AM
Authorized User
Join Date: Jun 2003
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).

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

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