简体   繁体   中英

Simple JDBC SQL Query returns an empty ResultSet

I have been trying to get a plugin I am working on to talk to my SQL database, creating tabels, and adding rows seems to work fine but simple SELECT queries are returning an empty ResultSet. The relevant code is below.

        queryL=
                "SELECT RATING"
                        + " FROM USERS"
                        + " WHERE UUID = '"
                        + UUID +"';";
        queryG=
                "SELECT RATING"
                        + " FROM " + Constants.serverName 
                        + "_USERS"
                        + " WHERE UUID = '"
                        + UUID +"';";
        try {
            stmt=con.createStatement();
            rs=stmt.executeQuery(queryL);

            if (rs.next()){
                r.setLocalRating(rs.getInt(1));
            }else{
                r.setLocalRating(0);
                registerPlayer(UUID,false);
                Dungeon.getPlugin(Dungeon.class).log("Player new to server");
            }
            stmt.close();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try{
            stmt=con.createStatement();
            rs=stmt.executeQuery(queryG);
            if (rs.next()){
                r.setGlobalRating(rs.getInt(1));
            }else{
                r.setGlobalRating(0);
                registerPlayer(UUID,true);
                Dungeon.getPlugin(Dungeon.class).log("Player new to network");
            }
            stmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

As you can see if the ResultSet is empty (player does not yet exist in my database) I call registerPlayer. Register player then throws a duplicate entry for primary key error, so I know the table and the row I am looking for exist.

The following code shows the update queries used inside the registerPlayer method.

    if (global){
        query=
                "INSERT INTO"
                //+ Constants.dbName + "."
                + " USERS"
                + " VALUES ('"
                + UUID + "', 0)";

    }else{
        query=
                "INSERT INTO "
                //+ Constants.dbName + "."
                + Constants.serverName
                + "_USERS "
                + "VALUES ('"
                + UUID + "', 0)";
    }

Finally for completeness, the queries used for creating the tables

    String userLocalTable=
            "CREATE TABLE IF NOT EXISTS " //+ Constants.dbName + "."
                    + Constants.serverName +
                    "_USERS " +
                    "(UUID varchar(36) NOT NULL, " +
                    "RATING int NOT NULL, " +
                    "PRIMARY KEY (UUID))";
    String userGlobalTable=
            "CREATE TABLE IF NOT EXISTS " + //Constants.dbName + "."
                    "USERS " +
                    "(UUID varchar(36) NOT NULL, " +
                    "RATING int NOT NULL, " +
                    "PRIMARY KEY (UUID))";

Any insight into my issue would be greatly appreciated, as far as I can tell the SELECT queries should not be returning empty ResultSets.

感谢您的输入,事实证明,这显然让我望而却步,QueryL和QueryG各自的查询需要交换。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM