JSP里连接数据库实现数据插入出错 求帮助。

问题描述

首先有两个页面insert.jsp和insert-success.jsp, 在insert.jsp里面连接数据库dbs,把表stu中的数据输出,然后添加一条记录,添加之后,跳转到insert_success.jsp 输出添加之后的stu里的所有记录。表结构如下:insert.jsp源码<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ page import="java.sql.*"%><html><head><title>Insert Record Test...</title></head><body><p><font color=green>data inserting test........</font></p><hr color="brown"><form action="insert_success.jsp" method="post"><table ><tr><td>input StuID</td><td><input type=text name="no"></td></tr><tr><td>input StuName</td> <td><input type=text name="name"></td></tr><tr><td>input ChineseScore</td> <td><input type=text name="chiscore"></td></tr><tr><td>input MathScore</td><td><input type=text name="mathscore"></td></tr><tr><td>input EngScore</td><td><input type=text name="engscore"></td></tr></table><br><input type="submit" value="click to add record"/></form><hr color="brown"><br><p> <font color=green>添加之前的数据记录:</font></p><%try{Class.forName("oracle.jdbc.driver.OracleDriver");}catch (ClassNotFoundException e){}String ConStr = "jdbc:oracle:thin:@" ;String ServerName = "localhost";String url =ConStr+ServerName+":1521:dbs";String SqlQuery = "SELECT * FROM stu";Connection con = DriverManager.getConnection(url,"soo","qq");Statement sql=con.createStatement();ResultSet rs = sql.executeQuery(SqlQuery);out.print("<table >");out.print("<TH>学号");out.print("<TH>姓名");out.print("<TH>语文");out.print("<TH>数学");out.print("<TH>英语");while (rs.next()){out.print("<tr>");out.print("<td>"+rs.getInt("no")+"</td>");out.print("<td>"+rs.getString("name")+"</td>");out.print("<td>"+rs.getInt("chinese")+"</td>");out.print("<td>"+rs.getInt("math")+"</td>");out.print("<td>"+rs.getInt("english")+"</td>");out.print("</tr>");}out.print("</table>");rs.close();sql.close();con.close();%></body></html> insert_success.jsp源码<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ page import="java.sql.*" %><html><head><title>New Records!</title></head><body><br><%try {Class.forName("oracle.jdbc.driver.OracleDriver");}catch (ClassNotFoundException e){}String name = request.getParameter("name");String no=request.getParameter("no");String chiscore = request.getParameter("chiscore");String mathscore = request.getParameter("mathscore");String engscore = request.getParameter("engscore");out.print("<table border>");out.print("<TH>StuID");out.print("<TH>StuName");out.print("<TH>ChineseScore");out.print("<TH>MathScore");out.print("<TH>EnglishScore");String ConStr = "jdbc:oracle:thin:@";String ServerName = "localhost";String url = ConStr+ServerName+":1521:dbs";Connection con = DriverManager.getConnection(url,"soo","qq");Statement sql = con.createStatement();String SQLQuery = "INSERT INTO stu VALUES("+no+",'"+name+"',"+chiscore+","+mathscore+","+engscore+")";out.print(SQLQuery);ResultSet rs = sql.executeQuery(SQLQuery);while(rs.next()){out.print("<tr>");out.print("<td>"+rs.getInt("no")+"</td>");out.print("<td>"+rs.getString("name")+"</td>");out.print("<td>"+rs.getInt("chinese")+"</td>");out.print("<td>"+rs.getInt("math")+"</td>");out.print("<td>"+rs.getInt("english")+"</td>");out.print("</tr>");}out.print("</table>");rs.close();con.close();sql.close();%></body></html>insert.jsp点击 click to add record之后出现如下错误:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /insert_success.jsp at line 3835: ResultSet rs = sql.executeQuery(SQLQuery);36: while(rs.next()){37: out.print("<tr>");38: out.print("<td>"+rs.getInt("no")+"</td>");39: out.print("<td>"+rs.getString("name")+"</td>");40: out.print("<td>"+rs.getInt("chinese")+"</td>");41: out.print("<td>"+rs.getInt("math")+"</td>");Stacktrace:org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)root cause javax.servlet.ServletException: java.sql.SQLException: ORA-00900: 无效 SQL 语句org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)org.apache.jsp.insert_005fsuccess_jsp._jspService(insert_005fsuccess_jsp.java:113)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)root cause java.sql.SQLException: ORA-00900: 无效 SQL 语句oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:283)oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:278)oracle.jdbc.driver.T4C8Odscrarr.receive(T4C8Odscrarr.java:214)oracle.jdbc.driver.T4CStatement.doDescribe(T4CStatement.java:723)oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3276)oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1914)oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:1563)org.apache.jsp.insert_005fsuccess_jsp._jspService(insert_005fsuccess_jsp.java:92)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.--------------------------------------------------------------------------------Apache Tomcat/6.0.29实在不知道哪里错了。。希望路过的大家帮忙看下。 问题补充:首先有两个页面insert.jsp和insert-success.jsp, 在insert.jsp里面连接数据库dbs,把表stu中的数据输出,然后添加一条记录,添加之后,跳转到insert_success.jsp 输出添加之后的stu里的所有记录。表结构如下:insert.jsp源码<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ page import="java.sql.*"%><html><head><title>Insert Record Test...</title></head><body><p><font color=green>data inserting test........</font></p><hr color="brown"><form action="insert_success.jsp" method="post"><table ><tr><td>input StuID</td><td><input type=text name="no"></td></tr><tr><td>input StuName</td> <td><input type=text name="name"></td></tr><tr><td>input ChineseScore</td> <td><input type=text name="chiscore"></td></tr><tr><td>input MathScore</td><td><input type=text name="mathscore"></td></tr><tr><td>input EngScore</td><td><input type=text name="engscore"></td></tr></table><br><input type="submit" value="click to add record"/></form><hr color="brown"><br><p> <font color=green>添加之前的数据记录:</font></p><%try{Class.forName("oracle.jdbc.driver.OracleDriver");}catch (ClassNotFoundException e){}String ConStr = "jdbc:oracle:thin:@" ;String ServerName = "localhost";String url =ConStr+ServerName+":1521:dbs";String SqlQuery = "SELECT * FROM stu";Connection con = DriverManager.getConnection(url,"soo","qq");Statement sql=con.createStatement();ResultSet rs = sql.executeQuery(SqlQuery);out.print("<table >");out.print("<TH>学号");out.print("<TH>姓名");out.print("<TH>语文");out.print("<TH>数学");out.print("<TH>英语");while (rs.next()){out.print("<tr>");out.print("<td>"+rs.getInt("no")+"</td>");out.print("<td>"+rs.getString("name")+"</td>");out.print("<td>"+rs.getInt("chinese")+"</td>");out.print("<td>"+rs.getInt("math")+"</td>");out.print("<td>"+rs.getInt("english")+"</td>");out.print("</tr>");}out.print("</table>");rs.close();sql.close();con.close();%></body></html> insert_success.jsp源码<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ page import="java.sql.*" %><html><head><title>New Records!</title></head><body><br><%try {Class.forName("oracle.jdbc.driver.OracleDriver");}catch (ClassNotFoundException e){}String name = request.getParameter("name");String no=request.getParameter("no");String chiscore = request.getParameter("chiscore");String mathscore = request.getParameter("mathscore");String engscore = request.getParameter("engscore");out.print("<table border>");out.print("<TH>StuID");out.print("<TH>StuName");out.print("<TH>ChineseScore");out.print("<TH>MathScore");out.print("<TH>EnglishScore");String ConStr = "jdbc:oracle:thin:@";String ServerName = "localhost";String url = ConStr+ServerName+":1521:dbs";Connection con = DriverManager.getConnection(url,"soo","qq");Statement sql = con.createStatement();String SQLQuery = "INSERT INTO stu VALUES("+no+",'"+name+"',"+chiscore+","+mathscore+","+engscore+")";out.print(SQLQuery);ResultSet rs = sql.executeQuery(SQLQuery);while(rs.next()){out.print("<tr>");out.print("<td>"+rs.getInt("no")+"</td>");out.print("<td>"+rs.getString("name")+"</td>");out.print("<td>"+rs.getInt("chinese")+"</td>");out.print("<td>"+rs.getInt("math")+"</td>");out.print("<td>"+rs.getInt("english")+"</td>");out.print("</tr>");}out.print("</table>");rs.close();con.close();sql.close();%></body></html>insert.jsp点击 click to add record之后出现如下错误:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /insert_success.jsp at line 3835: ResultSet rs = sql.executeQuery(SQLQuery);36: while(rs.next()){37: out.print("<tr>");38: out.print("<td>"+rs.getInt("no")+"</td>");39: out.print("<td>"+rs.getString("name")+"</td>");40: out.print("<td>"+rs.getInt("chinese")+"</td>");41: out.print("<td>"+rs.getInt("math")+"</td>");Stacktrace:org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)root cause javax.servlet.ServletException: java.sql.SQLException: ORA-00900: 无效 SQL 语句org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)org.apache.jsp.insert_005fsuccess_jsp._jspService(insert_005fsuccess_jsp.java:113)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)root cause java.sql.SQLException: ORA-00900: 无效 SQL 语句oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:283)oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:278)oracle.jdbc.driver.T4C8Odscrarr.receive(T4C8Odscrarr.java:214)oracle.jdbc.driver.T4CStatement.doDescribe(T4CStatement.java:723)oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3276)oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1914)oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:1563)org.apache.jsp.insert_005fsuccess_jsp._jspService(insert_005fsuccess_jsp.java:92)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.--------------------------------------------------------------------------------Apache Tomcat/6.0.29实在不知道哪里错了。。希望路过的大家帮忙看下。

