日期:2014-05-17  浏览次数:20805 次

SQL sum函数问题
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn = DriverManager.getConnection("jdbc:odbc:vote","sa","jintian");
  Statement stmt=conn.createStatement();
  ResultSet tmpRs = stmt.executeQuery("select sum(num) As totalnum from vote");
我一用这个sum就出问题,不用就没事,这是什么原因。。
  tmpRs.next();
  tot_num = tmpRs.getInt("totalnum");
  tmpRs.close();
错误代码:
exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /details.jsp at line 22

19: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
20: Connection conn = DriverManager.getConnection("jdbc:odbc:vote","sa","jintian");
21: Statement stmt=conn.createStatement();
22: ResultSet tmpRs = stmt.executeQuery("select sum(num) As totalnum from vote");
23: tmpRs.next();
24: tot_num = tmpRs.getInt("totalnum");
25: tmpRs.close();


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:517)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:409)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]操作数数据类型 nchar 对于 sum 运算符无效。
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:898)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:827)
org.apache.jsp.details_jsp._jspService(details_jsp.java:127)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:73)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:378)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]操作数数据类型 nchar 对于 sum 运算符无效。
sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
org.apache.jsp.details_jsp._jspService(details_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:73)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:378)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

------解决方案--------------------
sum不了字符串数据类型·
try:
你把num的数据类型改下,或者强制进行转换·
------解决方案--------------------
数据库里的num是不是有非数字的数据或null数据吧
sum是