How to get the return with executeQuery() in Java?

此生再无相见时 提交于 2019-12-25 02:52:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!