简体   繁体   English

Java JDBC:如何通过执行SQL查询来获取表的int大小

[英]Java JDBC: how to get int size of table, by exectuning SQL query

Part of my code looks like this: 我的部分代码如下所示:

int size = 0;

        try {

            String query = "SELECT count(*) FROM users";

            statement = connection.prepareStatement(query);
            statement.execute(query);

I don't know what to do to set size of table to my int size. 我不知道该怎么做才能将表的大小设置为我的int大小。

The result set will contain the value of numner of rows. 结果集将包含行数的值。 get the value into your size variable. 将值放入您的size变量中。

  ResultSet result = statement.executeQuery(query);
  while (result.next()){
  size= result.getInt(1);
  }
  System.out.println("Number of row:"+size);

int size = 0; int size = 0;

    try {

        String query = "SELECT count(*) FROM users";

        statement = connection.prepareStatement(query);
        ResultSet rs = statement.execute(query);
        size = rs.getInt(1);

rs.getInt(1) will return the first row from the query as int rs.getInt(1)将以int形式返回查询的第一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM