1、当图片文件超过1M的时候,出错如下:
JDBC错误:Packet for query is too large (1298910 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
这是因为mysql默认一个包最大1M,需要修改my.ini
max_allowed_packet = 8M
2、图片文件的写入时候遇到 " You have an error in your SQL syntax "
注意,网上常见的采用 pstmt.setBinaryStream 的方法并不管用。。会提示出现SQL语法错误。我分析是数据库的字符集类型造成的,所以我们直接存byte就OK了。
PreparedStatement pstmt = conn.prepareStatement("insert into table values(?,?)");
pstmt.setInt(1,0);
FileInputStream fin = new FileInputStream("图片文件名");
byte buf[] = new byte[fin.avaliable()];
fin.read(buf);
pstmt.setByte(2,buf);
pstmt.executeUpdate()