Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL help!

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to the JDBC forum, where this question is more on-topic. Then I'm going to answer it...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your tablename is a string and needs to be enclosed in ''.
Example:
String sql = "SELECT * FROM '" + table + "'";
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried that but it doesn't work!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're still stuck, why not elaborate on "doesn't work." What sort of error message do you get?
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
String query = "SELECT * FROM " + myTextField.getText();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic