I am having trouble setting up a command to automatically claim a donation on my 667 revision server. I have everything set up on the backend, including the webstore and mySQL databases. I have also implemented server side code in a Donation.java class.
Here is the important part of the donation.java class, excluding database information:
Code:
public class Donation implements Runnable {
public static final String HOST = "removed"; // website ip address
public static final String USER = "removed";
public static final String PASS = "removed";
public static final String DATABASE = "removed";
private Player player;
private Connection conn;
private Statement stmt;
/**
* The constructor
* @Param player
*/
public Donation(Player player) {
this.player = player;
}
@Override
public void run() {
try {
if (!connect(HOST, DATABASE, USER, PASS)) {
return;
}
String name = player.getUsername().replace("_", " ");
ResultSet rs = executeQuery("SELECT * FROM payments WHERE player_name='"+name+"' AND status='Completed' AND claimed=0");
while (rs.next()) {
int item_number = rs.getInt("item_number");
double paid = rs.getDouble("amount");
int quantity = rs.getInt("quantity");
switch (item_number) {// add products according to their ID in the ACP
case 1023: // example
player.getInventory().addItem(14484, 1);
player.getPackets().sendGameMessage("<col=00ff00>Thanks for donating.");
break;
}
rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
rs.updateRow();
}
destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @Param host the host ip address or url
* @Param database the name of the database
* @Param user the user attached to the database
* @Param pass the users password
* @Return true if connected
*/
public boolean connect(String host, String database, String user, String pass) {
try {
this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database, user, pass);
return true;
} catch (SQLException e) {
System.out.println("Failing connecting to database!");
return false;
}
}
/**
* Disconnects from the MySQL server and destroy the connection
* and statement instances
*/
public void destroy() {
try {
conn.close();
conn = null;
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Executes an update query on the database
* @Param query
* @see {@link Statement#executeUpdate}
*/
public int executeUpdate(String query) {
try {
this.stmt = this.conn.createStatement(1005, 1008);
int results = stmt.executeUpdate(query);
return results;
} catch (SQLException ex) {
ex.printStackTrace();
}
return -1;
}
/**
* Executres a query on the database
* @Param query
* @see {@link Statement#executeQuery(String)}
* @Return the results, never null
*/
public ResultSet executeQuery(String query) {
try {
this.stmt = this.conn.createStatement(1005, 1008);
ResultSet results = stmt.executeQuery(query);
return results;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
}
Here is the command I created to claim a donation:
Code:
if (cmd[0].equalsIgnoreCase("claim")) {
new Thread(new Donation(player)).start();
return true;
}
I have performed a real transaction in my webstore and have attempted to claim the donation on the server. Absolutely nothing happens and I get no output in the server console. I cannot tell if it is even trying to establish a connection with the MySQL database. Any insight appreciated! Thanks guys.