MySQL 1064 syntax error when using statement

April 01, 2018, at 00:08 AM

I am running a server that receives queries and sends them to the Database using Statements.

    connection = dbConnection.getDbConnection();
    if(connection != null) {
        Statement mySt = connection.createStatement();
        if(mySt != null) {
           ResultSet myRs = mySt.executeQuery(query);
               //getting data and printing it

It prints DA2 so the connection is created succefully. 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`;";

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).

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; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM `tb_name`' at line 1

The same query runs correctly on a MySQL database.

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.

Answer 1

You could use (single SQL statement with qualified name):

String query = "SELECT * FROM db_name.`tb_name`";
How to open a chrome window using a java program in a full screen mode/size?

How to open a chrome window using a java program in a full screen mode/size?

I am working on a program that opens multiple URLs in multiple chrome windows where I am supposed to take the screenshots of those windows and write to a fileSo far I am able to open the URLs in different chrome windows but I don't know how to open those windows...

Derby WHERE clause with Timestamp

Derby WHERE clause with Timestamp

I have an input field in which the user enters date (yyyy-mm-dd), and using this date, I would like to extract all email records from the database that were sent on that dayI convert the entered date from String to Timestamp and execute the query

Why doesn't Java allow generic subclasses of Throwable?

Why doesn't Java allow generic subclasses of Throwable?

According to the Java Language Sepecification, 3rd edition:

Spring Social vs. Facebook4j [on hold]

Spring Social vs. Facebook4j [on hold]

I need some info about these two API'sDo you know any differences between springSocial Facebook and Facebook4j?