jsp向mysql中某张表插入blog类型数据时,报错:
严重: Servlet.service() for servlet jsp threw exception
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 '\'I\'z\'?\'?(
(?(q(?(?))8)k)?)?**5*h*?*?++6+i+?+?,,9,n,?,×- -A-v-?-á..L.?.' at line 1
jsp代码:
<%@ page contentType="text/html;charset=gbk"%>  <%@ page import="java.sql.*" %>  <%@ page import="java.util.*"%>  <%@ page import="java.text.*"%>  <%@ page import="java.io.*"%>  <html>  <body>  
<%System.out.println("enter submit page!!!"); %>
<%  
request.setCharacterEncoding("gb2312");
//Class.forName("com.mysql.jdbc.Driver");  //加载驱动程序类  //Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vote","root","root");  //建立数据库联机,其中vote为数据库名,root为连接数据库的帐号及密码。  
Class.forName("com.mysql.jdbc.Driver").newInstance();
String URL="jdbc:mysql://localhost:3306/vote?Unicode=true&characterEncoding=gbk";
String uname="root";
String upass="root";
Connection con = DriverManager.getConnection(URL,uname,upass);Statement stmt=con.createStatement();  //建立Statement对象   
String content=request.getParameter("content");  String filename=request.getParameter("image");  String detail=request.getParameter("txtmail");  //获得所要显示图片的标题、存储路径、内容,并进行中文编码  FileInputStream str=new FileInputStream(filename);  String sql="insert into picturenews(content,image,detail) values(?,?,?)";  PreparedStatement pstmt=con.prepareStatement(sql);  pstmt.setString(1,content);  pstmt.setBinaryStream(2,str,str.available());  pstmt.setString(3,detail);  pstmt.execute();  //将数据存入数据库  out.println("Success,You Have Insert an Image Successfully");  %>  

解决方案 »

  1.   

    二进制数据一般很少直接用INSERT语句来实现。因为可以会包括特殊字符比如 ‘,\0 等
      

  2.   

    FileInputStream str=new FileInputStream(filename);   String sql="insert into picturenews(content,image,detail) values(?,?,?)";   PreparedStatement pstmt=con.prepareStatement(sql);   pstmt.setString(1,content);   pstmt.setBinaryStream(2,str,str.available());   pstmt.setString(3,detail);   pstmt.execute();   //将数据存入数据库   out.println("Success,You Have Insert an Image Successfully");   %>