Results 1 to 5 of 5
Thread: SQL query in Java
- 01-22-2012, 08:45 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
SQL query in Java
Hi,
I have small problems with SQL queries in Java. I try to select all rows where the "category" column equals "helloworld". At first I tried like this:
But I get error:Java Code:SELECT * FROM commands WHERE category = helloworld
I also tried:Java Code:Unknown column 'helloworld' in 'where clause'
Same error message.Java Code:SELECT * FROM commands WHERE category LIKE helloworld
I am coming from PHP, where the first method, with "=" operator, works. Is it different in Java?
- 01-22-2012, 08:47 PM #2
Re: SQL query in Java
try to put '`' when you are using a string in your WHERE thing.
Try this: SELECT * FROM `commands` WHERE category = `helloworld`
- 01-22-2012, 08:51 PM #3
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: SQL query in Java
I changed it to:
And still get the same error.Java Code:SELECT * FROM `commands` WHERE `category` = `helloworld`
- 01-22-2012, 09:13 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: SQL query in Java
Place quotes around the item you are selecting ( eg helloworld). That being said, I'd recommend using a PreparedStatement.
- 01-22-2012, 10:14 PM #5
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: SQL query in Java
Thanks doWhile! I rewrote the code to use PreparedStatement and it started to work. Here is the new code:
Java Code:public void getCommands(String category) throws Exception { Connection connection = connectToDatabase(); PreparedStatement prest; String query = "SELECT * FROM commands WHERE category = ?"; prest = connection.prepareStatement(query); prest.setString(1,"helloworld"); ResultSet queryResult = prest.executeQuery(); while (queryResult.next()) { String outputString = queryResult.getString("command"); System.out.println(outputString); } }
Similar Threads
-
Java DAO create query please help
By alvin0618 in forum XMLReplies: 5Last Post: 03-31-2011, 07:05 AM -
Java and appengine query?
By Emeljats in forum New To JavaReplies: 0Last Post: 12-12-2010, 10:06 PM -
java servlet/Query
By KumbhaniMehul in forum Java ServletReplies: 0Last Post: 04-06-2010, 08:19 PM -
Help with query (new to java!)
By gpittingale in forum New To JavaReplies: 3Last Post: 02-15-2009, 09:14 AM -
query on core java
By venkatallu in forum New To JavaReplies: 1Last Post: 09-01-2008, 02:23 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks