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.   

    你把content,image,detail都定义成String,而在sql中,直接用之作为表的FIELD名,当然不行了String sql="insert into picturenews(content,image,detail) values(?,?,?)";
      

  2.   

    是blob还是blog?
    如果是blob:插入大字段时一般是先插入一个空的blob,然后在把这条记录从数据库中读取出来,把blob放入一个流里边,然后把文件也写入到流里边,最后把流关闭,把文件读入数据库
      

  3.   

    create table 学生表(
    id char (12) primary key identity,
    content varchar(50) not null,
    detail varchar(500) not null,
    image blob,
    )
      

  4.   

    pstmt.setBinaryStream(2,str,str.available());
     变成pstmt.setBlob(2,str);
      

  5.   

    pstmt.setBinaryStream(2,str,str.available());
     变成pstmt.setBlob(2,str);