Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By doWhile

Thread: SQL query in Java

  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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:

    Java Code:
    SELECT * FROM commands WHERE category = helloworld
    But I get error:

    Java Code:
    Unknown column 'helloworld' in 'where clause'
    I also tried:

    Java Code:
    SELECT * FROM commands WHERE category LIKE helloworld
    Same error message.

    I am coming from PHP, where the first method, with "=" operator, works. Is it different in Java?

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default 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`

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: SQL query in Java

    I changed it to:

    Java Code:
    SELECT * FROM `commands` WHERE `category` = `helloworld`
    And still get the same error.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: SQL query in Java

    Place quotes around the item you are selecting ( eg helloworld). That being said, I'd recommend using a PreparedStatement.
    Eleeist likes this.

  5. #5
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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

  1. Java DAO create query please help
    By alvin0618 in forum XML
    Replies: 5
    Last Post: 03-31-2011, 07:05 AM
  2. Java and appengine query?
    By Emeljats in forum New To Java
    Replies: 0
    Last Post: 12-12-2010, 10:06 PM
  3. java servlet/Query
    By KumbhaniMehul in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2010, 08:19 PM
  4. Help with query (new to java!)
    By gpittingale in forum New To Java
    Replies: 3
    Last Post: 02-15-2009, 09:14 AM
  5. query on core java
    By venkatallu in forum New To Java
    Replies: 1
    Last Post: 09-01-2008, 02:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •