简体   繁体   English

使用语句时MySQL 1064语法错误

[英]MySQL 1064 syntax error when using statement

I am running a server that receives queries and sends them to the Database using Statements. 我正在运行一台服务器,该服务器接收查询并使用语句将其发送到数据库。

try{
    connection = dbConnection.getDbConnection();
    if(connection != null) {
         System.out.println("DA2");

        Statement mySt = connection.createStatement();
        if(mySt != null) {
           ResultSet myRs = mySt.executeQuery(query);
           System.out.println("DA3");

           while(myRs.next()){
               //getting data and printing it
           }
        }

}

It prints DA2 so the connection is created succefully. 它会打印DA2,因此可以成功创建连接。 The query is send by the client in the following way 查询是通过以下方式由客户端发送的

DataOutputStream out = new DataOutputStream(client.getOutputStream());
String query = "USE db_name; SELECT * FROM `tb_name`;";
out.writeUTF(query);

I changed the database name with db_name and the table name with tb_name(I am sure I wrote them correctly in my code). 我用db_name更改了数据库名称,用tb_name更改了表名称(我确定我在代码中正确地写了它们)。

The server receives them this way 服务器以这种方式接收它们

Socket client = socket.accept();
DataInputStream input = new DataInputStream(client.getInputStream());
String query = input.readUTF();

When the server is running and the client sends the query an exception is thrown with the following message(I handled the exceptions to show me this). 当服务器正在运行并且客户端发送查询时,将引发异常,并显示以下消息(我已处理了异常以向我展示此异常)。

SQLState: 42000 Error Code: 1064 Message: You have an error in your SQL syntax; SQLState:42000错误代码:1064消息:您的SQL语法有一个错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM `tb_name`' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的'SELECT * FROM`tb_name`'附近使用

The same query runs correctly on a MySQL database. 同一查询在MySQL数据库上正确运行。

How can I solve this? 我该如何解决? Is the database sending back the error and so throwing an exception or is it just the code? 数据库是发回错误并因此引发异常还是仅仅是代码?

SOLVED: I forgot to specify the database name in the connection. 已解决:我忘了在连接中指定数据库名称。

您可以使用(具有限定名称的单个SQL语句):

String query = "SELECT * FROM db_name.`tb_name`";
public static Connection dbConnect() {
        Connection c = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            c = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME", "USERNAME", "PASSWORD");
        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException " + e);
        } catch (SQLException e) {
            System.out.println("SQLException " + e);
        }
        return c;
    }

Hope you have created dbConnect in similar fashion. 希望您以类似的方式创建了dbConnect。 Here We heve included the database name in get connection method So explicitly no need to use database name for each time until you are accessing another database. 在这里,我们已经将数据库名称包含在get连接方法中,因此明确地,每次访问另一个数据库之前,无需每次都使用数据库名称。

so your query should be 所以你的查询应该是

String query = "SELECT * FROM `tb_name`";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM