JDBC query the database data volume

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;


import com.mysql.jdbc.Statement;


public class MySQLDao {
 public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement sta = (Statement) conn.createStatement(ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE);
String sql = "select * from homework";
ResultSet rs  =sta.executeQuery(sql);
rs.last();
System.out.println("行数:"+rs.getRow());
//如果后面需要遍历
rs.first();//将指针移动到第一行
//第二种方法:
int count = 1; // Since rs.first () has a first position, and the following loop and calls the Next (), where it starts a count from
the while (rs.next ()) {
count ++;
}
System.out.println ( "line number:" + COUNT);
// third method:
SQL = "SELECT COUNT (*) from Homework";
RS = sta.executeQuery (SQL);
rs.next ();
row rs.getInt = int (. 1);
System.out.println ( "line number:" + row);
conn.Close ();
} the catch (Exception E) {
e.printStackTrace ();
}

}
}

 

Non-Own Source: https://blog.csdn.net/qq_40673345/article/details/79039624

Guess you like

Origin www.cnblogs.com/gxlaqj/p/11223768.html