<%@ 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
字符串 String 類型 用‘’括起來 eg: "BookName=‘"+BookName+'+“and Author=’”+Author+'