java - 另一个 MYSQL 问题

标签 java mysql

int rs2 = s
                    .executeUpdate("INSERT INTO hiscores (userid, playerRights, LVL, XP, 0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) VALUES ('"
                            + player.getUniqueId()
                            + "', '"
                            + player.getStaffRights()
                            + "', '"
                            + player.getSkill().getTotalLevel()
                            + "','"
                            + player.getSkill().getTotalXp()
                            + "','"
                            + player.getSkill().getExp()[0]
                            + "','"
                            + player.getSkill().getExp()[1]
                            + "','"
                            + player.getSkill().getExp()[2]
                            + "','"
                            + player.getSkill().getExp()[3]
                            + "','"
                            + player.getSkill().getExp()[4]
                            + "','"
                            + player.getSkill().getExp()[5]
                            + "','"
                            + player.getSkill().getExp()[6]
                            + "','"
                            + player.getSkill().getExp()[7]
                            + "','"
                            + player.getSkill().getExp()[8]
                            + "','"
                            + player.getSkill().getExp()[9]
                            + "','"
                            + player.getSkill().getExp()[10]
                            + "','"
                            + player.getSkill().getExp()[11]
                            + "','"
                            + player.getSkill().getExp()[12]
                            + "','"
                            + player.getSkill().getExp()[13]
                            + "','"
                            + player.getSkill().getExp()[14]
                            + "','"
                            + player.getSkill().getExp()[15]
                            + "','"
                            + player.getSkill().getExp()[16]
                            + "','"
                            + player.getSkill().getExp()[17]
                            + "','"
                            + player.getSkill().getExp()[18]
                            + "','"
                            + player.getSkill().getExp()[19]
                            + "','"
                            + player.getSkill().getExp()[20] + "')");

我不确定为什么我的代码不起作用。我已经很接近了!为什么我的语法出现错误?对我来说看起来正确吗?有人可以帮我解决这个问题吗?非常感谢任何帮助!

填充填充填充填充填充填充填充填充填充填充填充填充填充

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) VALUES ('0', '0', '42','3' at line 1

最佳答案

在这种情况下我将使用PreparedStatement来简化此代码:

PreparedStatement st;

st = connection.prepareStatement(""
   +"INSERT INTO hiscores (userid, playerRights, LVL, XP, " 
   +"`0`,`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`10`,`11`,`12`,`13`,`14`,`15`,"
   +"`16`,`17`,`18`,`19`,`20`) "
   +"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

st.setInt(    1, player.getUniqueId());
st.setString( 2, player.getStaffRights());
st.setInt(    3, player.getSkill().getTotalLevel());
st.setString( 4, player.getSkill().getTotalXp());

for(int i=0; i<=20; i++){
   st.setInt( i+5, player.getSkill().getExp()[i]);
} 

st.executeUpdate();

关于java - 另一个 MYSQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644916/

相关文章:

php - 如何删除 = 和 ?来自 php 中的 URL

php - 在 PHP MYSQL 上获取 JSON

java - 使用 mod 对 id 进行分区的良好哈希函数

java - 尝试创建汽车对象数组,结果为 null

java - Gradle 构建失败,java 1.8,构建工具 2.2.0-alpha4,启用 jackOptions

php - 比较有问题的php中的日期

mysql - 从与其他关联的表中插入数据

java - 带有继承的Java程序应该如何追溯?

java - 如果字符串以负号开头,setText 将无法正常工作

mysql - MySQL 中何时使用单引号、双引号和反引号