先看看下面的代码:package test;import java.sql.*;
import java.util.*;public class Test
{ public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager
.getConnection(
"jdbc:mysql://localhost/mydb?characterEncoding=GBK",
"root", "password"); java.io.File file = new java.io.File("d:\\my.jpg");
int fileLen = (int) file.length();
java.io.InputStream is = new java.io.FileInputStream(file);
PreparedStatement pstmt = conn
.prepareStatement("INSERT INTO mydb.table1(name, image) VALUES(?, ?)"); pstmt.setString(1, "pic1"); pstmt.setBinaryStream(2, is, fileLen);
pstmt.executeUpdate(); pstmt.close();
conn.close();
}}上面的代码没什么问题。但运行后,竟然出现了如下的错误Exception in thread "main" com.mysql.jdbc.exceptions.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 '?:??%??êrè‘??|N????ó&?V??c!Q???$“??U 7?¤f???n??ò[?\\JH?\\\\?64??uU?`?8?' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1585)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1500)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1485)
at test.Test.main(Test.java:29)但是另建立一个aa.txt,就可以插入到数据库的image字段中。 我的表的定义如下:
CREATE TABLE mydb.table1 (
id int unsigned NOT NULL auto_increment,
name varchar(20) NOT NULL,
image longblob NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=GBK;
并且我已经在mysql的my.ini中加入了
max_allowed_packet=32M
而且my.jpg肯定小于32M不知这个错误是什么意思。 难度blob类型的字段还验证字符???哪位有空帮小弟解决一下,必加分!
import java.util.*;public class Test
{ public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager
.getConnection(
"jdbc:mysql://localhost/mydb?characterEncoding=GBK",
"root", "password"); java.io.File file = new java.io.File("d:\\my.jpg");
int fileLen = (int) file.length();
java.io.InputStream is = new java.io.FileInputStream(file);
PreparedStatement pstmt = conn
.prepareStatement("INSERT INTO mydb.table1(name, image) VALUES(?, ?)"); pstmt.setString(1, "pic1"); pstmt.setBinaryStream(2, is, fileLen);
pstmt.executeUpdate(); pstmt.close();
conn.close();
}}上面的代码没什么问题。但运行后,竟然出现了如下的错误Exception in thread "main" com.mysql.jdbc.exceptions.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 '?:??%??êrè‘??|N????ó&?V??c!Q???$“??U 7?¤f???n??ò[?\\JH?\\\\?64??uU?`?8?' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1585)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1500)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1485)
at test.Test.main(Test.java:29)但是另建立一个aa.txt,就可以插入到数据库的image字段中。 我的表的定义如下:
CREATE TABLE mydb.table1 (
id int unsigned NOT NULL auto_increment,
name varchar(20) NOT NULL,
image longblob NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=GBK;
并且我已经在mysql的my.ini中加入了
max_allowed_packet=32M
而且my.jpg肯定小于32M不知这个错误是什么意思。 难度blob类型的字段还验证字符???哪位有空帮小弟解决一下,必加分!
解决方案 »
- 我这个场景用泛型还是继承合适呢
- jdk1.4 ServerSocket用bind(SocketAddress endpoint)方法初始化,是否同时支持IPv4和IPv6的TCP连接?
- 我是初学者 请问getInstance在抽象类中是咋用的?
- java 加载过多png图片显示时报Outofmemory error
- 若客户端不给设cookie,以下的代码会发生exception吗?
- JBuilder问题DateSet has no unique row identifiers
- 靜態數據的初始化,(編程思想上的問題,不明白.)
- 为什么我读取txt文件是乱码,文件是按照unicode存的,代码如下:
- org.apache.http.client.HttpClient的问题
- 大家说说自己是怎么理解“上溯造型”的
- java抽象方法的问题?
- 一个算法问题
好像是说你sql语句语法错误
你用一个不到32k的东西,还不如在数据库中定义成别的字段
blob的最多可存4g