国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

java實現插入mysql二進制文件,blob類型,遇到問題及解決辦法

2024-07-24 12:44:00
字體:
來源:轉載
供稿:網友
首先是數據庫建立要準備的:
我們要把放置二進制字段設置為Blob類型,根據文件的大小選擇合適的Blob類型,一下是各個Blob類型所能容納二進制文件的大小
MySQL的四種BLOB類型
類型 大小(單位:字節)
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G
一下是具體操作代碼:
復制代碼 代碼如下:

/**
*
* 把二進制文件(該二進制文件可以是本地硬盤路徑,也可以是一個網絡路徑)存入數據庫
* create date:2009-5-13 author:Administrator
*
* @param file
* 可以是本地文件也可以是網絡文件
* @param conn
*/
public void saveBinary(String file, Connection conn) {
// 注意二進制文件寫入數據庫時所用到的類,以及類包裝轉換過程
File f = null;
if (file.toLowerCase().contains("http:"))
f = DownLoadWithUrl.downLoadFile(file);
else
f = new File(file);
if (f != null) {
try {
InputStream is = new FileInputStream(f);
PreparedStatement ps = conn
.prepareStatement("insert into bankVoice(name,text) values (?,?)");
ps.setString(1, file);
int i = is.available();
ps.setBinaryStream(2, is, is.available());
ps.executeUpdate();
System.out.println("二進制文件插入成功");
ps.clearParameters();
ps.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("二進制文件插入時出現異常");
}
}
}

注意在操作時候會出現以下異常,那么我們只需做一下設置:以我本地為例:進入D:/MySql5.0/mysql-5.0.51b-win32 目錄,有以下文件可以看到:my-large.ini、my-small.ini、my-medium.ini、my-huge.ini
我們把只需把mysql服務現在加載的ini文件中的配置項:max_allowed_packet 改為 16M
即是:max_allowed_packet = 16M 默認的是1M我們改為16M,然后重啟mysql服務器,這樣就不會出現下面的異常了。
復制代碼 代碼如下:

com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1048587 > 1047552). You can change this value on the server by setting the max_allowed_packet' variable.
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2632)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2618)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1551)
at com.mysql.jdbc.ServerPreparedStatement.storeStream(ServerPreparedStatement.java:2180)
at com.mysql.jdbc.ServerPreparedStatement.serverLongData(ServerPreparedStatement.java:1199)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1004)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
at SaveBinaryToDB.SaveBinaryToDB.saveBinary(SaveBinaryToDB.java:33)
at SaveBinaryToDB.SaveBinaryToDB.main(SaveBinaryToDB.java:17)
/**
* 從數據庫中讀取二進制文件 create date:2009-5-13 author:Administrator
*
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛浦县| 黄石市| 井研县| 松阳县| 淮阳县| 常德市| 资兴市| 社旗县| 铜鼓县| 区。| 巢湖市| 衡水市| 犍为县| 广安市| 张家界市| 翁牛特旗| 南和县| 建湖县| 通州市| 九江县| 德阳市| 如皋市| 天津市| 东城区| 旬阳县| 凤山县| 鹤岗市| 元朗区| 东安县| 都江堰市| 蒙阴县| 芷江| 增城市| 新野县| 石首市| 文登市| 康平县| 安国市| 西乌珠穆沁旗| 吴桥县| 安吉县|