简体   繁体   English

在 Java JDBC 中查找行数的正确方法

[英]Correct way to find rowcount in Java JDBC

I have tried different ways to get the row count in java JDBC, nut none seemed to be giving the correct result.我尝试了不同的方法来获取 java JDBC 中的行数,但似乎没有一个给出正确的结果。 Is there anything wrong that I am doing ?我做错了什么吗?

Even though the customer table is empty and I should be getting the rowcount as 0, I don't understand why I get a non zero rowcount value.即使客户表是空的并且我应该将行数设为 0,但我不明白为什么我会得到一个非零行数值。

Method 1 -方法1 -

query = "SELECT * FROM customer WHERE username ='"+username+"'";
rs = stmt.executeQuery(query);
ResultSetMetaData metaData = rs.getMetaData();
rowcount = metaData.getColumnCount();

Method 2 -方法2 -

query = "SELECT * FROM customer WHERE username ='"+username+"'";
rs = stmt.executeQuery(query);
rowcount = rs.last() ? rs.getRow() : 0;

See this snippet of code:请看这段代码:

import java.io.*;
import java.sql.*;

public class CountRows{
    public static void main(String[] args) {
        System.out.println("Count number of rows in a specific table!");
        Connection con = null;
        int count = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial","root","root");
            try {
                Statement st = con.createStatement();
                BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter table name:");
                String table = bf.readLine();
                ResultSet res = st.executeQuery("SELECT COUNT(*) FROM "+table);
                while (res.next()){
                    count = res.getInt(1);
                }
                System.out.println("Number of row:"+count);
            }
            catch (SQLException s){
                System.out.println("SQL statement is not executed!");
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

This the way I use to get the row count in Java:这是我用来在 Java 中获取行数的方式:

String query = "SELECT * FROM yourtable";

Statement st = sql.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY );
ResultSet rs = st.executeQuery(query);

int rows = 0;
rs.last();
rows = rs.getRow();
rs.beforeFirst();

System.out.println("Your query have " + rows + " rows.");

When you working with JDBC that does not support TYPE_FORWARD_ONLY use this method to get rowcount.当您使用不支持TYPE_FORWARD_ONLY的 JDBC 时,请使用此方法获取行数。

Statement s = cd.createStatement();
ResultSet r = s.executeQuery("SELECT COUNT(*) AS rowcount FROM TableName");
r.next();
int count = r.getInt("rowcount");
r.close();
System.out.println("MyTable has " + count + " row(s).");

You can Get Row count using above method.您可以使用上述方法获取行数。

Thanks..谢谢..

In Android, having no results returns an error.在 Android 中,没有结果会返回错误。 So check this case before incrementing count in while(resultset.next())因此,在while(resultset.next())中增加计数之前检查这种情况

if(resultset!=null)
{
//proceed with incrementing row count function
}
else
{
// No resultset found
}

Just iterate and count只需迭代和计数

ResultSet result = sta.executeQuery("SELECT * from A3");
            int k=0;
            while(result.next())
                k++;

            System.out.print(k); //k is the no of row

由于方法名称指定metaData.getColumnCount()将返回结果集中的总列数,但不返回总行数(计数)。

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

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