MySQL exception - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
10,388
Solution 1
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";
Solution 2
Look at what your query really is. This:
static String myQuery =
"SELECT customerno, name" +
"FROM customers;";
is equivalent to:
static String myQuery = "SELECT customerno, nameFROM customers;";
Now can you see what's wrong? I'm surprised it complained about customerno
rather than the lack of a FROM
part...
Note that I suspect you don't want the ;
either. I'd write it all one one line just for readability, when you can, as well as limiting the accessibility and making it final:
private static final String QUERY = "SELECT customerno, name FROM customers";
Author by
sweet dreams
Life can be very beautiful, only if we choose to make it so.
Updated on June 05, 2022Comments
-
sweet dreams almost 2 years
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);} } }
-
sweet dreams almost 12 yearsThanks ! I did not know that this kind of thing would not be handled by the API itself.
-
Jon Skeet almost 12 years@sweetdreams: How could it be? You've just got a string - how is it meant to know that when you wrote "nameFROM" you meant "name FROM"? That's asking a bit much of an API...