aspose file tools*
The moose likes JDBC and the fly likes SQL help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL help!" Watch "SQL help!" New topic
Author

SQL help!

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
How do you use a variable as part of an SQL statment?
For example I am asking the program to get all the fields from the table that the usre has specified. The tablename is stored in the variable table. I have tried the following but that does not work:
String query = "SELECT * FROM " + table;
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I'm moving this to the JDBC forum, where this question is more on-topic. Then I'm going to answer it...


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This should work fine. You should log the assembled "query" string to make sure you're getting what you expect; maybe you forgot the space before the table name?
If you're actually assembling parameters rather than table names, what you want to do is use the java.sql.PreparedStatement class. It takes care of all the quoting, escaping and other magic needed to plug variables into an SQL query.
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
your tablename is a string and needs to be enclosed in ''.
Example:
String sql = "SELECT * FROM '" + table + "'";


An egg is a chicken's house!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Jason Steele:
your tablename is a string and needs to be enclosed in ''.
Example:
String sql = "SELECT * FROM '" + table + "'";

I don't believe this is correct. If 'table' were the name of a parameter then yes. But the Table name part of an SQL Statement does not require ' ' around it.


GenRocket - Experts at Building Test Data
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I have tried that but it doesn't work!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you're still stuck, why not elaborate on "doesn't work." What sort of error message do you get?
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
It's Okay its started working - with '' nothing was happening (and I had no error message)but using this works:
String query = "SELECT * FROM " + table + "";
Thank-You for all your help!
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Errrmmmm... What is the difference between:
String query = "SELECT * FROM " + table + "";
and
String query = "SELECT * FROM " + table;
Can you give us previous errors? I'm really want to know. BTW, what database do you use?
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
Originally posted by Gregg Bolinger:

I don't believe this is correct. If 'table' were the name of a parameter then yes. But the Table name part of an SQL Statement does not require ' ' around it.

Correct...I was assuming that since he separated the tablename from the rest of the statement that it was a parameter.
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
There were no errors as such when I used my original statement- the only thing was that the table wasn't being displayed and thus suggesting that it had not been connected to but after I tried the ammendments (as mentioned in earlier reply) the table was displayed.
I am using Access Databases.
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Hi, I know i said it was working fine but I have changed some of my code so that it is better. As a result the variable being used to carry the name of the table is no longer a String because I am now using a JTextField- so now it doesn't work! I have tried to cast to String but you can't do this. At present my code is :
String query = "SELECT * FROM " + tableName + "";
Any suggestions???
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
Try:
String query = "SELECT * FROM " + myTextField.getText();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQL help!