我得到错误:
'Unknown column 'customerno' in 'field list' '.
但是,该列存在于我的客户表中。那为什么我会得到这个异常?
代码:
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);}
}
}
最佳答案
看看您的查询到底是什么。这:
static String myQuery =
"SELECT customerno, name" +
"FROM customers;";
相当于:
static String myQuery = "SELECT customerno, nameFROM customers;";
现在你能看出哪里出了问题吗?我很惊讶它提示 customerno
而不是缺少 FROM
部分...
请注意,我怀疑您也不想要 ;
。如果可以的话,我会把它全部写成一行只是为了可读性,同时限制可访问性并使其成为最终版本:
private static final String QUERY = "SELECT customerno, name FROM customers";
关于java - MySQL 异常 - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11594157/