【发布时间】:2012-01-22 11:14:11
【问题描述】:
我创建此代码是为了计算表中的行数。但是,我无法返回计数的数字,并显示“无法从结果类型为 void 的方法返回值”的错误。有人可以告诉我我的错误在哪里吗?非常感谢!
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) {
}
【问题讨论】:
-
附带说明:当您只想计算行数时,像
SELECT COUNT(*) FROM testdb.emg这样的查询效率更高 -
您似乎遗漏了
}复制粘贴错误?