java.sql.ResultSet.first()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(461)

本文整理了Java中java.sql.ResultSet.first方法的一些代码示例,展示了ResultSet.first的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ResultSet.first方法的具体详情如下:
包路径:java.sql.ResultSet
类名称:ResultSet
方法名:first

ResultSet.first介绍

[英]Shifts the cursor position to the first row in the ResultSet.
[中]将光标位置移动到结果集中的第一行。

代码示例

代码示例来源:origin: apache/incubator-gobblin

@Override
public boolean isEmpty(String database, String table) throws SQLException {
 String sql = String.format(SELECT_SQL_FORMAT, database, table);
 try (PreparedStatement pstmt = this.conn.prepareStatement(sql); ResultSet resultSet = pstmt.executeQuery();) {
  if (!resultSet.first()) {
   throw new RuntimeException("Should have received at least one row from SQL " + pstmt);
  }
  return 0 == resultSet.getInt(1);
 }
}

代码示例来源:origin: apache/incubator-gobblin

@Override
public boolean isEmpty(String database, String table)
  throws SQLException {
 String sql = String.format(SELECT_SQL_FORMAT, database, table);
 try (PreparedStatement pstmt = this.conn.prepareStatement(sql); ResultSet resultSet = pstmt.executeQuery();) {
  if (!resultSet.first()) {
   throw new RuntimeException("Should have received at least one row from SQL " + pstmt);
  }
  return 0 == resultSet.getInt(1);
 }
}

代码示例来源:origin: apache/incubator-gobblin

@Override
public boolean isEmpty(String database, String table) throws SQLException {
 String sql = String.format(SELECT_SQL_FORMAT, database, table);
 try (PreparedStatement pstmt = this.conn.prepareStatement(sql); ResultSet resultSet = pstmt.executeQuery();) {
  if (!resultSet.first()) {
   throw new RuntimeException("Should have received at least one row from SQL " + pstmt);
  }
  return 0 == resultSet.getInt(1);
 }
}

代码示例来源:origin: apache/incubator-gobblin

