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;//由于rs.first()已经在第一个位置了,而下面的循环又调用了next(),所以此处count从1开始
while(rs.next()){
count++;
}
System.out.println("行数:"+count);
//第三种方法:
sql = "select count(*) from homework";
rs = sta.executeQuery(sql);
rs.next();
int row = rs.getInt(1);
System.out.println("行数:"+row);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
非自创来源:https://blog.csdn.net/qq_40673345/article/details/79039624