Java 另一个MYSQL问题
我不知道为什么我的代码不起作用。我好近啊!为什么我的语法会出错?我看对了吗?谁能帮我一下吗?非常感谢您的帮助 填隙器填隙器填隙器填隙器填隙器填隙器填隙器填隙器Java 另一个MYSQL问题,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 ('"
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] + "')");
在本例中,我将使用PreparedStatement来简化此代码:
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 st;
st=连接。准备状态(“”)
+“插入到核心(用户ID、playerRights、LVL、XP,”
+"`0`,`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`10`,`11`,`12`,`13`,`14`,`15`,"
+"`16`,`17`,`18`,`19`,`20`) "
+“值(?,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,”;
st.setInt(1,player.getUniqueId());
st.setString(2,player.getStaffRights());
st.setInt(3,player.getSkill().gettotalevel());
st.setString(4,player.getSkill().getTotalXp());
对于(int i=0;iEscape)列名称,它们是带反勾号的数字。非常好。谢谢!根据答案使用preparedstatements。不要使用串联构造语句。preparedstatements还可以避免SQL注入。
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();