Java应用程序提供整数的随机输出

时间:2014-08-19 20:45:22

标签: java mysql bukkit

我的Java应用程序遇到了问题,每次完成操作时,我都会在数据库中为用户提供一定数量的“积分”。 32个数据库点等于一个游戏点。在用户执行操作的事件中,运行方法以查看点是否为32的倍数,然后表示在将数据库中的点除以32之后具有天空点。用户完成操作一定次数后它停止工作并在每次用户执行操作时返回0,即使它是32的倍数也是如此。这是输出的屏幕截图,代码位于下方,让您了解我拥有的内容。

截图:

enter image description here

我的数据库类中的代码

public long getPoints(String uuid) {
    long points = 0;
    try {
    // Open a connection
    Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
    Statement stmt = conn.createStatement();

    // Execute a query
    stmt = conn.createStatement();

    String sql = "SELECT playerpoints FROM SkyPoints.players WHERE playerid='" + uuid + "';";
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()) {
        points = rs.getLong("playerpoints") / 32;
    }
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return points;
}

public long getRawPoints(String uuid) {
    long points = 0;
    try {
    // Open a connection
    Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
    Statement stmt = conn.createStatement();

    // Execute a query
    stmt = conn.createStatement();

    String sql = "SELECT playerpoints FROM SkyPoints.players WHERE playerid='" + uuid + "';";
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()) {
        points = rs.getLong("playerpoints");
    }
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return points;
}

我的主要课程内的代码

    if(DatabaseObject.getRawPoints(uuid) % 32 == 0) {
        String newamount = String.valueOf(DatabaseObject.getPoints(uuid));
        String word = null;
        if(DatabaseObject.getPoints(uuid) == 1) {
            word = "Point";
        } else {
            word = "Points";
        }
        player.sendMessage(ChatColor.GRAY + "You have earned 1 Sky Point. New amount: " + ChatColor.BLUE + newamount + ChatColor.GRAY + " Sky " + word + ".");
    }

注意:UUID是用户ID

感谢您的帮助,我真的很感激。

更新:出现sql错误

[16:58:42 WARN]: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
[16:58:42 WARN]:        at sun.reflect.GeneratedConstructorAccessor58.newInstance(Unknown Source)
[16:58:42 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[16:58:42 WARN]:        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[16:58:42 WARN]:        at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[16:58:42 WARN]:        at com.mysql.jdbc.Util.getInstance(Util.java:382)
[16:58:42 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
[16:58:42 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
[16:58:42 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
[16:58:42 WARN]:        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1116)
[16:58:42 WARN]:        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2338)
[16:58:42 WARN]:        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2371)
[16:58:42 WARN]:        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
[16:58:42 WARN]:        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
[16:58:42 WARN]:        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
[16:58:42 WARN]:        at sun.reflect.GeneratedConstructorAccessor44.newInstance(Unknown Source)
[16:58:42 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[16:58:42 WARN]:        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[16:58:42 WARN]:        at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[16:58:42 WARN]:        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
[16:58:42 WARN]:        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
[16:58:42 WARN]:        at java.sql.DriverManager.getConnection(DriverManager.java:571)
[16:58:42 WARN]:        at java.sql.DriverManager.getConnection(DriverManager.java:215)
[16:58:42 WARN]:        at me.andrewyunt.SkyPoints.Database.getPoints(Database.java:159)
[16:58:42 WARN]:        at me.andrewyunt.SkyPoints.SkyPoints.onBlockBreak(SkyPoints.java:77)
[16:58:42 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[16:58:42 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[16:58:42 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[16:58:42 WARN]:        at java.lang.reflect.Method.invoke(Method.java:606)
[16:58:42 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:296)
[16:58:42 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
[16:58:42 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502)
[16:58:42 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.PlayerInteractManager.breakBlock(PlayerInteractManager.java:264)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.PlayerInteractManager.dig(PlayerInteractManager.java:118)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:550)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.a(SourceFile:53)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.handle(SourceFile:8)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:731)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490)
[16:58:42 WARN]:        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628)
  

数据源拒绝建立连接,来自服务器的消息:“连接太多”

1 个答案:

答案 0 :(得分:2)

完成后,您需要关闭ConnectionsResultSets,否则,就好像您仍在使用它们执行查询一样,即使您不是&#39 ;吨

要关闭ConnectionResultSet,只需使用variableName.close();。因此,例如,您应该使用:

connection.close();
resultset.close();

对于您的代码,您应该使用:

Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
//set the query, create the statement

ResultSet rs = stmt.executeQuery(sql);
//get values

conn.close();
rs.close();

//return values

您收到错误的原因是因为MySQL认为您仍在使用所有这些连接,即使您不是。为了使MySQL不会认为您仍在使用这些连接,请使用connection.close()关闭它们。