sweet dreams sweet dreams - 6 months ago 13
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 ?

Code:

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";
try
{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Business", username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(myQuery);

while(rs.next())
{
System.out.print(rs.getString("customerno"));
}

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

}

}

Answer

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";