3 Replies - 212 Views - Last Post: 27 April 2011 - 12:47 PM Rate Topic: -----

#1 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 01-February 09

sql statement error when retrieving data by jdbc

Posted 27 April 2011 - 07:19 AM

Hi guys,

i have this code

        try {
            String url = "jdbc:mysql://localhost/cyclingclub";
            java.sql.Connection  conn = DriverManager.getConnection(url,"root","milan");
            java.sql.Statement stmt = conn.createStatement();
            ResultSet rs;
 
            rs = stmt.executeQuery("SELECT * FROM BicycleDetail");
            while ( rs.next() ) {
                String mdl = rs.getString("model");
                String make = rs.getString("make");
                String typ = rs.getString("type");
                System.out.println(mdl);
                System.out.println(make);
                System.out.println(typ);
                System.out.println("----------------------------");
                        
            }
            conn.close();
        } catch (Exception e) {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }



--this code prints out for me the model/make/type of bicycle in my db...now i want to extract only one make of bicycles, but when the sql statement is modified like this to extract from db only bmx's an error saying incorrect sql stment occurs:
rs = stmt.executeQuery("SELECT * FROM BicycleDetail WHERE make = bmx ");





can pls some one guide me what i'm doing wrong in this?

Thanks.....

Is This A Good Question/Topic? 0
  • +

Replies To: sql statement error when retrieving data by jdbc

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,329
  • Joined: 15-February 11

Re: sql statement error when retrieving data by jdbc

Posted 27 April 2011 - 07:29 AM

Try this

rs = stmt.executeQuery("SELECT * FROM BicycleDetail WHERE make = 'bmx'"); //note the single quotes

Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,788
  • Joined: 20-September 08

Re: sql statement error when retrieving data by jdbc

Posted 27 April 2011 - 08:34 AM

Consider using a PreparedStatement - it will handle all quoting for you
Was This Post Helpful? 2
  • +
  • -

#4 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 01-February 09

Re: sql statement error when retrieving data by jdbc

Posted 27 April 2011 - 12:47 PM

Thanks guys,

'bmx' (single quotes) worked....prepared statement is also a more efficient way to work with
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1