我的代码没有将任何数据保存到表中。我不知道为什么不是,我修复了我在上一篇文章中发布的错误。有什么理由不会吗?它不再给我任何错误。表格是否与查询兼容?我现在真的被困住了。
以下是将其保存到表格中的代码:
public boolean saveHighScore(Client c) throws SQLException {
long totallvlexp = (long) (c.playerXP[0]) + (c.playerXP[1]) + (c.playerXP[2]) + (c.playerXP[3]) + (c.playerXP[4]) + (c.playerXP[5]) + (c.playerXP[6]) + (c.playerXP[7]) + (c.playerXP[8]) + (c.playerXP[9]) + (c.playerXP[10]) + (c.playerXP[11]) + (c.playerXP[12]) + (c.playerXP[13]) + (c.playerXP[14]) + (c.playerXP[15]) + (c.playerXP[16]) + (c.playerXP[17]) + (c.playerXP[18]) + (c.playerXP[19]) + (c.playerXP[20]);
int totallevell = (int) (c.getLevelForXP(c.playerXP[0]) + c.getLevelForXP(c.playerXP[1]) + c.getLevelForXP(c.playerXP[2]) + c.getLevelForXP(c.playerXP[3]) + c.getLevelForXP(c.playerXP[4]) + c.getLevelForXP(c.playerXP[5]) + c.getLevelForXP(c.playerXP[6]) + c.getLevelForXP(c.playerXP[7]) + c.getLevelForXP(c.playerXP[8]) + c.getLevelForXP(c.playerXP[9]) + c.getLevelForXP(c.playerXP[10]) + c.getLevelForXP(c.playerXP[11]) + c.getLevelForXP(c.playerXP[12]) + c.getLevelForXP(c.playerXP[13]) + c.getLevelForXP(c.playerXP[14]) + c.getLevelForXP(c.playerXP[15]) + c.getLevelForXP(c.playerXP[16]) + c.getLevelForXP(c.playerXP[17]) + c.getLevelForXP(c.playerXP[18]) + c.getLevelForXP(c.playerXP[19]) + c.getLevelForXP(c.playerXP[20]));
boolean result = true;
String updateSkillsSql = "UPDATE skills"
+ " SET `Attacklvl`=?,"
+ " `Attackxp`=?,"
+ " `Defencelvl`=?,"
+ " `Defencexp`=?,"
+ " `Strengthlvl`=?,"
+ " `Strengthxp`=?,"
+ " `Hitpointslvl`=?,"
+ " `Hitpointsxp`=?,"
+ " `Rangelvl`=?,"
+ " `Rangexp`=?,"
+ " `Prayerlvl`=?,"
+ " `Prayerxp`=?,"
+ " `Magiclvl`=?,"
+ " `Magicxp`=?,"
+ " `Cookinglvl`=?,"
+ " `Cookingxp`=?,"
+ " `Woodcuttinglvl`=?,"
+ " `Woodcuttingxp`=?,"
+ " `Fletchinglvl`=?,"
+ " `Fletchingxp`=?,"
+ " `Fishinglvl`=?,"
+ " `Fishingxp`=?,"
+ " `Firemakinglvl`=?,"
+ " `Firemakingxp`=?,"
+ " `Craftinglvl`=?,"
+ " `Craftingxp`=?,"
+ " `Smithinglvl`=?,"
+ " `Smithingxp`=?,"
+ " `Mininglvl`=?,"
+ " `Miningxp`=?,"
+ " `Herblorelvl`=?,"
+ " `Herblorexp`=?,"
+ " `Agilitylvl`=?,"
+ " `Agilityxp`=?,"
+ " `Thievinglvl`=?,"
+ " `Thievingxp`=?,"
+ " `Slayerlvl`=?,"
+ " `Slayerxp`=?,"
+ " `Farminglvl`=?,"
+ " `Farmingxp`=?,"
+ " `Runecraftlvl`=?,"
+ " `Runecraftxp`=?"
+ " WHERE `playerName`=?";
String updateSkillsoverallSql = "UPDATE skillsoverall"
+ " SET `lvl`=?,"
+ " `xp`=?"
+ " WHERE `playerName`=?";
String updatePlayerRightsSql = "UPDATE playerrights"
+ " SET `rank` = ?"
+ " WHERE `playerName`=?";
try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost/highscores","root","root")) {
con.setAutoCommit(false);
try(PreparedStatement pstmt = con.prepareStatement(updateSkillsSql);PreparedStatement pstmt2 = con.prepareStatement(updateSkillsoverallSql);PreparedStatement pstmt3 = con.prepareStatement(updatePlayerRightsSql);) {
setParameters(pstmt,
c.playerLevel[0], c.playerXP[0],
c.playerLevel[1], c.playerXP[1],
c.playerLevel[2], c.playerXP[2],
c.playerLevel[3], c.playerXP[3],
c.playerLevel[4], c.playerXP[4],
c.playerLevel[5], c.playerXP[5],
c.playerLevel[6], c.playerXP[6],
c.playerLevel[7], c.playerXP[7],
c.playerLevel[8], c.playerXP[8],
c.playerLevel[9], c.playerXP[9],
c.playerLevel[10], c.playerXP[10],
c.playerLevel[11], c.playerXP[11],
c.playerLevel[12], c.playerXP[12],
c.playerLevel[13], c.playerXP[13],
c.playerLevel[14], c.playerXP[14],
c.playerLevel[15], c.playerXP[15],
c.playerLevel[16], c.playerXP[16],
c.playerLevel[17], c.playerXP[17],
c.playerLevel[18], c.playerXP[18],
c.playerLevel[19], c.playerXP[19],
c.playerLevel[20], c.playerXP[20],
c.playerName);
setParameters(pstmt2,
totallevell, totallvlexp,
c.playerName);
setParameters(pstmt3,
c.getPermission().getId(), c.playerName);
pstmt.executeUpdate();
pstmt2.executeUpdate();
pstmt3.executeUpdate();
} catch (Exception e) {
System.out.println(String.format("There's a problem when saving the data of player %s.", c.playerName));
e.printStackTrace(System.out);
con.rollback();
result = false;
}
if (result) {
con.commit();
con.setAutoCommit(true);
}
} catch (Exception e) {
System.out.println(String.format("There's a problem when saving the data of player %s.", c.playerName));
e.printStackTrace(System.out);
result = false;
}
return result;
}
private void setParameters(PreparedStatement pstmt, Object ... args) {
int i = 0;
try {
for (Object arg : args) {
pstmt.setObject(++i, arg);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
以下是表格:
CREATE TABLE `skills` (
`playerName` varchar(15) NOT NULL default '',
`Attacklvl` double default NULL,
`Attackxp` double default NULL,
`Defencelvl` double default NULL,
`Defencexp` double default NULL,
`Strengthlvl` double default NULL,
`Strengthxp` double default NULL,
`Hitpointslvl` double default NULL,
`Hitpointsxp` double default NULL,
`Rangelvl` double default NULL,
`Rangexp` double default NULL,
`Prayerlvl` double default NULL,
`Prayerxp` double default NULL,
`Magiclvl` double default NULL,
`Magicxp` double default NULL,
`Cookinglvl` double default NULL,
`Cookingxp` double default NULL,
`Woodcuttinglvl` double default NULL,
`Woodcuttingxp` double default NULL,
`Fletchinglvl` double default NULL,
`Fletchingxp` double default NULL,
`Fishinglvl` double default NULL,
`Fishingxp` double default NULL,
`Firemakinglvl` double default NULL,
`Firemakingxp` double default NULL,
`Craftinglvl` double default NULL,
`Craftingxp` double default NULL,
`Smithinglvl` double default NULL,
`Smithingxp` double default NULL,
`Mininglvl` double default NULL,
`Miningxp` double default NULL,
`Herblorelvl` double default NULL,
`Herblorexp` double default NULL,
`Agilitylvl` double default NULL,
`Agilityxp` double default NULL,
`Thievinglvl` double default NULL,
`Thievingxp` double default NULL,
`Slayerlvl` double default NULL,
`Slayerxp` double default NULL,
`Farminglvl` double default NULL,
`Farmingxp` double default NULL,
`Runecraftlvl` double default NULL,
`Runecraftxp` double default NULL,
index (playerName),
constraint fk_playername_rights
foreign key (playername) references players (playername)
on delete restrict
on update restrict
) ENGINE=MyISAM;
ALTER TABLE skills ADD INDEX idx_playername (playername);
CREATE TABLE `playerrights` (
`playerName` varchar(15) NOT NULL default '',
`rank` int(2) default NULL,
index (playername),
constraint fk_playername_rights
foreign key (playername) references players (playername)
on delete restrict
on update restrict
) ENGINE=MyISAM;
ALTER TABLE playerrights ADD INDEX idx_playername (playername);
CREATE TABLE `skillsoverall` (
`playerName` varchar(15) NOT NULL default '',
`lvl` int(11) default NULL,
`xp` BIGINT(11) default NULL,
index (playername),
constraint fk_playername_rights
foreign key (playername) references players (playername)
on delete restrict
on update restrict
) ENGINE=MyISAM;
ALTER TABLE skillsoverall ADD INDEX idx_playerName (playerName);
答案 0 :(得分:0)
审核后我发现您的代码没有任何问题。但是我有一个重要的建议,我认为会解决你的问题。
在尝试更新代码中的表之前 - 打印不起作用的SQL。在你的情况下,我相信它是updateSkillsSql。
System.out.println(updateSkillsSql);
接下来,将查询完全复制并粘贴到当前连接的SQL控制台并使用您的数据库。如果返回任何错误,请检查SQL是否有错误。
我发现99%的时间在尝试用其他语言(例如PHP,Java)编写查询时,我能够通过SQL控制台直接应用它来更快地调试问题。通常它最终会丢失引号,或者是我没想到的那个空格。
如果您这样做 - 在SQL中运行查询和结果的结果。如果您无法解决问题,我会以更多详细信息更新我的答案。
答案 1 :(得分:0)
将评论转化为答案。
您可能希望/需要将准备好的陈述更改为INSERT ... ON DUPLICATE KEY UPDATE
但是,要实现此目的,您需要PRIMARY KEY
/ UNIQUE
索引。只要辅助表的每个人输入的最大值不超过一个, playerName 列就会满足此要求。 (虽然从长远来看,数字ID可能是更好的选择......)
举一个简单的例子,请参阅SQL Fiddle
或者查看SQL Fiddle,它更接近您的问题域。请注意,尽管有两个INSERT
,但技能中只有一条记录存在。
如果需要调整/进一步详细说明,请发表评论。