java MySQL 异常 - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11594157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL exception - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
提问by sweet dreams
I get the error:
我收到错误:
'Unknown column 'customerno' in 'field list' '.
'字段列表'中的未知列'customerno'。
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);}
}
}
回答by Jon Skeet
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...
现在你能看出哪里出了问题吗?我很惊讶它抱怨customerno
而不是缺少FROM
零件......
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";
回答by John Woo
the problem with your syntax is that you have no space between name
and FROM
你的语法问题是你name
和和之间没有空格FROM
String myQuery =
"SELECT customerno, name" + // problem is here
"FROM customers;";
instead add a space after name
而是在后面添加一个空格 name
String myQuery =
"SELECT customerno, name " + // add space here
"FROM customers";
回答by Aaron
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())
{
//Try and change this with the numeric value that is present in the database, e.g if it's column 2 do something like rs.getString(1);
//尝试使用数据库中存在的数值更改它,例如,如果它是第 2 列,则执行类似 rs.getString(1) 的操作;
System.out.print(rs.getString("customerno"));
}
}catch(SQLException ex){System.out.println(ex);}
}
}