Mysql 数据库表中返回0的列的总和
我正在使用mySql数据库 我创建了一个方法来计算列的和。它工作得很好Mysql 数据库表中返回0的列的总和,mysql,sql,database,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)
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
您是否尝试输入您试图直接运行到mySql的查询?可能总和实际上是0。您确定数据库中有用于测试EmployeeID和Manager的值的数据吗。另外,假设EmployeeID是Employee表的主键,为什么还需要在manager上进行筛选(如果只有一行包含任何给定的EmployeeID,为什么需要聚合结果)?很抱歉,实际上我需要EmployeeID=?或者经理=?现在,我得到了我的答案。@spydon你是对的。该查询的结果为0。