问题
I have this code;
String sql = "UPDATE Players SET Losses=Losses+1 WHERE UUID='" + p.getUniqueId() + "';";
stmt.executeUpdate(sql);
How can I get the current Losses
for a specific Player p
?
回答1:
You can get the losses like the following. Seems really trivial, guess you are an absolute beginner.
String sql = "SELECT Losses FROM Players WHERE UUID='" + p.getUniqueId() + "';";
ResultSet rs = stmt.executeQuery(sql);
int losses = -1;
if(rs.next()) {
losses = rs.getInt("Losses");
}
回答2:
SELECT query will return it, but what's your real question?
String sql ="SELECT column_name,column_name FROM table_name WHERE column_name ="" ;";
回答3:
I would use a Select for Update, and something like this -
String query = "SELECT Losses FROM Players FOR UPDATE "
+ "Players SET Losses=Losses+1 WHERE UUID=?";
ps = conn.prepareStatement(query,
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
ps.setString(1, p.getUniqueId());
ResultSet rs = ps.executeQuery();
int losses = 0;
if (rs.next()) {
losses = rs.getInt("Losses");
}
回答4:
You'd have to make a SELECT statement. Like SELECT losses FROM Players WHERE UUID='"+p.getUniqueId()+"';"; Then use Statement's executeQuery() which returns a ResultSet. You can use an iterator to extract the data from the ResultSet object.
来源:https://stackoverflow.com/questions/23986348/how-to-get-the-return-with-executequery-in-java