如何在Java中使用executeQuery()获得返回?

时间:2014-06-02 01:52:17

标签: java mysql sql database bukkit

我有这段代码;

String sql = "UPDATE Players SET Losses=Losses+1 WHERE UUID='" + p.getUniqueId() + "';"; 
stmt.executeUpdate(sql);

如何获取特定Losses的当前Player p

4 个答案:

答案 0 :(得分:1)

SELECT查询将返回它,但你真正的问题是什么?

String sql =" SELECT column_name,column_name FROM table_name WHERE column_name ="" ;&#34 ;;

答案 1 :(得分:1)

我会使用Select for Update,类似这样的东西 -

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");
}

答案 2 :(得分:0)

您可以获得如下所示的损失。看起来真是微不足道,猜猜你是一个绝对的初学者。

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");
}

答案 3 :(得分:0)

您必须制作SELECT语句。就像选择SELECT FROM FROM WHERS UUID ='“+ p.getUniqueId()+”';“; 然后使用Statement的executeQuery()返回ResultSet。您可以使用迭代器从ResultSet对象中提取数据。