如何从 JDBC 中的 ResultSet 获取行数和列数
行数
ResultSet 接口的last()方法将光标移动到 ResultSet 的最后一行,而getRow()方法返回当前行的索引/位置。
因此,要获取行数,请使用last()方法将光标移动到最后一行,然后使用 getRow() 方法获取该(最后)行的位置。
让我们使用如下所示的 CREATE 语句在MySQL数据库中创建一个名为 MyPlayers 的表:
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用 INSERT 语句在MyPlayers表中插入 7 条记录:
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
以下 JDBC 程序建立与数据库的连接,并将 MyPlayers 表的内容检索到 ResultSet 对象中,并检索表中的行数并显示它。
示例
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class RS_Row_Count { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Retrieving the data ResultSet rs = stmt.executeQuery("select * from MyPlayers"); //Moving the cursor to the last row rs.last(); System.out.println("Table contains "+rs.getRow()+" rows"); } }
输出
Connection established...... Table contains 7 rows
列数
您可以使用ResultSetMetaData接口的getColumnCount()方法获取表中的列数。调用此方法时,它将返回一个整数,表示当前 ResultSet 对象中表的列数。
//Retrieving the ResultSetMetaData object ResultSetMetaData rsmd = rs.getMetaData(); //getting the column type int column_count = rsmd.getColumnCount();
以下 JDBC 程序建立与数据库的连接,检索MyPlayers表的ResultSetMetaData对象,并打印其中的列数。
示例
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.sql.ResultSetMetaData; public class RS_ColumnCount { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Retrieving the data ResultSet rs = stmt.executeQuery("select * from MyPlayers"); ResultSetMetaData rsMetaData = rs.getMetaData(); //Moving the cursor to the last row System.out.println("Table contains "+rsMetaData.getColumnCount()+" columns"); } }
输出
Connection established...... Table contains 6 columns
广告