有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Debian上使用Java连接到MySQL

我正在尝试使用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控制台工作没有任何问题。事实上,即使我从上面的代码中删除了查询和语句,我仍然会得到相同的错误。 有人能帮忙吗? 提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    很可能是因为你正在使用 从表中选择字段-表是SQL保留字。如果要执行此查询,可能需要执行以下操作

    select field from `table` 
    

    将单词表放在后面