我一直在做我的功课,我决定将我的 vote4cash 类重写为一个名为 MysqlManager 的新类,该类为我的 vote4cash 奖励系统管理 mysql。我制作的 MysqlManager 类需要允许 Commands 类连接到 mysql - 完成并且它需要允许 Commands 类执行查询 - 我需要这部分的帮助。我的新课程取得了更大的进步,但我仍然停留在课程的最后、最重要的部分之一,再次允许命令类执行查询。
在我的 MysqlManager 类中,我将连接到 MySql 的代码放在下面
public synchronized static void createConnection() {
现在我只需要将允许 Commands 类执行查询的代码也放在它下面。我已经研究并尝试这样做了一段时间,但我绝对没有运气。
整个 MysqlManager 类:
package server.util;
/*
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
*/
import java.sql.*;
import java.net.*;
import server.model.players.Client;//Will be needed eventually so that I can reward players who have voted.
/**
* MySQL and Vote4Cash Manager
* @author Cloudnine
*
*/
public class MysqlManager {
/** MySQL Connection */
public static Connection conn = null;
public static Statement statement = null;
public static ResultSet results = null;
public static Statement stmt = null;
public static ResultSet auth = null;
public static ResultSet given = null;
/** MySQL Database Info */
public static String DB = "vote4gold";
public static String URL = "localhost";
public static String USER = "root";
public static String PASS = "";
public static String driver = "com.mysql.jdbc.Driver"; //Driver for JBDC(Java and MySQL connector)
/** Connects to MySQL Database*/
public synchronized static void createConnection() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(URL + DB, USER, PASS);
conn.setAutoCommit(false);
stmt = conn.createStatement();
Misc.println("Connected to MySQL Database");
}
catch(Exception e) {
//e.printStackTrace();
}
}
public synchronized static void destroyConnection() {
try {
statement.close();
conn.close();
} catch (Exception e) {
//e.printStackTrace();
}
}
public synchronized static ResultSet query(String s) throws SQLException {
try {
if (s.toLowerCase().startsWith("select")) {
ResultSet rs = statement.executeQuery(s);
return rs;
} else {
statement.executeUpdate(s);
}
return null;
} catch (Exception e) {
destroyConnection();
createConnection();
//e.printStackTrace();
}
return null;
}
}
我的命令片段:
if (playerCommand.equals("claimreward")) {
try {
PreparedStatement ps = DriverManager.getConnection().createStatement("SELECT * FROM votes WHERE ip = hello AND given = '1' LIMIT 1");
//ps.setString(1, c.playerName);
ResultSet results = ps.executeQuery();
if(results.next()) {
c.sendMessage("You have already been given your voting reward.");
} else {
ps.close();
ps = DriverManager.getConnection().createStatement("SELECT * FROM votes WHERE ip = hello AND given = '0' LIMIT 1");
//ps.setString(1, playerCommand.substring(5));
results = ps.executeQuery();
if(results.next()) {
ps.close();
ps = DriverManager.getConnection().createStatement("UPDATE votes SET given = '1' WHERE ip = hello");
//ps.setString(1, playerCommand.substring(5));
ps.executeUpdate();
c.getItems().addItem(995, 5000000);
c.sendMessage("Thank you for voting! You've recieved 5m gold!");
} else {
c.sendMessage("You haven't voted yet. Vote for 5m gold!");
}
}
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return;
命令的工作原理:当玩家输入 ::commandname(在本例中为 claimreward)时,将执行命令函数。这不是整个命令类,只是我认为需要发布的部分,以便我的问题足够详细以获得有用的答案。
注意:我有我所有的进口。
注意:Mysql 连接成功。
注意:我需要使上面的命令代码片段能够执行 mysql 查询。
注意:我更喜欢直接从命令执行查询,而不是从 MysqlManager,但我会尽我所能来解决这个问题。
我觉得我已经足够详细且相关地描述了我的问题,但是如果您需要更多信息或对任何事情的理解,请告诉我,我会尝试更具体。
感谢您花时间检查我的问题。如果您能提供帮助,请提前致谢。-亚历克斯