jsp表单插入MYSQL中失败
提交表单的代码不写啦,只列出处理插入数据的JSP页面代码:<%@pageimport="java.sql.*"%><%@pageimport="java.util.*"%...
提交表单的代码不写啦,
只列出处理插入数据的JSP页面代码:
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.io.*" %>
<html>
<head>
<title>数据库添加数据</title>
</head>
<body>
<%
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="root";//数据库用户名
String userPasswd="root";//密码
String dbName="book";//数据库名
String tableName="bookInfo";//表名
String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;//连接字符串
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
String name=request.getParameter("name");
String publisher=request.getParameter("publihser");
name=new String(name.getBytes("iso-8859-1"));
publisher=new String(publisher.getBytes("iso-8859-1"));
String price=request.getParameter("price");
price=new String(price.getBytes("iso-8859-1"));
if(name.equals("") || publisher.equals("")||price.equals(""))
{response.sendRedirect("MySQLInsert.jsp");}
else{
String sql="insert into bookInfo (bookname,publisher,price) values('"+name+"','"+publisher+"','"+price+"')";
stmt.executeUpdate(sql);
out.print("数据添加成功");
}
stmt.close();
conn.close();//关闭连接、释放资源
%>
</body>
</html>
运行后,提交数据,服务器500错误:
org.apache.jasper.JasperException: An exception occurred processing JSP page /MysqlInsert1.jsp at line 24
21: String name=request.getParameter("name");
22: String publisher=request.getParameter("publihser");
23: name=new String(name.getBytes("iso-8859-1"));
24: publisher=new String(publisher.getBytes("iso-8859-1"));
25: String price=request.getParameter("price");
26: price=new String(price.getBytes("iso-8859-1"));
27: if(name.equals("") || publisher.equals("")||price.equals(""))
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
org.apache.jsp.MysqlInsert1_jsp._jspService(MysqlInsert1_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
请各位高手帮助!!
能把问题解决了再送分 展开
只列出处理插入数据的JSP页面代码:
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.io.*" %>
<html>
<head>
<title>数据库添加数据</title>
</head>
<body>
<%
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="root";//数据库用户名
String userPasswd="root";//密码
String dbName="book";//数据库名
String tableName="bookInfo";//表名
String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;//连接字符串
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
String name=request.getParameter("name");
String publisher=request.getParameter("publihser");
name=new String(name.getBytes("iso-8859-1"));
publisher=new String(publisher.getBytes("iso-8859-1"));
String price=request.getParameter("price");
price=new String(price.getBytes("iso-8859-1"));
if(name.equals("") || publisher.equals("")||price.equals(""))
{response.sendRedirect("MySQLInsert.jsp");}
else{
String sql="insert into bookInfo (bookname,publisher,price) values('"+name+"','"+publisher+"','"+price+"')";
stmt.executeUpdate(sql);
out.print("数据添加成功");
}
stmt.close();
conn.close();//关闭连接、释放资源
%>
</body>
</html>
运行后,提交数据,服务器500错误:
org.apache.jasper.JasperException: An exception occurred processing JSP page /MysqlInsert1.jsp at line 24
21: String name=request.getParameter("name");
22: String publisher=request.getParameter("publihser");
23: name=new String(name.getBytes("iso-8859-1"));
24: publisher=new String(publisher.getBytes("iso-8859-1"));
25: String price=request.getParameter("price");
26: price=new String(price.getBytes("iso-8859-1"));
27: if(name.equals("") || publisher.equals("")||price.equals(""))
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
org.apache.jsp.MysqlInsert1_jsp._jspService(MysqlInsert1_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
请各位高手帮助!!
能把问题解决了再送分 展开
3个回答
展开全部
原因publisher没有值传过来,是null,执行publisher.getBytes()出错:java.lang.NullPointerException
publisher=new String(publisher.getBytes("iso-8859-1"));
这种写法不安全
万一publisher没有值传过来,再执行publisher.getBytes()不是要出错了吗?同理这里:
name.equals("") || publisher.equals("")||price.equals("")
写法也不行,万一name,publisher,price没有值,就会报错,哦我明白了,你要明白name如果没有值传过来,request.getParameter("name")得到的不是"",而是null
这样写:
publisher=null==publisher?"":publisher;
此处应该这样写(结合上面的,不用检测null的情况了,而且这样写安全,即使name等为null也不会报错的哦):
"".equals(name) || "".equals(publisher) || "".equals(price)
还有就是转换编码形式,可以都等到组装成sql以后直接对sql语句进行转换,
这样前面的就省了,
sql=new String(sql.getBytes(""iso-8859-1""));
============================================================
关键部分我给你写一下:
<%
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="root";//数据库用户名
String userPasswd="root";//密码
String dbName="book";//数据库名
String tableName="bookInfo";//表名
String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;//连接字符串
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
String name=request.getParameter("name");
String publisher=request.getParameter("publihser");
String price=request.getParameter("price");
if(null==name || "".equals(name.trim()) || null==publisher || "".equals(publisher.trim()) || null==price || "".equals(price.trim())){
out.print("数据填写不完整");
out.print("<script>setTimeout('window.history.go(-1)',2000);</script>");
}else{
String sql="insert into bookInfo (bookname,publisher,price) values('"+name+"','"+publisher+"','"+price+"')";
sql=new String(sql.getBytes(""iso-8859-1""));
stmt.executeUpdate(sql);
out.print("数据添加成功");
}
stmt.close();
conn.close();//关闭连接、释放资源
%>
publisher=new String(publisher.getBytes("iso-8859-1"));
这种写法不安全
万一publisher没有值传过来,再执行publisher.getBytes()不是要出错了吗?同理这里:
name.equals("") || publisher.equals("")||price.equals("")
写法也不行,万一name,publisher,price没有值,就会报错,哦我明白了,你要明白name如果没有值传过来,request.getParameter("name")得到的不是"",而是null
这样写:
publisher=null==publisher?"":publisher;
此处应该这样写(结合上面的,不用检测null的情况了,而且这样写安全,即使name等为null也不会报错的哦):
"".equals(name) || "".equals(publisher) || "".equals(price)
还有就是转换编码形式,可以都等到组装成sql以后直接对sql语句进行转换,
这样前面的就省了,
sql=new String(sql.getBytes(""iso-8859-1""));
============================================================
关键部分我给你写一下:
<%
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="root";//数据库用户名
String userPasswd="root";//密码
String dbName="book";//数据库名
String tableName="bookInfo";//表名
String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;//连接字符串
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
String name=request.getParameter("name");
String publisher=request.getParameter("publihser");
String price=request.getParameter("price");
if(null==name || "".equals(name.trim()) || null==publisher || "".equals(publisher.trim()) || null==price || "".equals(price.trim())){
out.print("数据填写不完整");
out.print("<script>setTimeout('window.history.go(-1)',2000);</script>");
}else{
String sql="insert into bookInfo (bookname,publisher,price) values('"+name+"','"+publisher+"','"+price+"')";
sql=new String(sql.getBytes(""iso-8859-1""));
stmt.executeUpdate(sql);
out.print("数据添加成功");
}
stmt.close();
conn.close();//关闭连接、释放资源
%>
展开全部
程序本身就存在缺陷
应该:String name = "";
.....
name = request.getParameter("name");
......
if("".equals(name)||...)
{
response.sendRedirect("MySQLInsert.jsp");
}
else
{
String sql="insert into bookInfo bookname,publisher,price)values('"+name+"','"+publisher+"','"+price+"')";
sql=new String(sql.getBytes(""iso-8859-1""));
int count = stmt.executeUpdate(sql);
}
应该:String name = "";
.....
name = request.getParameter("name");
......
if("".equals(name)||...)
{
response.sendRedirect("MySQLInsert.jsp");
}
else
{
String sql="insert into bookInfo bookname,publisher,price)values('"+name+"','"+publisher+"','"+price+"')";
sql=new String(sql.getBytes(""iso-8859-1""));
int count = stmt.executeUpdate(sql);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询