Results 1 to 3 of 3
  1. #1
    mike_ledis is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default Problem executing SELECT query

    Hello. Create the following database with phpmyadmin

    CREATE TABLE course
    (
    c_id VARCHAR(8) NOT NULL PRIMARY KEY,
    c_name VARCHAR(40) NOT NULL,
    c_ects SMALLINT NOT NULL
    ) ;

    insert into course values ('c1', 'Programming 1', 2) ;
    insert into course values ('c2', 'Programming 2', 2) ;
    insert into course values ('c3', 'Databases 1', 3) ;
    insert into course values ('c4', 'System analysis', 3) ;
    insert into course values ('c5', 'Local Area Networks', 3) ;

    I made the following method to retrieve the rows by c_id
    public static void displayCourse() throws SQLException
    {
    Statement stmt ;
    ResultSet rs ;

    stmt = dbConnection.createStatement () ;
    rs = stmt.executeQuery ("SELECT c_id, c_name, c_ects FROM course WHERE c_id= "+"c1") ;
    while (rs.next ())
    System.out.println (rs.getString ("c_id") + " " +
    rs.getString ("c_name") + ", ects=" +
    rs.getInt ("c_ects")) ;
    rs.close () ;
    stmt.close () ;
    }
    When i run the method i get the following message
    Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: Unknown column 'c1' in 'where clause'

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

    Default

    You need to quote text values in your query
    SQL WHERE Clause

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Or better use a PreparedStatement and it will quote everything for you when you set the parameters. It also guards against sql injection.

Similar Threads

  1. SELECT FROM WHERE query
    By herfnai in forum JDBC
    Replies: 3
    Last Post: 11-24-2008, 11:04 AM
  2. problem in select query
    By herfnai in forum JDBC
    Replies: 0
    Last Post: 08-17-2008, 11:47 AM
  3. Replies: 2
    Last Post: 07-18-2008, 01:13 PM
  4. Error When Executing Query
    By radz in forum New To Java
    Replies: 5
    Last Post: 06-26-2008, 03:37 AM
  5. Using a variable in a SELECT FROM WHERE query
    By cplmckenzie in forum JDBC
    Replies: 12
    Last Post: 04-23-2008, 03:24 AM

Posting Permissions

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