解决方案

修改如下,供参考:String SQLQuery = "INSERT INTO stu VALUES("+no+",'"+name+"',"+chiscore+","+mathscore+","+engscore+")"; // 先更新sql.executeUpdate(SQLQuery);// 再查询SqlQuery = "SELECT * FROM stu";ResultSet rs = sql.executeQuery(SQLQuery); while(rs.next()){ out.print("<tr>"); out.print("<td>"+rs.getInt("no")+"</td>"); out.print("<td>"+rs.getString("name")+"</td>"); out.print("<td>"+rs.getInt("chinese")+"</td>"); out.print("<td>"+rs.getInt("math")+"</td>"); out.print("<td>"+rs.getInt("english")+"</td>"); out.print("</tr>"); }
解决方案二:
String SQLQuery = "INSERT INTO stu VALUES("+no+",'"+name+"',"+chiscore+","+mathscore+","+engscore+")"; out.print(SQLQuery); ResultSet rs = sql.executeQuery(SQLQuery); 这是一条UPdate语句,不应该用executeQuery,应该用executeUpdate

时间: 2025-01-29 05:19:21

JSP里连接数据库实现数据插入出错 求帮助。的相关文章

求教数据插入出错的问题

问题描述 strSQL="InsertInto访客留言簿(留言者,电子邮件,IP,留言内容,留言日期)Values('"&UserName.Text&"','"&Email.Text&"','"&Request.UserHostAddress&"','"&Content.Text&"','"&System.DateTime.Now&am

