如何获得JDBC中的行数?
SQLCount()
函数返回表中的行数。使用此方法,您可以获取表中的行数。
select count(*) from TABLE_NAME;
假设我们已经建立了与MySQL的连接,并使用Statement对象以如下方式在名为mydatabase的数据库中创建了一个表:
//创建Statement对象 Statement stmt = con.createStatement(); //查询创建表 String query = "CREATE TABLE Cricketers_Data( " + "First_Name VARCHAR(255), " + "Last_Name VARCHAR(255), " + "Date_Of_Birth Date, " + "Place_Of_Birth VARCHAR(255), " + "Country VARCHAR(255))"; //执行查询 stmt.execute(query); System.out.println("Table created......");
在此表中,我们使用准备好的语句插入了5列:
//Query to insert values Query = "INSERT INTO Cricketers_Data values (?, ?, ?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, "Shikhar"); pstmt.setString(2, "Dhawan"); pstmt.setDate(3, new Date(376401869000L)); pstmt.setString(4, "Delhi"); pstmt.setString(5, "India"); pstmt.executeUpdate(); pstmt.setString(1, "Jonathan"); pstmt.setString(2, "Trott"); pstmt.setDate(3, new Date(356788333000L)); pstmt.setString(4, "CapeTown"); pstmt.setString(5, "SouthAfrica"); pstmt.executeUpdate(); pstmt.setString(1, "Kumara"); pstmt.setString(2, "Sangakkara"); pstmt.setDate(3, new Date(246801133000L)); pstmt.setString(4, "Matale"); pstmt.setString(5, "Srilanka"); pstmt.executeUpdate(); pstmt.setString(1, "Virat"); pstmt.setString(2, "Kohli"); pstmt.setDate(3, new Date(546781933000L)); pstmt.setString(4, "Delhi"); pstmt.setString(5, "India"); pstmt.executeUpdate(); pstmt.setString(1, "Rohit"); pstmt.setString(2, "Sharma"); pstmt.setDate(3, new Date(546781933000L)); pstmt.setString(4, "Nagpur"); pstmt.setString(5, "India"); pstmt.executeUpdate();
这使得Cricketers_Data表看起来像:
+------------+------------+---------------+----------------+-------------+ | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country | +------------+------------+---------------+----------------+-------------+ | Shikhar | Dhawan | 1981-12-05 | Delhi | India | | Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica | | Kumara | Sangakkara | 1977-10-27 | Matale | Srilanka | | Virat | Kohli | 1987-04-30 | Delhi | India | | Rohit | Sharma | 1987-04-30 | Nagpur | India | +------------+------------+---------------+----------------+-------------+
示例
以下JDBC程序建立与MySQL的连接,并在名为cricketers_data的表中显示行数。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Count_Example { public static void main(String args[]) throws Exception { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //创建Statement对象 Statement stmt = con.createStatement(); //查询以获取表中的行数 String query = "select count(*) from Cricketers_Data"; //执行查询 ResultSet rs = stmt.executeQuery(query); //检索结果 rs.next(); int count = rs.getInt(1); System.out.println("Number of records in the cricketers_data table: "+count); } }
输出结果
Connection established...... Number of records in the cricketers_data table: 5