No of visitors who read this post: 458
Category: MySQL Server
Type: Question
Author: Anthony Reed
Your rating: None Average: 5 (20 votes)

 I am trying to connect to a MySQL server using a Java program. I always get this error when trying to connect:

SQL Exception: 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

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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

I'm lost and have no idea what else to do because I'm just a newbie. Please help. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

# Comment Id: 11807

Well Anthony,

It is better if you can tell us the code written to connect to the MySQL server and the IDE you are using, so we can contribute to correcting your code if there is some error within that.

Anyway I'll give you the check list and you can follow them if you miss something.

1) Download the JDBC driver for MySql and add it to your build path.

2) Check your code is something like below.

import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
  public static void main(String[] argv) throws Exception {

    String driver = "com.mysql.jdbc.Driver";

    Class.forName(driver);

    String connection = "jdbc:mysql://localhost:3306/YourDBName";
    String user = "root";
    String password = "root";    

    Connection con = DriverManager.getConnection(connection, user, password);

     if (!con.isClosed()) {

      con.close();
    }
  }
}

As your sql exception throws at the line 1 from your code I think, it is a error from the beginning. So try out using above code and connect to the server. If not please send us your code. I think we can do something together.

# Comment Id: 12371

Well there is a syntax error on your code. What is that question mark thing ('????????????????'). Did you use it? Did you use any variable kind of thing to make that connection string and that variable returns these question marks? Error says that there is a syntax error on your code.

It means a character or a string incorrectly placed in your code, which can be an instruction or command, that will cause a failure in execution. So all the syntax errors get recognized at compilation time. When a program throws syntax error exceptions, it says that an error of the language (In your case it is SQL) resulting from code that does not conform to the syntax of the programming language.

A common syntax error is not closing parenthesis (brackets), quotes, double quotes...Etc. So check your code and see if there are any unclosed parenthesis. If you use an IDE, it will show all the syntax errors on your code. I guess that you are using a notepad. So it is time to move on to a IDE now. Most popular IDEs for java is NetBeans and Eclipse/MyEclipse. Netbeans and Eclipse are freely available WYSIWYG editors and MyEclipse is a proprietary one so you need license. Try out those IDEs and you can see how easy they are to use. It will shows up all the syntax errors on your code. And you do not need to write your code all by yourself because most of the parts can auto generate.

And there is another issue which can be cause to this error. What kind of font are you using for writing your code? Are you using the system font or other kind of artistic font? It is better to use a common font like Tahoma or Courier font for code writing.

It is better if you can supply the answers for the above questions so we can support you in solving your problem.