标题:[已解决]初学JSP,遇到executeUpdate()调用错误
只看楼主
lon91ong
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-7-13
 问题点数:0 回复次数:5 
[已解决]初学JSP,遇到executeUpdate()调用错误

如下图所示:


在上图的代码中,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编辑过]

搜索更多相关主题的帖子: JSP Microsoft 初学 Driver 
2007-07-13 17:25
guoxhvip
Rank: 8Rank: 8
来 自:聖西羅南看臺
等 级:贵宾
威 望:44
帖 子:4052
专家分:135
注 册:2006-10-8
得分:0 
最好把代码贴出来

愛生活 && 愛編程
2007-07-13 17:48
lon91ong
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-7-13
得分:0 

源代码:
<%@ 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>

2007-07-13 18:04
ricoclindy
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-5-13
得分:0 
回复:(lon91ong)源代码:
String sql="insert into 图书明细表 values("+bookid+",'"+bookname+"','"+bookpress+"',"+bookprice+","+bookdate+")";
图书明细表的字段除主键外都可为NULL吗
如果有不能为NULL的 把表中的所有字段都写在SQL里
2007-07-14 06:32
lon91ong
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-7-13
得分:0 

我刚检查了一下,除了主键为必填外,其他的都是非必填
这个就是你说的“除主键外都可为NULL”吧?!

2007-07-14 12:52
lon91ong
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-7-13
得分:0 

终于找到问题所在了,主要是数据库中有几个字段的格式要求是数字,这样修改就没问题了
<%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);
%>

2007-08-09 21:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-155161-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.291631 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved