Solved MySQL syntax error but why

Discussion in 'Plugin Development' started by bennie3211, May 1, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    bennie3211

    Hello,

    I'm trying to check if a player is placed inside the database, but it gives me an syntax error and I can't figure out why it gives this error. If I check it online with w3schools it works perfectly, but when I tried it in Java it gives me the error:

    Code:
    [14:44:37 INFO]: com.mysql.jdbc.JDBC4PreparedStatement@31a5fc57: SELECT * FROM 'playerdata' WHERE UUIdentifier='a89dab9a
    -503e-4be0-a7c9-9380d2638f5a';
    [14:44:37 WARN]: com.mysql.jdbc.exceptions.jdbc4.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 ''playerdata' WHERE UUIdentifi
    er='a89dab9a-503e-4be0-a7c9-9380d2638f5a'' at line 1
    [14:44:37 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [14:44:37 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    [14:44:37 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [14:44:37 WARN]:        at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [14:44:37 WARN]:        at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    [14:44:37 WARN]:        at com.mysql.jdbc.Util.getInstance(Util.java:382)
    [14:44:37 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    [14:44:37 WARN]:        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
    [14:44:37 WARN]:        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
    [14:44:37 WARN]:        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
    [14:44:37 WARN]:        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
    [14:44:37 WARN]:        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
    [14:44:37 WARN]:        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
    [14:44:37 WARN]:        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2273)
    [14:44:37 WARN]:        at me.gonnakillyou2.mysql.MySQL.tableContainsPlayer(MySQL.java:88)
    [14:44:37 WARN]:        at me.gonnakillyou2.listeners.PlayerDataListener.onLogin(PlayerDataListener.java:37)
    [14:44:37 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [14:44:37 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    [14:44:37 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    [14:44:37 WARN]:        at java.lang.reflect.Method.invoke(Unknown Source)
    [14:44:37 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306)
    [14:44:37 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    [14:44:37 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502)
    [14:44:37 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.PlayerList.attemptLogin(PlayerList.java:456)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:118)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:148)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:814)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    [14:44:37 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    [14:44:37 WARN]:        at java.lang.Thread.run(Unknown Source)
    
    This is my class I used:

    Code:
    package me.gonnakillyou2.mysql;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import org.bukkit.entity.Player;
    
    public class MySQL {
    
        private Connection connection;
    
        private String IP, port, dbName, username, password;
    
        public MySQL(String IP, String port, String dbName, String username, String pass) {
    
            this.IP = IP;
            this.port = port;
            this.dbName = dbName;
            this.username = username;
            this.password = pass;
        }
    
        public synchronized Connection openConnection() {
            try {
                if (checkConnection()) {
                    return connection;
                }
    
                String connectionURL = "jdbc:mysql://" + getIP() + ":" + getPort();
    
                if (getDbName() != null) {
                    connectionURL = connectionURL + "/" + getDbName();
                }
    
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(connectionURL, getUsername(), getPassword());
    
                return connection;
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public boolean closeConnection() {
            try {
                if (connection == null)
                    return false;
    
                if (connection.isClosed())
                    return true;
    
                connection.close();
                return true;
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
    
        public boolean checkConnection() {
            try {
                return connection != null && !connection.isClosed();
            } catch (SQLException e) {
                return false;
            }
        }
    
        public Connection getConnection() {
            return connection;
        }
    
        public synchronized boolean tableContainsPlayer(Player p, String tableName) {
            openConnection();
    
            try {
                PreparedStatement statement = (PreparedStatement) getConnection()
                        .prepareStatement("SELECT * FROM ? WHERE UUIdentifier=?;");
    
                statement.setString(1, tableName);
                statement.setString(2, p.getUniqueId().toString());
    
                System.out.println(statement);
            
                ResultSet result = statement.executeQuery();
                boolean exists = result.next();
    
                statement.close();
                result.close();
    
                return exists;
    
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            } finally {
                closeConnection();
            }
        }
    
        public synchronized void createTable(String tableName) {
            openConnection();
    
            try {
                PreparedStatement statement = (PreparedStatement) getConnection()
                        .prepareStatement("CREATE TABLE IF NOT EXISTS " + tableName);
    
                statement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeConnection();
            }
        }
    
        /**
         * @return the iP
         */
        public String getIP() {
            return IP;
        }
    
        /**
         * @return the port
         */
        public String getPort() {
            return port;
        }
    
        /**
         * @return the dbName
         */
        public String getDbName() {
            return dbName;
        }
    
        /**
         * @return the username
         */
        public String getUsername() {
            return username;
        }
    
        /**
         * @return the password
         */
        public String getPassword() {
            return password;
        }
    }
    
    The error I get is:
    "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 ''playerdata' WHERE UUIdentifier='a89dab9a-503e-4be0-a7c9-9380d2638f5a'' at line 1"

    But I can't findout why it gives me the error when I execute the function tableContainsPlayer. The player is not null and the table is exists in the database.

    Can someone help me with this?

    [EDIT] Solved it, I removed the questionmark where the tablename should come and replaced it with a variable and added 2 "`" symbols at the begin and end by the table name, not it works correctly
     
    Last edited: May 1, 2016
Thread Status:
Not open for further replies.

Share This Page