我正在编写一个JDBC程序,我需要获取特定列的总和。
当我执行sysout
时,正在打印的结果是正确的,但问题是,它是打印行大小的时间。我的列大小为882,结果打印为time takes s 5.75
882次(5.75是该列的总和)。
以下是我的代码。
public int getTotalFromDB() throws Exception {
Connection conn = dbConnection.getConn();
Statement stmt, stmt1 = dbConnection.getStmt();
ResultSet rs, rs1 = dbConnection.getRs();
String queryString, queryString1 = null;
dbConnection.createClassForNameForSQLServer();
String userName = "sa";
String password = "mypassword";
String url = "MYDBURL";
dbConnection.createClassForNameForSQLServer();
conn = DriverManager.getConnection(url, userName, password);
stmt = conn.createStatement();
stmt1 = conn.createStatement();
queryString = "SELECT COUNT(*) as CNT from statusTable";
queryString1 = "select sum([Time Taken(minutes)]) as timeTaken from statusTable";
rs = stmt.executeQuery(queryString);
rs.next();
DBCount = rs.getInt("CNT");
rs.close();
stmt.close();
rs1 = stmt1.executeQuery(queryString1);
rs1.next();
timeTaken = rs1.getFloat("timeTaken");
rs1.close();
stmt1.close();
System.out.println("Time taken s " + timeTaken);
return DBCount;
}
调用它的代码如下。
JSP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<form action="GetTheCounts" method="get" id="form1">
<div class="status">
<span class="totalTime">Count is ${DAOCount}/${excelCount}</span> <span
class="efficiency"></span>
</div>
</form>
<script type="text/javascript">
document.getElementById("form1").submit();
</script>
</body>
</html>
控制器
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
GetTheCountsDAO getTheCountsDAO = new GetTheCountsDAO();
try {
int excelCount = getTheCountsDAO.getTotalFromExcel();
int DAOCount = getTheCountsDAO.getTotalFromDB();
request.setAttribute("DAOCount", DAOCount);
request.setAttribute("excelCount", excelCount);
request.getRequestDispatcher("counts.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
请让我知道我哪里出错了,怎么才打印一次。
由于
答案 0 :(得分:0)
使用此代码
此代码可以帮助您打印每行中列值的总和并返回列数。
while(rs1.next())
{
timeTaken = rs1.getFloat("timeTaken");
System.out.println("Time taken s " + timeTaken);
}
return DBCount;