mysql中使用blob存取png格式文件导致异常?

萌萌哒的小丁丁 发布于 2016/08/31 10:03
阅读 480
收藏 0
public static void insert() throws SQLException, IOException{
		Connection conn = null;
		ResultSet rs = null;
		conn = JDBCUtil.getConnection();
		String sql = "insert into test_blob(id,big_blob)values(2,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		File file = new File("1.png");
		InputStream in = new BufferedInputStream(new FileInputStream(file));
		ps.setBinaryStream(1, in, (int)file.length());
//		System.out.println(ps);
		int i = ps.executeUpdate();
		in.close();
		System.out.println(i);
		JDBCUtil.free(conn, ps, rs);
	}

我用这段代码测试了gif,jpg格式的文件可以存入,但是存png格式的就出现下面错误:(觉得不是编码问题)后来我也测试了longblob还是报这样的异常。

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 '@3\0 \0\0\0Pdg2Pic.exe!??€?è??\"?“??D??t#????”??1?””	\'r???%???*???¨"' 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:3283)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
	at cn.qzu.jdbc.day2.BlobTest.insert(BlobTest.java:31)
	at cn.qzu.jdbc.day2.BlobTest.main(BlobTest.java:19)

为了确定是否是编码的问题,我新建了数据库并设置了gbk编码与IDE保持一致,连接时也指定gbk,测试仍然报错:

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 'j6?‘§j?€?E???è??d??±’???é??/T???è???€–??QE$3g???è??é±?D?’?'5è??s???è????‰?_?' 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:3283)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
	at cn.qzu.jdbc.day2.BlobTest.insert(BlobTest.java:32)
	at cn.qzu.jdbc.day2.BlobTest.main(BlobTest.java:19)


why?




加载中
返回顶部
顶部