[英]java mysql count number of rows
I created this code to allow me calculate the number of rows in my table.我创建了此代码以允许我计算表中的行数。 However, I'm not able to return the counted number with an error saying "cannot return a value from method whose result type is void."
但是,我无法返回计数的数字,并显示“无法从结果类型为 void 的方法中返回值”的错误。 Could someone show me where' my error?
有人可以告诉我我的错误在哪里吗? Thanks alot!
非常感谢!
public void num() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
+ "user=root&password=");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
resultSet = statement.executeQuery("select * from testdb.emg");
int count = 0;
while (resultSet.next()) {
count++;
}
return count;
} catch (Exception e) {
}
Try below code试试下面的代码
public int num() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
+ "user=root&password=");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
resultSet = statement.executeQuery("select count(*) from testdb.emg");
while (resultSet.next()) {
return resultSet.getInt(1);
}
} catch (Exception e) {
}
Below were error下面是错误
public void num() throws Exception {
should be应该
public int num() throws Exception {
For counting total rows you should use query select count(*) from testdb.emg
要计算总行数,您应该使用
select count(*) from testdb.emg
查询select count(*) from testdb.emg
Let me know incase of any problem.如果有任何问题,请告诉我。
Change改变
public void num() throws Exception {
to到
public int num() throws Exception {
You are returning value from variable count
which is of type int
therefore the return type of the method should be int
as well.您正在从
int
类型的变量count
返回值,因此该方法的返回类型也应该是int
。
You should also make sure there is a return
statement in every execution path through your code including the exception handler in the catch
blocks (or you will get a "missing return statement" error message).您还应该确保在通过您的代码的每个执行路径中都有一个
return
语句,包括catch
块中的异常处理程序(否则您将收到“缺少 return 语句”错误消息)。 However, it is best to avoid catch
statements which catch all exceptions (like yours).但是,最好避免捕获所有异常的
catch
语句(如您的)。 Also, ignoring (ie not handling) exceptions in the catch block often leads to hard to diagnose problems and is a bad practice.此外,忽略(即不处理)catch 块中的异常通常会导致难以诊断问题,这是一种不好的做法。
There are also other problems with the code: with the exception of count
none of your variables have been declared.代码还有其他问题:除了
count
之外,没有任何变量被声明。
Note that you may use the following SQL statement to obtain the number of rows directly:请注意,您可以使用以下 SQL 语句直接获取行数:
select count(*) from testdb.emg
This avoids sending all of the data from table testdb.emg
to your application and is much faster for big tables.这避免了将表
testdb.emg
所有数据发送到您的应用程序,并且对于大表要快得多。
How to get count(*) mysql data table in java. java中如何获取count(*)mysql数据表。 TRY IT:
尝试一下:
public int getRowNumber(){
int numberRow = 0;
Connection mysqlConn = DriverManager.getConnection(HOST, USER_ID, PASSWORD);
try{
mysqlConn.getConnection();
String query = "select count(*) from dataTable";
PreparedStatement st = mysqlConn.preparedStatement(query);
ResultSet rs = st.executeQuery();
while(rs.next()){
numberRow = rs.getInt("count(*)");
}
}catch (Exception ex){
System.out.println(ex.getMessage());
}
return numberRow;
}
I use Fahim Parker answer with a bit change我使用 Fahim Parker 的答案,稍作改动
` `
public int num() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
+ "user=root&password=");
statement = connect.createStatement();
resultSet = statement.executeQuery("<your query statement>");
resultSet.last(); //go to last row;
return resultSet.getRow(); //get row number which is equal to rows count
} catch (Exception e) {
}
` `
public void num() throws Exception {
应该
public int num() throws Exception {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.