超级郁闷的问题,关于jdbc向MySQL的Blog字段插入数据的问题
先看看下面的代码:
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类型的字段还验证字符???
哪位有空帮小弟解决一下,必加分!