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