数据库表中的列总和返回0

时间:2013-10-04 02:15:06

标签: mysql sql database

我正在使用mySql数据库。

我创建了一种计算列总和的方法。它工作正常。

public int calculate(String department) {
    int sum = 0;
    try {
        BaseDao baseDao = new BaseDao();
        Connection connection = baseDao.getConnection();
        String query = "select sum(Allocation) from test.Employee where Department=?";
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, department);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int c=resultSet.getInt(1);
            sum = sum+c;
        }
        System.out.println(sum);
        }catch(Exception e) {
        e.printStackTrace();
    }
    return sum; 
}

我还创建了另一种方法,我在其中计算列的总和,并且我提到了两个条件,但它不起作用。它显示总和为0.

    public int calculate2(String managerId, String managerId2) {
    int sum = 0;
    try {
        BaseDao baseDao = new BaseDao();
        Connection connection = baseDao.getConnection();
        String query = "select sum(Allocation) from test.Employee where EmployeeId=? and Manager=?";
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, managerId);
        preparedStatement.setString(2, managerId2);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int c = resultSet.getInt(1);
            sum=sum+c;
        }
        System.out.println(sum);
        }catch(Exception e) {
        e.printStackTrace();
    }
    return sum;
}

这是错误日志

Oct 03, 2013 9:06:22 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/SavvisProject] has started
Oct 03, 2013 9:06:22 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/SavvisProject] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Oct 03, 2013 9:06:23 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/SavvisProject] is completed

0 个答案:

没有答案