gpt4 book ai didi

java mysql 计算行数

转载 作者:可可西里 更新时间:2023-11-01 06:53:17 25 4
gpt4 key购买 nike

我创建此代码是为了计算表格中的行数。但是,我无法返回计数,并显示错误“无法从结果类型为 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) {
}

最佳答案

试试下面的代码

 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) {
}

下面是错误

  1. public void num() 抛出异常 {

    应该是

    public int num() 抛出异常 {

  2. 要计算总行数,您应该使用查询 select count(*) from testdb.emg

如果有任何问题,请告诉我。

关于java mysql 计算行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com