Posted by  Ashish Pandey
 2608  View(s)
Rate this:

I've just started to learn MySQL and JDBC.

I created a table called testdb using phpmyadmin. Table just has 2 columns called first and last. When I am trying to connect the database from my java class, I get MySQLSyntaxError. However I could not figure it out.

Here is my 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.
        //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( {
            System.out.println(result.getString(1) + " " + result.getString(2));

And here is the exception:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table '' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at com.mysql.jdbc.Util.handleNewInstance(
    at com.mysql.jdbc.Util.getInstance(
    at com.mysql.jdbc.SQLError.createSQLException(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.sendCommand(
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
    at com.mysql.jdbc.ConnectionImpl.execSQL(
    at com.mysql.jdbc.PreparedStatement.executeInternal(
    at com.mysql.jdbc.PreparedStatement.executeQuery(
    at Main.main(

Thank you for your help

  1. Re: MySQLSyntaxErrorException

    Hey Ashish Pandey!

    You've created a table named testdb therefore your query should be
    select * from testdb
    not select * from name
    you should really check your stacktraces.