wxywp
2016-11-17 15:37
采纳率: 100%
浏览 1.2k

jsp 链接MySQL数据库使用excuteUpdate()方法出错

<%@ page language="java" import="java.sql.*,java.io.*" pageEncoding="UTF-8"%>
<%
String driverNmae="com.mysql.jdbc.Driver";
String username="root";
String password="123456";
String dbName="book";
String tableNmae="bookinfo";
String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+username+"&password="+password;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();//执行 SQL语句并返回它所生成结果的对象
//定义变量接收数据
String bookname=request.getParameter("name");
String publisher=request.getParameter("publisher");
String price=request.getParameter("price");
bookname=new String(bookname.getBytes("iso-8859-1"));//对输入的数据进行编码
publisher=new String(publisher.getBytes("iso-8859-1"));
price=new String(price.getBytes("iso-8859-1"));
if(bookname.equals("")|publisher.equals("")|price.equals("")){
response.sendRedirect("index.jsp");
}else{
String sql="insert into bookinfo(bookname,publisher,price) values("+bookname+","+publisher+","+price+")";
stmt.executeUpdate(sql);//**就是这句老是报错**
out.print("数据添加成功");
}
stmt.close();
conn.close();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



连接数据库测试







<!--

-->




错误如下:
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: An exception occurred processing JSP page /MyJsp.jsp at line 23

20: response.sendRedirect("index.jsp");
21: }else{
22: String sql="insert into bookinfo(bookname,publisher,price) values("+bookname+","+publisher+","+price+")";
23: stmt.executeUpdate(sql);****
24: out.print("鏁版嵁娣诲姞鎴愬姛");
25: }
26: stmt.close();

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
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

javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,清�大学出版�,12.00)' at line 1
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.MyJsp_jsp._jspService(MyJsp_jsp.java:109)
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)

root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,清�大学出版�,12.00)' at line 1
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.Util.getInstance(Util.java:381)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485)
org.apache.jsp.MyJsp_jsp._jspService(MyJsp_jsp.java:78)
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)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.


Apache Tomcat/6.0.13

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • qq_36522073 2016-11-18 01:39
    已采纳

    字符串 String 类型 用‘’括起来 eg: "BookName=‘"+BookName+'+“and Author=’”+Author+'

    点赞 打赏 评论
  • wxywp 2016-11-17 15:43

    急啊!!!大神们帮帮忙,小弟先在这里谢谢了

    点赞 打赏 评论
  • blownewbee 2016-11-17 16:08

    sql里面bookname等是字符串类型,需要外面加上引号

    点赞 打赏 评论

相关推荐 更多相似问题