分享更有价值
被信任是一种快乐

mysql、sqlserver和oracle三种数据库的大对象存取方式

文章页正文上

本篇内容主要讲解“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:/aa免费主机域名a.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三种数据库的大对象存取方式”有了更深的了解,不妨来实际操作一番吧!这里是云技术网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

相关推荐: mysql中is 、is not和的区别用法

本篇内容主要讲解“mysql中is 、is not和的区别用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql中is 、is not和的区别用法”吧!#案例:查询没有奖金的员工名和奖金率#=免费主机域名或号…

文章页内容下
赞(0) 打赏
版权声明:本站采用知识共享、学习交流,不允许用于商业用途;文章由发布者自行承担一切责任,与本站无关。
文章页正文下
文章页评论上

云服务器、web空间可免费试用

宝塔面板主机、支持php,mysql等,SSL部署;安全高速企业专供99.999%稳定,另有高防主机、不限制内容等类型,具体可咨询QQ:360163164,Tel同微信:18905205712

主机选购导航云服务器试用

登录

找回密码

注册