java - MySQL语法错误异常

标签 java mysql jdbc phpmyadmin

我刚刚开始学习 MySQL 和 JDBC。

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

这是我的类(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));
        }


    }

}

这里是异常(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)

谢谢你的帮助

最佳答案

MySQLSyntaxErrorException: Table 'testdb.name' doesn't exist

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

I created a table called testdb using phpmyadmin.

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

关于java - MySQL语法错误异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14311704/

相关文章:

java - 具有多个注释的方法的 AspectJ 切入点

java - 如何使用 JFrame 上的 JButtons 在 JTextField 上书写?

java - 如何在将记录插入数据库表Java之前检查非空字段

java - 强制 Oracle JDBC 在每个连接上使用代理

java - 如何使用 JOGL 处理并返回数组

java - 如何删除 Checkstyle 信息(导入 org.apache.log4j.Logger 的顺序错误)

MySQL:字段中的空白字符实际上不是空白。它是什么?

python - 使用 Django 将 Unicode 字符存储到 MySQL 时出现问题

mysql - SQL 如何在不使用 SQL 的情况下对来自列的任何 SQL 查询数据进行字符串替换

java - 编写使用数据库的桌面应用程序。有关如何管理用户对表的访问的建议?