想单击按钮后,插入一条记录到数据库
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<sql:setDataSource
url="jdbc:mysql://localhost:3306/hz_db?characterEncoding=UTF-8"
driver="com.mysql.jdbc.Driver" user="root"
password="root" var="mydb" />
<form action="thisform" method="post">
学号:<input type="text" name="edt_number"><br>
姓名:<input type="text" name="edt_name"><br>
性别:<input type="text" name="edt_sex"><br>
<input type="submit" value="提交" onclick="get_value()">
</form>
<script language="javascript">
function get_value(){
document.thisform.submit();
}
</script>
<%
String p_number=request.getParameter("edt_number");
String p_name=request.getParameter("edt_name");
String p_sex=request.getParameter("edt_sex");
%>
<sql:update var="insert" dataSource="${mydb}" scope="request">
insert into t_student(number, name, sex)
values(?, ?, ?)
<sql:param value="${p_number}"/>
<sql:param value="${p_name}"/>
<sql:param value="${p_sex}"/>
</sql:update>
运行后出错:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException:
insert into t_student(number, name, sex)
values(?, ?, ?)
: Column 'number' cannot be null
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException:
insert into t_student(number, name, sex)
values(?, ?, ?)
: Column 'number' cannot be null
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:907)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.hz_005fdb.update_jsp._jspService(update_jsp.java:107)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'number' cannot be null
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
com.mysql.jdbc.Util.getInstance(Util.java:386)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
org.apache.taglibs.standard.tag.common.sql.UpdateTagSupport.doEndTag(UpdateTagSupport.java:157)
org.apache.jsp.hz_005fdb.update_jsp._jspx_meth_sql_005fupdate_005f0(update_jsp.java:192)
org.apache.jsp.hz_005fdb.update_jsp._jspService(update_jsp.java:100)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)