Can't get MySQL command SELECT to work..

Discussion in 'Plugin Development' started by Maro, Dec 16, 2012.

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

    Maro

    Hi, I'm making this warp plugin for my server, with MySQL. I have managed to make setwarp, but I'm a little stuck on warp. Here's my code:

    Code:
    public static Location Warp(String warp, Player p) {
            try {
                Connection con = DriverManager.getConnection(
                        "jdbc:mysql://" + host + "/" + database, user, password);
                ResultSet rp1 = null;
                Statement stat = con.createStatement();
                rp1 = stat.executeQuery("SELECT x, y, z FROM maltaria_warps WHERE warpname = " + warp);
                if (!rp1.next()){
                    p.sendMessage(rød + "Warp doesn't exist!");
                }else{
                int x = rp1.getInt(2);
                int y = rp1.getInt(3);
                int z = rp1.getInt(4);
                return new Location(
                        Bukkit.getWorld("world"),
                        x, y, z);
                }
            } catch (SQLException e) {
                System.out
                        .print("||||||||||| ERROR :: Couldn't connect to database ||||||||||||");
                e.printStackTrace();
            }
            return null;
        }
    But when I do warp, I just get an error in the console saying that the column doesn't exist, wich it does. Here's the stack tracer:

    PS Upside down

    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    16.12 14:51:52 [Server] SEVERE at com.mysql.jdbc.Util.getInstance(Util.java:382)
    16.12 14:51:52 [Server] SEVERE at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    16.12 14:51:52 [Server] SEVERE at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    16.12 14:51:52 [Server] SEVERE at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    16.12 14:51:52 [Server] SEVERE at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    16.12 14:51:52 [Server] SEVERE at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    16.12 14:51:52 [Server] SEVERE com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'test' in 'where clause'
     
  2. Offline

    leiger

    Put the warp name in quotes, and see if that fixes your problem:

    Code:
    rp1 = stat.executeQuery("SELECT x, y, z FROM maltaria_warps WHERE warpname = '" + warp + "'");
    
     
  3. Offline

    fireblast709

    you forgot the quotes after the '='.
    Code:java
    1. rp1 = stat.executeQuery("SELECT x, y, z FROM maltaria_warps WHERE warpname = '" + warp + "';");
     
  4. Offline

    Maro

    Now I get this error:

    16.12 15:17:15 [Server] SEVERE java.sql.SQLException: Column Index out of range, 4 > 3.

    Sorry for being a noob, I'm quite new to MySQL.

    Code is this:

    Code:
    rp1 = stat.executeQuery("SELECT x, y, z FROM maltaria_warps WHERE warpname = '" + warp + "'");
     
  5. Offline

    Lolmewn

    .getInt(1, 2 and 3). 4 is not present. Actually, might be 0, 1 and 2, just not sure.
     
  6. Offline

    Maro

    Never mind, got It to work.

    Thanks for the help, it was much appreciated!

    EDIT: That was the answer Lolmewn
     
Thread Status:
Not open for further replies.

Share This Page