BLOB 二进制数据 最大长度4G
ORACLE的话
ORACLE的话
解决方案 »
- 再问个nio高级问题,nio如何处理域名解析
- 才学java 有谁能远程帮我下 java空指针错误 关于ssh整合的东西
- IBM小型机AIX+weblogic8.1.4 ,老宕机,望大虾们给点帮助
- 把GB2312改成UTF-8网页乱码
- jsp页面的小问题
- JBOSS下通过JNDI无法连接SQLServer2000数据库
- spring使用dbcp类型的DataSource,数据库为mysql,老报异常!!!都疯了!!
- 刚开始学用iReport作报表,现遇到如下问题,请各位帮忙
- 各位,有根据类名可以查找到,所在的打成jar包的文件吗?
- jsp:useBean的class引入包出错
- 如何做一个二级域名 急
- jsf的dataTable中的下拉列表导致commandLink和commandButton失效
oracle 9i
jdk 1.6.xx
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.swing.JFileChooser;/*-- 测试表
create table TestImage(
img_id int primary key,
img_content blob
)*/
public class BlobTestForOracle {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:oratest";
private static final String DB_USER = "test";
private static final String DB_PASSWORD = "password";
public BlobTestForOracle(){
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 另村为...
*
* */
public String chooseFileSave(String title,String fileName){
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(fileName));
chooser.setDialogTitle(title);
int rnt = chooser.showSaveDialog(null);
if(rnt == JFileChooser.APPROVE_OPTION){
return chooser.getSelectedFile().getAbsolutePath();
}else{
return "";
}
} /**
* 打开...
*
* */
public String chooseFileOpen(String title,String fileName){
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(fileName));
chooser.setDialogTitle(title);
int rnt = chooser.showOpenDialog(null);
if(rnt == JFileChooser.APPROVE_OPTION){
return chooser.getSelectedFile().getAbsolutePath();
}else{
return "";
}
}
/**
* 写入blob
* */
public void writeFileToDatabaseByRid(String fileName,int rid){
BufferedInputStream bis = null;
Connection conn = null;
PreparedStatement pstmt = null;
String strSQL = null;
File file = null;
int size = 0;
try{
strSQL = "INSERT INTO TestImage(img_id,img_content) VALUES(?,?)";
conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
pstmt = conn.prepareStatement(strSQL);
fileName = this.chooseFileOpen("请选择一个文件",fileName);
file = new File(fileName);
if(! file.exists()){
System.out.println(file.getAbsolutePath());
System.out.println("文件貌似不存在!");
return;
}
else if(file.length() > 50 * 1024 * 1024){ // 限制一下文件大小
System.out.println("文件是不是太大鸟? " + file.length());
return;
}
size = (int)file.length();
bis = new BufferedInputStream(new FileInputStream(file));
pstmt.setInt(1, rid);
pstmt.setBinaryStream(2,bis,size); // 类似于逐一读取,bis流也被耗尽需要reset
int effect = pstmt.executeUpdate();
if(effect == 1){
System.out.println("写入ok!" + file.getAbsolutePath());
}
else{
System.out.println("写入failed!");
}
bis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(SQLException sqle){
sqle.printStackTrace();
}finally{
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
/**
* 从blob读取
* */
public void getFileFromDatabaseByRid(String fileName,int rid){
String strSQL = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
int size = 0;
File file = null;
try{
strSQL = "SELECT * FROM TestImage WHERE img_id = ?";
conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, rid);
rst = pstmt.executeQuery();
if(rst.next()){
fileName = this.chooseFileSave("存储为...", fileName);
file = new File(fileName);
if("".equals(file.getName().trim())){
System.out.println("failed!");
return;
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
BufferedInputStream bis = new BufferedInputStream(rst.getBinaryStream("img_content"));
byte[] b = new byte[10 * 1024]; // 这里是设置了缓存 byte[] b 设置为 10K
int len = 0; // 返回read一次实际读取的字节数
while((len = bis.read(b)) != -1){
os.write(b,0,len); // 将b数组的内容写(按照len个字节输出)到输出流中,注意这里不要混淆了b.length
size += len;
}
os.flush();
os.close();
bis.close();
System.out.println("保存文件完毕! " + size + " Byte(s)");
}
else{
System.out.println("id : " + rid + " 文件没有找到");
}
}catch(IOException ioe){
ioe.printStackTrace();
}
catch(SQLException sqle){
sqle.printStackTrace();
}finally{
if(rst != null){
try {
rst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
public static void main(String[] args){
// test
BlobTestForOracle test = new BlobTestForOracle();
test.writeFileToDatabaseByRid("C:/a.jpg",123);
test.getFileFromDatabaseByRid("C:/a.jpg", 123); }
}
http://my.so-net.net.tw/idealist/Java/JDBC/streamDataType.html