mysql 存入 blob类型数据

展开阅读全文

MySQL插入blob类型出错

03-11

OS:windows7rnMySQL:5.0rnrncode:rnrnpackage com.hgd.richard.jdbc;rnrnimport java.io.BufferedInputStream;rnimport java.io.File;rnimport java.io.FileInputStream;rnimport java.io.InputStream;rnimport java.sql.Connection;rnimport java.sql.DriverManager;rnimport java.sql.PreparedStatement;rnrnpublic class MyBlobTest rnrn public static void main(String[] args) throws Exception rn insert();rn rn rn static void insert() throws Exceptionrn Class.forName("com.mysql.jdbc.Driver");rn rn Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "mysql");rn //blob_test为表名,id为自动增长型,big_bit为blob型rn PreparedStatement ps = conn.prepareStatement("insert into blob_test(big_bit) values(?)");rn rn File file = new File("Type3.jpg");//Type3.jpg放在工程根目录下rn InputStream in = new BufferedInputStream(new FileInputStream(file));rn System.out.println(file.length());rn ps.setBinaryStream(1, in, (int)file.length());rn rn System.out.println(ps.executeUpdate());rn rn ps.close();rn rn conn.close();rn rnrnrnrn数据库默认编码为:gbkrnrnException 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 '?;?w?v=??OY?\0?zoü?\'???í?7?~“ò?á@ZV????\0?Iù???oM?\0?¤ü??P…#????zoü?\'?' at line 1rn at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)rn at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)rn at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)rn at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)rn at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)rn at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)rn at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)rn at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)rn at com.hgd.richard.jdbc.MyBlobTest.insert(MyBlobTest.java:34)rn at com.hgd.richard.jdbc.MyBlobTest.main(MyBlobTest.java:19)rnrn请高手指点rn 论坛

没有更多推荐了,返回首页