简体   繁体   English

使用 JDBC 的 Java MySQL 查询

[英]Java MySQL Query using JDBC

So i've been working a project of mine and i'm trying to make highscores for the players of my project.所以我一直在做我的一个项目,我正在努力为我的项目的玩家创造高分。 Currently the highscores works to display every skill and their combined level, exp and level per skill.目前,高分可以显示每项技能及其组合级别、经验和每个技能的级别。 What i'm trying to do at the moment is have it display the prestige level of every skill.我现在要做的是让它显示每项技能的声望水平。 So i'm trying to cycle through all the skills prestige levels 1 by 1. Here's what I have currently:所以我正在尝试 1 级 1 级循环所有技能声望等级。 这是我目前拥有的:

        try {
            if (!connect(HOST, DATABASE, USER, PASS)) {
                return;
            }

            String name = player.getUsername();

            PreparedStatement stmt1 = prepare("DELETE FROM "+TABLE+" WHERE username=?");
            stmt1.setString(1, player.getUsername());
            stmt1.execute();

            PreparedStatement stmt2 = prepare(generateQuery());
            stmt2.setString(1, player.getUsername());
            stmt2.setInt(2, player.getRights());

            stmt2.setInt(3, 0); // game mode number
            stmt2.setInt(4, player.getSkills().getTotalLevel(player)); // total level

            stmt2.setLong(5, player.getSkills().getTotalXp());

            for (int i = 0; i < 25; i++)
                stmt2.setInt(6 + i, (int)player.getSkills().getXp()[i]);
            for (int ii = 0; ii < 25; ii++) //this is what i added and wont work
                stmt2.setInt(7 + ii, player.prestigeManager.getTotalTimesSkillPrestiged(ii));
            stmt2.execute();

            destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

This is the query builder :这是查询构建器:

public static String generateQuery() {
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO "+TABLE+" (");
        sb.append("username, ");
        sb.append("rights, ");
        sb.append("mode, ");
        sb.append("total_level, ");
        sb.append("overall_xp, ");
        sb.append("attack_xp, ");
        sb.append("defence_xp, ");
        sb.append("strength_xp, ");
        sb.append("constitution_xp, ");
        sb.append("ranged_xp, ");
        sb.append("prayer_xp, ");
        sb.append("magic_xp, ");
        sb.append("cooking_xp, ");
        sb.append("woodcutting_xp, ");
        sb.append("fletching_xp, ");
        sb.append("fishing_xp, ");
        sb.append("firemaking_xp, ");
        sb.append("crafting_xp, ");
        sb.append("smithing_xp, ");
        sb.append("mining_xp, ");
        sb.append("herblore_xp, ");
        sb.append("agility_xp, ");
        sb.append("thieving_xp, ");
        sb.append("slayer_xp, ");
        sb.append("farming_xp, ");
        sb.append("runecrafting_xp, ");
        sb.append("hunter_xp, ");
        sb.append("construction_xp, ");
        sb.append("summoning_xp, ");
        sb.append("dungeoneering_xp) ");
        sb.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        sb.append("overall_prestige, ");
        sb.append("attack_prestige, ");
        sb.append("defence_prestige, ");
        sb.append("strength_prestige, ");
        sb.append("constitution_prestige, ");
        sb.append("ranged_prestige, ");
        sb.append("prayer_prestige, ");
        sb.append("magic_prestige, ");
        sb.append("cooking_prestige, ");
        sb.append("woodcutting_prestige, ");
        sb.append("fletching_prestige, ");
        sb.append("fishing_prestige, ");
        sb.append("firemaking_prestige, ");
        sb.append("crafting_prestige, ");
        sb.append("smithing_prestige, ");
        sb.append("mining_prestige, ");
        sb.append("herblore_prestige, ");
        sb.append("agility_prestige, ");
        sb.append("thieving_prestige, ");
        sb.append("slayer_prestige, ");
        sb.append("farming_prestige, ");
        sb.append("runecrafting_prestige, ");
        sb.append("hunter_prestige, ");
        sb.append("construction_prestige, ");
        sb.append("summoning_prestige, ");
        sb.append("dungeoneering_prestige) ");
        sb.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        return sb.toString();
    } 

This is the error :这是错误:

java.sql.SQLException: No value specified for parameter 32
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2578)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2554)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2480)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1298)
    at com.RuneDivinity.game.cores.mysql.Highscores.run(Highscores.java:67)
    at java.lang.Thread.run(Unknown Source)

You have你有

 sb.append("dungeoneering_xp) ");

has to be必须

sb.append("dungeoneering_xp, ");

sb.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); sb.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?)");

is twice in your code::在您的代码中是两次::

Remove simply the first from the top.从顶部简单地删除第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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