java.sql.SQLException: Before start of result set

Discussion in 'Programming' started by TheNeoTV, Jan 29, 2020.

  1. I have a problem with executing a command that teleports me into my world via / tpworld. I get an error in the console in this code snippet.

    Before i change
    Code (Java):
    st = conn.prepareStatement("SELECT * FROM worlds WHERE Owner='"+p.getName()+"'");
                            rs = st.executeQuery();
                            if(rs.isBeforeFirst()){
    to the new code

    Code (Java):
    st = conn.prepareStatement("SELECT * FROM worlds WHERE Owner='"+p.getName()+"'");
                            rs = st.executeQuery();
                            if(rs.next()){
    I get this error :

    So I used Google to change the code, but then I got a new error.

    Now I get the following error

    Code (Java):
    st = conn.prepareStatement("SELECT * FROM worlds WHERE Owner='"+p.getName()+"'");
                            rs = st.executeQuery();
                            if(rs.next()){
                            World Playerworld = Bukkit.getWorld(rs.getString("Weltname"));
                           
                            if(Playerworld != null){
                                rs.next();
                                Double x = rs.getDouble("spawnX");
                                Double y = rs.getDouble("spawnY");
                                Double z = rs.getDouble("spawnZ");
                               
                                Float yaw = rs.getFloat("spawnYAW");
                                Float pitch = rs.getFloat("spawnPITCH");
                               
                                Location loc = new Location(Playerworld, x, y, z, yaw, pitch);
                               
                                p.teleport(loc);
                            }else{
                                rs.next();
                                Bukkit.getServer().createWorld(new WorldCreator(rs.getString("Weltname")));
                                Double x = rs.getDouble("spawnX");
                                Double y = rs.getDouble("spawnY");
                                Double z = rs.getDouble("spawnZ");
                               
                                Float yaw = rs.getFloat("spawnYAW");
                                Float pitch = rs.getFloat("spawnPITCH");
                               
                                Location loc = new Location(Playerworld, x, y, z, yaw, pitch);
                               
                                p.teleport(loc);
                            }
    What do I have to write down below for my command to work. I don't want any external links to other wikis or service providers. I just want this code to work. I myself have any help pages open, but everything makes no sense to me!
     
    • Funny Funny x 1
  2. Try to use this structure for prepared statements as it only protect you from sql injection from user input when its binded to the statement.

    Code (Java):
     
    public void test() throws SQLException {
        String query = "SELECT * FROM worlds WHERE Owner = ?";
        PreparedStatement stmt = this.con.prepareStatement(query);
        stmt.setString(1, "owner");
        stmt.execute();

        ResultSet rs = stmt.getResultSet();

        if(!rs.next()) {
            return;
        }

        World Playerworld = Bukkit.getWorld(rs.getString("Weltname"));

        if(Playerworld != null){
            Double x = rs.getDouble("spawnX");
            Double y = rs.getDouble("spawnY");
            Double z = rs.getDouble("spawnZ");

            Float yaw = rs.getFloat("spawnYAW");
            Float pitch = rs.getFloat("spawnPITCH");

            Location loc = new Location(Playerworld, x, y, z, yaw, pitch);

            p.teleport(loc);
        }else{
            Bukkit.getServer().createWorld(new WorldCreator(rs.getString("Weltname")));
            Double x = rs.getDouble("spawnX");
            Double y = rs.getDouble("spawnY");
            Double z = rs.getDouble("spawnZ");

            Float yaw = rs.getFloat("spawnYAW");
            Float pitch = rs.getFloat("spawnPITCH");

            Location loc = new Location(Playerworld, x, y, z, yaw, pitch);

            p.teleport(loc);
        }
    }
     
     
    • Agree Agree x 1
  3. cannot use this. because this gives some issues sry. another solution with my code?