java存图片mysql异常
mysql 的建的表
create table abc (a int,b longblob,c char(2));
jsp页面代码
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%@ page import="Model.DataAccess.DBBean"%>
<%
Connection con=DBBean.getConnection();
//插入数据库
String sql="insert into abc (a,c,b) values (?,?,?)";
//转换成file格式
File filename=new File("F:\\Tomcat\\webapps\\EP\\12.jpg");
//将文件的长度读出,并转换成Long型
long l1=filename.length();
int l2=(int)l1;
//以流的格式赋值
FileInputStream fis=new FileInputStream(filename);
PreparedStatement pstm =con.prepareStatement(sql);
pstm.setInt(1,1);
pstm.setString(2,"d");
// pstm.setBinaryStream(3,fis,l2); 这种方法试了不行,换下面的也不行
pstm.setBinaryStream(3,fis,fis.available());
pstm.executeUpdate();
pstm.close();
fis.close();
DBBean.close(con);
out.println("ok!!!");
%>
//出现的异常
java.sql.SQLException: 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 '轔��鳆�囚\"篿躥X0ra寫枝襱.m铑阢蓺iK~駟�?M猲鏁�,/屈9OsìQ<盰枢矊�$夂' at line 1
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1534)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1625)
com.mysql.jdbc.Connection.execSQL(Connection.java:2297)
com.mysql.jdbc.Connection.execSQL(Connection.java:2226)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1812)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1906)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1831)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1693)
org.apache.jsp._123_jsp._jspService(_123_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:123)
create table abc (a int,b longblob,c char(2));
jsp页面代码
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%@ page import="Model.DataAccess.DBBean"%>
<%
Connection con=DBBean.getConnection();
//插入数据库
String sql="insert into abc (a,c,b) values (?,?,?)";
//转换成file格式
File filename=new File("F:\\Tomcat\\webapps\\EP\\12.jpg");
//将文件的长度读出,并转换成Long型
long l1=filename.length();
int l2=(int)l1;
//以流的格式赋值
FileInputStream fis=new FileInputStream(filename);
PreparedStatement pstm =con.prepareStatement(sql);
pstm.setInt(1,1);
pstm.setString(2,"d");
// pstm.setBinaryStream(3,fis,l2); 这种方法试了不行,换下面的也不行
pstm.setBinaryStream(3,fis,fis.available());
pstm.executeUpdate();
pstm.close();
fis.close();
DBBean.close(con);
out.println("ok!!!");
%>
//出现的异常
java.sql.SQLException: 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 '轔��鳆�囚\"篿躥X0ra寫枝襱.m铑阢蓺iK~駟�?M猲鏁�,/屈9OsìQ<盰枢矊�$夂' at line 1
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1534)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1625)
com.mysql.jdbc.Connection.execSQL(Connection.java:2297)
com.mysql.jdbc.Connection.execSQL(Connection.java:2226)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1812)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1906)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1831)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1693)
org.apache.jsp._123_jsp._jspService(_123_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:123)
...全文
相关推荐
MySQL存储图片方法[项目代码]
在MySQL中存储图片通常有两种方法:一是存储图片路径,将图片文件保存在服务器或云存储中,数据库中仅保存路径;二是以二进制格式(BLOB)存储图片数据。前者适用于需要频繁访问图片的场景,后者则直接将图片数据存入数据库。文章还提供了一个创建表、插入和查询图片数据的示例,并提醒在实际应用中应权衡性能和可扩展性,建议将大型图片存储在外部服务器或云存储中,数据库中仅保存路径或元数据。
java+mysql 保存图片
通过Java对图片数据进行保存到Mysql数据中,再从数据库中读取写到本地
Java实现用Mysql存取图片操作实例
主要介绍了Java实现用Mysql存取图片操作实例,本文讲解了使用BLOB类型保存和读取图片的代码实例,需要的朋友可以参考下
java如何将图片类型的数据存入mysql 数据库
源文件不含库 博文链接:https://liyaxi.iteye.com/blog/474188
在java web中显示存入mysql数据库的图片
在java web中显示存入mysql数据库的图片


Eclipse
