java mysql计数行数
我创建此代码是为了计算表中的行数。但是,我无法返回计数的数字,错误是“无法从结果类型为void的方法返回值”。有人能告诉我“我的错误”在哪里吗?非常感谢java mysql计数行数,java,mysql,sql,Java,Mysql,Sql,我创建此代码是为了计算表中的行数。但是,我无法返回计数的数字,错误是“无法从结果类型为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"); // Setu
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) {
}
改变
public void num() throws Exception {
到
您正在从变量count
返回值,该变量的类型为int
,因此该方法的返回类型也应为int
您还应该确保通过代码的每个执行路径中都有一个return
语句,包括catch
块中的异常处理程序(否则您将收到一条“missing return statement”错误消息)。但是,最好避免捕获所有异常(如您的异常)的catch
语句。此外,忽略(即不处理)catch块中的异常通常会导致难以诊断的问题,这是一种不好的做法
代码还有其他问题:除了count
之外,没有声明任何变量
请注意,您可以使用以下SQL语句直接获取行数:
select count(*) from testdb.emg
这避免了将表testdb.emg
中的所有数据发送到应用程序,对于大型表来说速度更快
public void num() throws Exception {
应该是
public int num() throws Exception {
试试下面的代码
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) {
}
下面是错误
public void num()引发异常{
应该是
public int num() throws Exception {
public int num()引发异常{
从testdb.emg中选择count(*)
我用了法希姆·帕克的答案,稍作改动 `
public int num()引发异常{
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
connect=DriverManager.getConnection(“jdbc:mysql://localhost/testdb?"
+“user=root&password=”);
语句=connect.createStatement();
resultSet=statement.executeQuery(“”);
resultSet.last();//转到最后一行;
return resultSet.getRow();//获取等于行数的行数
}捕获(例外e){
}
`作为旁注:当您只想计算行数时,类似testdb.emg的
SELECT count(*)这样的查询效率更高。您似乎缺少一个}
复制粘贴错误?我已经更改了它,但是,这里有一条消息“缺少返回语句”。我确实将返回语句放在了“返回计数”如果引发异常,则不会返回。您可以在异常中放置return语句,也可以在异常下放置on。在这种情况下,您可能会使用0或-1作为返回值。我已经更改了它,但是,出现了此消息“缺少return语句”。我确实放置了return语句“return count;”我添加了解释:您没有从catch
块返回任何内容。
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;
}
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) {
}