pstmt.setString(2, table);
LOG.info("Retrieving column type information from SQL: " + pstmt);
try (ResultSet rs = pstmt.executeQuery()) {
 if (!rs.first()) {
  throw new IllegalArgumentException("No result from information_schema.columns");

代码示例来源:origin: apache/incubator-gobblin

pstmt.setString(2, table);
log.info("Retrieving column type information from SQL: " + pstmt);
try (ResultSet rs = pstmt.executeQuery()) {
 if (!rs.first()) {
  throw new IllegalArgumentException("No result from information_schema.columns");

代码示例来源:origin: apache/incubator-gobblin

pstmt.setString(2, table);
LOG.info("Retrieving column type information from SQL: " + pstmt);
try (ResultSet rs = pstmt.executeQuery()) {
 if (!rs.first()) {
  throw new IllegalArgumentException("No result from information_schema.columns");

代码示例来源:origin: banq/jdonframework

public static int getProductAllCount(DataSource ds, String key, String sql_allcount) throws Exception {
 Connection c = null;
 PreparedStatement ps = null;
 ResultSet rs = null;
 int ret = 0;
 try {
  c = ds.getConnection();
  ps = c.prepareStatement(sql_allcount,
              ResultSet.TYPE_SCROLL_INSENSITIVE,
              ResultSet.CONCUR_READ_ONLY);
  ps.setString(1, key);
  rs = ps.executeQuery();
  if (rs.first()) {
   ret = rs.getInt(1);
  }
 } catch (SQLException se) {
  throw new Exception("SQLException: " + se.getMessage());
 } finally {
  if (rs != null)
   rs.close();
  if (ps != null)
   ps.close();
  if (c != null)
   c.close();
 }
 return ret;
}

代码示例来源:origin: apache/cloudstack

public static boolean releaseGlobalLock(String name) {
  try (Connection conn = getConnectionForGlobalLocks(name, false);) {
    if (conn == null) {
      s_logger.error("Unable to acquire DB connection for global lock system");
      assert (false);
      return false;
    }
    try (PreparedStatement pstmt = conn.prepareStatement("SELECT COALESCE(RELEASE_LOCK(?), 0)");) {
      pstmt.setString(1, name);
      try (ResultSet rs = pstmt.executeQuery();) {
        if (rs != null && rs.first()) {
          return rs.getInt(1) > 0;
        }
        s_logger.error("releaseGlobalLock:RELEASE_LOCK() returns unexpected result");
      }
    }
  } catch (SQLException e) {
    s_logger.error("RELEASE_LOCK() throws exception ", e);
  } catch (Throwable e) {
    s_logger.error("RELEASE_LOCK() throws exception ", e);
  }
  return false;
}

代码示例来源:origin: apache/cloudstack

@Override
public int getProxyActiveLoad(long proxyVmId) {
  TransactionLegacy txn = TransactionLegacy.currentTxn();
  PreparedStatement pstmt = null;
  try {
    pstmt = txn.prepareAutoCloseStatement(GET_PROXY_ACTIVE_LOAD);
    pstmt.setLong(1, proxyVmId);
    ResultSet rs = pstmt.executeQuery();
    if (rs != null && rs.first()) {
      return rs.getInt(1);
    }
  } catch (SQLException e) {
    s_logger.debug("Caught SQLException: ", e);
  }
  return 0;
}

代码示例来源:origin: apache/cloudstack

@Override
public int getProxyStaticLoad(long proxyVmId) {
  TransactionLegacy txn = TransactionLegacy.currentTxn();
  ;
  PreparedStatement pstmt = null;
  try {
    pstmt = txn.prepareAutoCloseStatement(GET_PROXY_LOAD);
    pstmt.setLong(1, proxyVmId);
    ResultSet rs = pstmt.executeQuery();
    if (rs != null && rs.first()) {
      return rs.getInt(1);
    }
  } catch (SQLException e) {
    s_logger.debug("Caught SQLException: ", e);
  }
  return 0;
}

代码示例来源:origin: apache/cloudstack

public static boolean getGlobalLock(String name, int timeoutSeconds) {
  Connection conn = getConnectionForGlobalLocks(name, true);
  if (conn == null) {
    s_logger.error("Unable to acquire DB connection for global lock system");
    return false;
  }
  try (PreparedStatement pstmt = conn.prepareStatement("SELECT COALESCE(GET_LOCK(?, ?),0)");) {
    pstmt.setString(1, name);
    pstmt.setInt(2, timeoutSeconds);
    try (ResultSet rs = pstmt.executeQuery();) {
      if (rs != null && rs.first()) {
        if (rs.getInt(1) > 0) {
          return true;
        } else {
          if (s_logger.isDebugEnabled())
            s_logger.debug("GET_LOCK() timed out on lock : " + name);
        }
      }
    }
  } catch (SQLException e) {
    s_logger.error("GET_LOCK() throws exception ", e);
  } catch (Throwable e) {
    s_logger.error("GET_LOCK() throws exception ", e);
  }
  removeConnectionForGlobalLocks(name);
  closeAutoCloseable(conn, "connection for global lock");
  return false;
}

代码示例来源:origin: apache/cloudstack

rs = pstmt.executeQuery();
if (rs.first()) {
  networkOfferingId = rs.getLong(1);
} else {
  rs = pstmt.executeQuery();
  if (rs.first()) {
    f5HostId = rs.getLong(1);
  } else {
  pstmt.setLong(1, f5HostId);
  rs = pstmt.executeQuery();
  if (rs.first()) {
    f5DeviceId = rs.getLong(1);
  } else {
  pstmt.setLong(1, zoneId);
  rs = pstmt.executeQuery();
  if (rs.first()) {
    srxHostId = rs.getLong(1);
  } else {
  pstmt.setLong(1, srxHostId);
  rs = pstmt.executeQuery();
  if (rs.first()) {
    srxDevivceId = rs.getLong(1);
  } else {

代码示例来源:origin: apache/cloudstack

rs = pstmt.executeQuery();
if (rs.first()) {
  networkOfferingId = rs.getLong(1);
} else {
  rs = pstmt.executeQuery();
  if (rs.first()) {
    f5HostId = rs.getLong(1);
  } else {
  pstmt.setLong(1, f5HostId);
  rs = pstmt.executeQuery();
  if (rs.first()) {
    f5DeviceId = rs.getLong(1);
  } else {
  pstmt.setLong(1, zoneId);
  rs = pstmt.executeQuery();
  if (rs.first()) {
    srxHostId = rs.getLong(1);
  } else {
  pstmt.setLong(1, srxHostId);
  rs = pstmt.executeQuery();
  if (rs.first()) {
    srxDevivceId = rs.getLong(1);
  } else {

代码示例来源:origin: apache/cloudstack

try(ResultSet sel_id_off_rs = sel_id_off_pstmt.executeQuery();) {
  if (sel_id_off_rs.first()) {
    networkOfferingId = sel_id_off_rs.getLong(1);
  } else {
    try(ResultSet sel_id_host_pstmt_rs = sel_id_host_pstmt.executeQuery();) {
      if (sel_id_host_pstmt_rs.first()) {
        f5HostId = sel_id_host_pstmt_rs.getLong(1);
      } else {
    sel_id_ext_pstmt.setLong(1, f5HostId);
    try(ResultSet sel_id_ext_rs = sel_id_ext_pstmt.executeQuery();) {
      if (sel_id_ext_rs.first()) {
        f5DeviceId = sel_id_ext_rs.getLong(1);
      } else {
    sel_id_hostdc_pstmt.setLong(1, zoneId);
    try(ResultSet sel_id_hostdc_pstmt_rs = sel_id_hostdc_pstmt.executeQuery();) {
      if (sel_id_hostdc_pstmt_rs.first()) {
        srxHostId = sel_id_hostdc_pstmt_rs.getLong(1);
      } else {
    sel_id_ext_frwl_pstmt.setLong(1, srxHostId);
    try(ResultSet sel_id_ext_frwl_pstmt_rs = sel_id_ext_frwl_pstmt.executeQuery();) {
      if (sel_id_ext_frwl_pstmt_rs.first()) {
        srxDevivceId = sel_id_ext_frwl_pstmt_rs.getLong(1);
      } else {

代码示例来源:origin: zstackio/zstack

conn.setAutoCommit(true);
pstmt = conn.prepareStatement(String.format("select get_lock('%s', %s)", name, timeout));
ResultSet rs = pstmt.executeQuery();
if (rs == null) {
  String err = "Unable to get DB lock: " + name + ", internal database error happened";
  throw new CloudRuntimeException(err);
} else if (rs.first() && rs.getInt(1) == 0) {
  throw new CloudRuntimeException(String.format("lock[%s] failed, timeout after %s seconds", name, timeout));

代码示例来源:origin: zstackio/zstack

try {
  pstmt = conn.prepareStatement(String.format("select release_lock('%s')", name));
  ResultSet rs = pstmt.executeQuery();
  if (rs == null) {
    throw new CloudRuntimeException("Mysql cannot find lock: " + name);
  } else if (rs.first() && rs.getInt(1) == 0) {
    String err = "Unable to release DB lock: " + name + ", lock: " + name + " is not held by this connection, internal error";
    throw new CloudRuntimeException(err);

代码示例来源:origin: stackoverflow.com

public boolean isLoginFree(String login) throws SQLException {
  PreparedStatement pst = con.prepareStatement("select login " + 
                         "from User " + 
                         "where login=?");
  pst.setString(1, login);
  ResultSet rs = pst.executeQuery();
  return !rs.first();
}

代码示例来源:origin: stackoverflow.com

rs.first();
while( rs.next() ){
  float hourlyRate = rs.getFloat("HourlyRate");
  int employeeID   = rs.getInt("employeeID ");
  //Do your code to calculate and update other database....
  //You need to check for INSERT or UPDATE in the other database.
  //Make one select on the current tupel

  //e.g.
  PreparedStatement checkUpdateOrInsert = conOnOtherDb.prepareStatement("SELECT employeeID FROM otherSchema.otherTable");
  ResultSet rsCheckUpdateOrInsert = checkUpdateOrInsert .executeQuery();
  if( !rsCheckUpdateOrInsert.first() ){
    //On onther DB you must insert
  }//untested, (bool) first should return false on no row?
  else{
    //On onther DB you must update
  }
}

代码示例来源:origin: org.apache.gobblin/gobblin-sql

@Override
public boolean isEmpty(String database, String table) throws SQLException {
 String sql = String.format(SELECT_SQL_FORMAT, database, table);
 try (PreparedStatement pstmt = this.conn.prepareStatement(sql); ResultSet resultSet = pstmt.executeQuery();) {
  if (!resultSet.first()) {
   throw new RuntimeException("Should have received at least one row from SQL " + pstmt);
  }
  return 0 == resultSet.getInt(1);
 }
}

代码示例来源:origin: org.apache.gobblin/gobblin-sql

@Override
public boolean isEmpty(String database, String table) throws SQLException {
 String sql = String.format(SELECT_SQL_FORMAT, database, table);
 try (PreparedStatement pstmt = this.conn.prepareStatement(sql); ResultSet resultSet = pstmt.executeQuery();) {
  if (!resultSet.first()) {
   throw new RuntimeException("Should have received at least one row from SQL " + pstmt);
  }
  return 0 == resultSet.getInt(1);
 }
}

相关文章

微信公众号

最新文章

更多

ResultSet类方法