java MySQL语法错误异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14311704/
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
MySQLSyntaxErrorException
提问by skynyrd
I've just started to learn MySQL and JDBC.
我刚刚开始学习 MySQL 和 JDBC。
I created a table called testdb using phpmyadmin. Table just has 2 columns called first and last. When I am trying to connect the database from my java class, I get MySQLSyntaxError. However I could not figure it out.
我使用 phpmyadmin 创建了一个名为 testdb 的表。表只有 2 列,分别称为 first 和 last。当我尝试从我的 java 类连接数据库时,出现 MySQLSyntaxError。但是我想不通。
Here is my class:
这是我的课:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/testdb";
//Accessing driver from the JAR file.
Class.forName("com.mysql.jdbc.Driver");
//Creating a variable for the connection "con"
Connection con = DriverManager.getConnection(url,"root","password");
//Here is the query
PreparedStatement statement = con.prepareStatement("select * from name");
//Execute query
ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(1) + " " + result.getString(2));
}
}
}
And here is the exception:
这是例外:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'testdb.name' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at Main.main(Main.java:22)
Thank you for your help
谢谢您的帮助
回答by kosa
MySQLSyntaxErrorException: Table 'testdb.name' doesn't exist
error is pretty much descriptive. There is not table with name "name" in testdb
schema.
错误几乎是描述性的。testdb
架构中没有名称为“name”的表。
I created a table called testdbusing phpmyadmin.
我使用 phpmyadmin创建了一个名为testdb的表。
If you have created table testdb
then it should be select * from testdb
. isn't it?
如果您已经创建了表,testdb
那么它应该是select * from testdb
. 不是吗?
回答by KyelJmD
You've created a table named testdbtherefore your query should be
您已经创建了一个名为testdb的表,因此您的查询应该是
select * from testdb
not select * from name
不是 select * from name
you should really check your stacktraces.
你真的应该检查你的堆栈跟踪。