您好,登錄后才能下訂單哦!
本篇內容主要講解“mysql、sqlserver和oracle三種數據庫的大對象存取方式”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“mysql、sqlserver和oracle三種數據庫的大對象存取方式”吧!
大對象存取:
類型一般應該用mediumblod,
blob只能存2的16次方個byte,
mediumblod是24次方,
一般來說夠用了.longblob是32次方有些大.
MYSQL默認配置只能存1M大小的文件,要修改配置,WIN版本的在mysql.ini文件中
修改max_allowed_packet,net_buffer_length等幾個參數,或直接SET GLOBAL varName=value.
linux版本可以在啟動參數后加-max_allowed_packet=xxM等幾個參數.
MYSQL存大對象最好直接就setBinaryStream,又快又方便.
而不要先插入空再造型成BLOB然后再setBlob
例子:
import java.sql.*;
import java.io.*;
public class DBTest {
static String driver = "org.gjt.mm.mysql.Driver";
static String url = "jdbc:mysql://localhost:3306/test";
static String user = "root";
static String passwd = "passwd";
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,passwd);
int op = 1;
//插入
if (op == 0) {
PreparedStatement ps = conn.prepareStatement("insert into tb_file values (?,?)");
ps.setString(1, "aaa.exe");
InputStream in = new FileInputStream("d:/aaa.exe");
ps.setBinaryStream(2,in,in.available());
ps.executeUpdate();
ps.close();
}
else {
//取出
PreparedStatement ps = conn.prepareStatement("select * from tb_file where filename = ?");
ps.setString(1, "aaa.exe");
ResultSet rs = ps.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("filecontent");
System.out.println(in.available());
FileOutputStream out = new FileOutputStream("d:/bbb.exe");
byte[] b = new byte[1024];
int len = 0;
while ( (len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
rs.close();
ps.close();
}
}
catch (Exception ex) {
ex.printStackTrace(System.out);
}
finally {
try {conn.close();}
catch (Exception ex) { }
}
}
}
sqlserver 大對象存取沒有什么多說的,只要是image類型就行了,注意這是column類型,有人以為它只能存
圖象.image是文件鏡象的意思.
import java.sql.*;
import java.io.*;
public class DBTest {
static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
static String url = "jdbc:microsoft:sqlserver://192.168.0.202:9999999999;DatabaseName=dddd";
static String user = "sa";
static String passwd = "ps";
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,passwd);
int op = 0;
//插入
if (op == 0) {
PreparedStatement ps = conn.prepareStatement("insert into tb_file values (?,?)");
ps.setString(1, "aaa.exe");
InputStream in = new FileInputStream("d:/aaa.exe");
ps.setBinaryStream(2,in,in.available());
ps.executeUpdate();
ps.close();
}
else {
//取出
PreparedStatement ps = conn.prepareStatement("select * from tb_file where filename = ?");
ps.setString(1, "aaa.exe");
ResultSet rs = ps.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("filecontent");
System.out.println(in.available());
FileOutputStream out = new FileOutputStream("d:/bbb.exe");
byte[] b = new byte[1024];
int len = 0;
while ( (len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
rs.close();
ps.close();
}
}
catch (Exception ex) {
ex.printStackTrace(System.out);
}
finally {
try {conn.close();}
catch (Exception ex) { }
}
}
}
ORACLE的大對象存儲有些變態,要無論是Blob,還是CLOB都要求先插入一個空值,然后
查詢并鎖定這一條記錄,獲取對Lob的引用再進行填充,網上有太多的例子.我個人認為
這種方法垃圾得連寫都不想寫了,你可以自己去搜索一下.
這種特別的操作既增加操作的復雜度,又違反了JDBC接口的規范,所以我極力反對這樣
使用,如果你和我有同樣的觀點.那么我提供另一種通用的方法.就是你不用LOB而用
oracle的LONG RAW來代替它們.這樣就可以象其它對象一樣操作了:
create table tb_file(filename varchar2(255),filecontent LONG RAW);
import java.sql.*;
import java.io.*;
public class BlobTest {
static String driver = "oracle.jdbc.driver.Driver";
static String url = "jdbc:oracle:thin:@localhost:1521:test";
static String user = "system";
static String passwd = "passwd";
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
int op = 1;
//插入
if (op == 0) {
PreparedStatement ps = conn.prepareStatement("insert into tb_file values (?,?)");
ps.setString(1, "aaa.exe");
InputStream in = new FileInputStream("d:/aaa.exe");
ps.setBinaryStream(2,in,in.available());
ps.executeUpdate();
ps.close();
}
else {
//取出
PreparedStatement ps = conn.prepareStatement("select * from tb_file where filename = ?");
ps.setString(1, "aaa.exe");
ResultSet rs = ps.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("filecontent");
System.out.println(in.available());
FileOutputStream out = new FileOutputStream("d:/bbb.exe");
byte[] b = new byte[1024];
int len = 0;
while ( (len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
rs.close();
ps.close();
}
}
catch (Exception ex) {
ex.printStackTrace(System.out);
}
finally {
try {
conn.close();
}
catch (Exception ex) {}
}
}
}
到此,相信大家對“mysql、sqlserver和oracle三種數據庫的大對象存取方式”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。