So I want to send a user to a mysql database with the columns: user, skill, time
Now, I want it to be able to add more then one row for the same user but having different time and etc, how would I do this? Here’s my code for sending it to the database:
public static boolean recentActivity(Player paramPlayer){
try {
Statement statement = con.createStatement();
ResultSet group = statement.executeQuery("SELECT * FROM recentactivity WHERE user = '"+ paramPlayer.getDisplayName() + "'");
if (!group.next() && !paramPlayer.levelTime.equals(""))
statement.execute("INSERT INTO `recentactivity`(`user`, `skill`, `time`) VALUES('"+ paramPlayer.getDisplayName() +"', '"+ paramPlayer.levelledSkill +"', '"+ paramPlayer.levelTime +"')");
} catch (Exception localException) {
return false;
}
return true;
}
Then I have this to use it:
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
String currentTime = sdf.format(dt);
player.levelledSkill = skill;
player.levelTime = ""+currentTime+"";
Hiscores.recentActivity(player);
Any help with this?
Schema:
CREATE TABLE IF NOT EXISTS recentactivity (
user varchar(255) NOT NULL
, skill int(11) NOT NULL
, time varchar(255) NOT NULL
, PRIMARY KEY (user)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
IIUC you want to add more than one row for the same user.
However, you have two places in you code that are preventing you from doing this.
The first piece is in java :
if (!group.next()
It searches if there is already a row for that user, and executes the INSERT only if there is none.
However, that code is there to prevent an SQL exception to occur. In your SQL schema, user is the primary key of the table, so you cannot insert two rows with the same user.
You should change these two places (remove the primary key, remove the !group.next), but first check if the rest of the application will still work with multiple rows pertaining to the same user, cause it seems like it has been designed to prevent that situation more than to allow it.