Java mySQL 查询
Java mySQL query
基本上我只是检查 mySQL 数据库中是否存在某个玩家,但它不断抛出错误等。我不知道该怎么做。
(这都是在 bukkit api 中完成的,但问题只是 SQL)
玩家登录后会调用databaseFindPlayer方法查看数据库中是否存在
我只需要查看玩家是否存在于数据库中
@EventHandler
public void onPlayerLogin(PlayerJoinEvent e) throws SQLException {
getLogger().info("Player joining.... + TEST");
if (databaseFindPlayer(e.getPlayer().getName()) == false) {
getLogger().info("Player does not have data file.. Generating one now");
databaseCreatePlayer(e.getPlayer().getName());
databaseFindPlayer(e.getPlayer().getName()); //For debugging
}
}
public boolean databaseFindPlayer(String name) throws SQLException {
getLogger().info("finding players");
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = '" + name + "';");
preparedStatement.setString(1, name);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
System.out.println("Player exists!");
return true;
} else {
System.out.println("Player doesn't exist!");
return false;
}
}
(所有的 getLogger() 调用都是为了调试)
编辑:
Error message
[18:14:17 ERROR]: Could not pass event PlayerJoinEvent to CustomLeaderboard v0.1
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.ja
va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.PlayerList.c(PlayerList.java:251) [craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.PlayerList.a(PlayerList.java:138) [craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.LoginListener.c(LoginListener.java:76) [
craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.LoginListener.a(LoginListener.java:42) [
craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:160
) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craf
tbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of pa
rameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[craf
tbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) ~[craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3
709) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3
693) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:454
4) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mcaletheia.customLeaderboard.CustomLeaderboard.databaseFindPlayer
(CustomLeaderboard.java:88) ~[?:?]
at com.mcaletheia.customLeaderboard.CustomLeaderboard.onPlayerLogin(Cust
omLeaderboard.java:69) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
.8.0_25]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.ja
va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
... 14 more
异常准确地告诉了哪里出了问题
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
回顾一下您使用
声明语句的代码
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = '" + name + "';");
preparedStatement.setString(1, name);
但是您没有提供可以绑定您的值的参数
相反,应该是
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = ?");
preparedStatement.setString(1, name);
有关详细信息,请参阅 Using Prepared Statements
您在这里混淆了 PreparedStatement
class 的意图。
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = '" + name + "';");
preparedStatement.setString(1, name);
说 setString(1, name)
,就是说 "Set the first replacement in the query to whatever is in the field called name
"。由于您的查询没有任何替换,因此它正在抱怨。 (参数索引超出范围)。它知道你有零个参数,而你告诉它你有一个参数。
试试这个(注意,我还删除了您查询末尾的分号):
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = ?");
preparedStatement.setString(1, name);
解决此问题的另一种方法是根本不设置 name 参数,但通过手动插入一个可能被污染的值,您将面临 SQL 注入攻击。坚持使用上面概述的 ?
替换。
基本上我只是检查 mySQL 数据库中是否存在某个玩家,但它不断抛出错误等。我不知道该怎么做。 (这都是在 bukkit api 中完成的,但问题只是 SQL)
玩家登录后会调用databaseFindPlayer方法查看数据库中是否存在
我只需要查看玩家是否存在于数据库中
@EventHandler
public void onPlayerLogin(PlayerJoinEvent e) throws SQLException {
getLogger().info("Player joining.... + TEST");
if (databaseFindPlayer(e.getPlayer().getName()) == false) {
getLogger().info("Player does not have data file.. Generating one now");
databaseCreatePlayer(e.getPlayer().getName());
databaseFindPlayer(e.getPlayer().getName()); //For debugging
}
}
public boolean databaseFindPlayer(String name) throws SQLException {
getLogger().info("finding players");
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = '" + name + "';");
preparedStatement.setString(1, name);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
System.out.println("Player exists!");
return true;
} else {
System.out.println("Player doesn't exist!");
return false;
}
}
(所有的 getLogger() 调用都是为了调试)
编辑:
Error message
[18:14:17 ERROR]: Could not pass event PlayerJoinEvent to CustomLeaderboard v0.1
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.ja
va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.PlayerList.c(PlayerList.java:251) [craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.PlayerList.a(PlayerList.java:138) [craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.LoginListener.c(LoginListener.java:76) [
craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.LoginListener.a(LoginListener.java:42) [
craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:160
) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craf
tbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of pa
rameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[craf
tbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) ~[craft
bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3
709) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3
693) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:454
4) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
at com.mcaletheia.customLeaderboard.CustomLeaderboard.databaseFindPlayer
(CustomLeaderboard.java:88) ~[?:?]
at com.mcaletheia.customLeaderboard.CustomLeaderboard.onPlayerLogin(Cust
omLeaderboard.java:69) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
.8.0_25]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.ja
va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
... 14 more
异常准确地告诉了哪里出了问题
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
回顾一下您使用
声明语句的代码PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = '" + name + "';");
preparedStatement.setString(1, name);
但是您没有提供可以绑定您的值的参数
相反,应该是
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = ?");
preparedStatement.setString(1, name);
有关详细信息,请参阅 Using Prepared Statements
您在这里混淆了 PreparedStatement
class 的意图。
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = '" + name + "';");
preparedStatement.setString(1, name);
说 setString(1, name)
,就是说 "Set the first replacement in the query to whatever is in the field called name
"。由于您的查询没有任何替换,因此它正在抱怨。 (参数索引超出范围)。它知道你有零个参数,而你告诉它你有一个参数。
试试这个(注意,我还删除了您查询末尾的分号):
PreparedStatement preparedStatement = c.prepareStatement("show columns from players where field = ?");
preparedStatement.setString(1, name);
解决此问题的另一种方法是根本不设置 name 参数,但通过手动插入一个可能被污染的值,您将面临 SQL 注入攻击。坚持使用上面概述的 ?
替换。