如下图所示:
在上图的代码中,executeQuery()中被加亮显示,测试正常通过;而executeUpdate()不被加亮显示,测试不能通过,错误代码如下:
exception
org.apache.jasper.JasperException: Exception in JSP: /DataInsertB.jsp:20
17: String strcon = "jdbc:odbc:Driver={Microsoft Access Driver
(*.mdb)};DBQ=D:/Apache/www/DataBase/Book.mdb";
18: Connection con = DriverManager.getConnection(strcon,"","");
19: Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
20: stmt.executeUpdate(sql);
21: sql="select * from 图书明细表";
22: ResultSet rs = stmt.executeQuery(sql); %>
23: <table border="1" align="center" bordercolor="#7188e0">
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:467)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
javax.servlet.ServletException: [Microsoft][ODBC Microsoft Access Driver] 查询值的数目与目标字段中的数目不同。
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.DataInsertB_jsp._jspService(DataInsertB_jsp.java:104)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 查询值的数目与目标字段中的数目不同。
sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
org.apache.jsp.DataInsertB_jsp._jspService(DataInsertB_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
[此贴子已经被作者于2007-8-9 21:37:46编辑过]
----------------解决方案--------------------------------------------------------
最好把代码贴出来
----------------解决方案--------------------------------------------------------
源代码:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP连接SQL数据库</title>
<LINK href=style.css type=text/css rel=stylesheet>
</head>
<body>
<%
int bookid=Integer.parseInt(request.getParameter("bookid"));
String bookname=request.getParameter("bookname");
String bookpress=request.getParameter("bookpress");
String bookprice=request.getParameter("bookprice");
String bookdate=request.getParameter("bookdate");
String sql="insert into 图书明细表 values("+bookid+",'"+bookname+"','"+bookpress+"',"+bookprice+","+bookdate+")";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String strcon = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/Apache/www/DataBase/Book.mdb";
Connection con = DriverManager.getConnection(strcon,"","");
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
sql="select * from 图书明细表";
ResultSet rs = stmt.executeQuery(sql); %>
<table border="1" align="center" bordercolor="#7188e0">
<r bgcolor="d1d1ff"><Th width="100">图书编号</th>
<h width="180">图书名称</th><th width="112">出版社</th>
<h width="112">定价</th><th width="150">日期</th>
</tr>
<%while(rs.next()){%>
tr bgcolor="#f8f8f8" class=time> <td><%=rs.getString("图书编号")%></td>
<td><%=rs.getString("图书名称")%></td><td><%=rs.getString("出版社")%></td>
<td><%=rs.getString("定价")%></td><td><%=rs.getString("日期")%></td>
</tr>
<%}
rs.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>
----------------解决方案--------------------------------------------------------
图书明细表的字段除主键外都可为NULL吗
如果有不能为NULL的 把表中的所有字段都写在SQL里
----------------解决方案--------------------------------------------------------
我刚检查了一下,除了主键为必填外,其他的都是非必填
这个就是你说的“除主键外都可为NULL”吧?!
----------------解决方案--------------------------------------------------------
终于找到问题所在了,主要是数据库中有几个字段的格式要求是数字,这样修改就没问题了
<%request.setCharacterEncoding("gbk");
int id=Integer.parseInt(request.getParameter("bookid"));
String name=request.getParameter("bookname");
String press=request.getParameter("bookpress");
int price=Integer.parseInt(request.getParameter("bookprice"));
String date=request.getParameter("bookdate");
String sql="insert into 图书明细表 values("+id+",'"+name+"','"+press+"',"+price+",'"+date+"')";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String strcon = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/Apache/www/DataBase/Book.mdb";
Connection con = DriverManager.getConnection(strcon,"","");
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
%>
----------------解决方案--------------------------------------------------------