sweet dreams sweet dreams - 2 years ago 176
SQL Question

MySQL exception - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

I get the error:

'Unknown column 'customerno' in 'field list' '.

But, that column exists in my customer table. Then why am I getting this exception ?


import java.sql.*;

public class Classy {

static String myQuery =
"SELECT customerno, name" +
"FROM customers;";

public static void main(String[]args)
String username = "cowboy";
String password = "1234567";
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Business", username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(myQuery);


} catch(SQLException ex){System.out.println(ex);}



Answer Source

the problem with your syntax is that you have no space between name and FROM

String myQuery =   
        "SELECT customerno, name" +  // problem is here
        "FROM customers;"; 

instead add a space after name

 String myQuery =   
        "SELECT customerno, name " +  // add space here
        "FROM customers"; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download