【问题标题】:MySQLSyntaxErrorExceptionMySQLSyntaxErrorException
【发布时间】:2012-12-28 00:21:38
【问题描述】:

我刚刚开始学习 MySQL 和 JDBC。

我使用 phpmyadmin 创建了一个名为 testdb 的表。表只有 2 列,分别称为 first 和 last。当我尝试从我的 java 类连接数据库时,我得到 MySQLSyntaxError。但是我想不通。

这是我的课:

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));
        }


    }

}

这里是个例外:

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)

感谢您的帮助

【问题讨论】:

    标签: java mysql jdbc phpmyadmin


    【解决方案1】:
    MySQLSyntaxErrorException: Table 'testdb.name' doesn't exist
    

    错误几乎是描述性的。 testdb 架构中没有名称为“name”的表。

    我使用 phpmyadmin 创建了一个名为 testdb 的表。

    如果您创建了表testdb,那么它应该是select * from testdb。不是吗?

    【讨论】:

    • @AnılSelimSürmeli:不客气。很高兴它有帮助。祝你好运。
    【解决方案2】:

    您已经创建了一个名为 testdb 的表,因此您的查询应该是

    select * from testdb 
    

    不是select * from name

    你真的应该检查你的堆栈跟踪。

    【讨论】:

      猜你喜欢
      • 2012-04-20
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2013-11-20
      • 2016-07-07
      • 2015-01-22
      相关资源
      最近更新 更多