java-把数据+类型写入字节数组里这个程序总错,求大神指出错的地方,或者给出正确的代码,感激不尽

问题描述 把数据+类型写入字节数组里这个程序总错,求大神指出错的地方,或者给出正确的代码,感激不尽 package 处理流; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import

jsp-怎么在action里获取到JSP里的数据

问题描述 怎么在action里获取到JSP里的数据 如题 这些数据包括select radio text的,中间经过struts,求大神指导 解决方案 Struts2 action里获取ip地址 解决方案二: struts1~还是2. 2有OHNL表达式,可直接封装到对象.属性就好.1的话,可以传参,在action类里写private String xx生成get.set方法就可以使用该属性. 解决方案三: 在将表单提交给action之后通过action对应的form(struts1.x)或ac

jsp文件连接数据库后能添加数据,但是不能修改和删除

问题描述 jsp文件连接数据库后能添加数据,但是不能修改和删除 代码如下: 数据库操作的代码: <%@ page language="java" import="java.util.*" contentType="text/html;charset=GB2312"%> <%@ page import="java.sql.*" %> 新闻编辑 <% request.setCharacterEncod

eclipse中jsp能连接数据库但是访问不了数据库里面的表!!!

问题描述 eclipse中jsp能连接数据库但是访问不了数据库里面的表!!! 代码如下: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//D

jsp页面多条数据提交到后台,并插入到数据库

问题描述 jsp页面多条数据提交到后台,并插入到数据库 jsp页面点击提交,后台怎么接收和并插入数据库? 数据库是mysql,先谢谢了. 最主要一点的忘记写了,就是name是相同的三组数据同时提交. 解决方案 给个邮箱,我发你一份项目代码.祝好! 解决方案二: 可能上面还不全,如果有需要你可以根据自己需要修改 如果回答对您有帮助,请采纳 解决方案三: 表单提交的话后台应该有一个action或者一个controller进行接收并进行数据库操作,看你的工程用的spingMVC还是StructsMVC

hibernate-刚学Hibernate,遇到数据插入不全的问题,求大神!

问题描述 刚学Hibernate,遇到数据插入不全的问题,求大神! 代码: package com.java.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; im

服务器上的access数据插入更新出错,查询正常

问题描述 今天修改了数据库某表的一个字段,重新上传后,就出问题了,能正常查询数据,但是插入更新数据是出错.本地能正常运行,我觉得是权限的问题,但是不知道怎样修改.求指导.... 解决方案 解决方案二:IIS的权限改一下,要设置可以写入.解决方案三:怎样设置,能具体点吗引用楼主shryap的回复: 今天修改了数据库某表的一个字段,重新上传后,就出问题了,能正常查询数据,但是插入更新数据是出错.本地能正常运行,我觉得是权限的问题,但是不知道怎样修改.求指导.... 解决方案四:怎样设置,能具体点吗引

怎样在JavaScript里写一个swing把数据插入数据库_基础知识

最终的目标是想这样的,在JavaScript里写一个swing来实现确定取消,来决定是否执行这个功能的,但是在执行的过程中,出现了一点问题,每次执行时,都是直接就会插入把数据插入数据库,不能控制了,想要知道应该怎样来解决这个问题,详情要参考下面的代码详情: 复制代码 代码如下: <% boolean foo=false; if (((theqingjiadays<3)&&(thetiqiandays>=1))||((theqingjiadays<10) &&