【发布时间】:2011-05-19 16:58:59
【问题描述】:
我正在尝试使用 Java 连接到 MySQL 数据库,但出现以下错误:
SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
我无法理解错误并在网上搜索了很多但没有找到任何东西。这是我正在使用的代码:
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception E)
{
System.err.println("Unable to load driver");
E.printStackTrace();
}
try
{
Connection C = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/DATABASE_NAME","USERNAME","PASSWORD");
Statement Stmt = C.createStatement();
ResultSet RS = Stmt.executeQuery("SELECT somefield FROM sometable");
while (RS.next())
{
System.out.print("\"" + RS.getString(1) + "\"");
System.out.print(" by " + RS.getString(2));
System.out.println(": " + RS.getString(3));
}
C.close();
RS.close();
Stmt.close();
}
catch (SQLException E)
{
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}
上面的 SQL 查询是该问题的一个示例。我正在使用的那个在 MySQL 控制台中没有任何问题。事实上,即使我从上面的代码中删除查询和语句,我仍然会得到同样的错误。 有人可以帮忙吗? 提前致谢
【问题讨论】:
-
从哪一行抛出异常(在你的捕获中,添加一个 E.printStackTrace())?另外,请尝试在查询末尾添加分号,例如"从表中选择字段;"
-
您好,感谢您的评论,我也尝试添加分号,但错误没有区别