java sqlserver text 類型字段讀取方法
更新時(shí)間:2012年11月30日 09:26:05 作者:
有這樣一個(gè)需求,需要將原本存儲(chǔ)在數(shù)據(jù)庫中的文檔轉(zhuǎn)存至文件系統(tǒng)中,于是寫了一個(gè)簡單的程序完成此功能
有這樣一個(gè)需求,需要將原本存儲(chǔ)在數(shù)據(jù)庫中的文檔轉(zhuǎn)存至文件系統(tǒng)中,于是寫了一個(gè)簡單的程序完成此功能,代碼如下:
Java代碼
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.dbunit.util.Base64;
public class ReadBlob {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=test1", "sa",
"123456");
PreparedStatement ps = conn.prepareStatement("select * from aa");
ResultSet rs = ps.executeQuery();
while(rs.next()){
String fileName = rs.getString("FileName");
String content = rs.getString("Content");
byte[] byte_content = Base64.decode(content);
generateFile(byte_content, "D:\\doc", fileName);
}
conn.close();
}
/**
* 根據(jù)byte數(shù)組,生成文件
*/
public static void generateFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
Java代碼
復(fù)制代碼 代碼如下:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.dbunit.util.Base64;
public class ReadBlob {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=test1", "sa",
"123456");
PreparedStatement ps = conn.prepareStatement("select * from aa");
ResultSet rs = ps.executeQuery();
while(rs.next()){
String fileName = rs.getString("FileName");
String content = rs.getString("Content");
byte[] byte_content = Base64.decode(content);
generateFile(byte_content, "D:\\doc", fileName);
}
conn.close();
}
/**
* 根據(jù)byte數(shù)組,生成文件
*/
public static void generateFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
您可能感興趣的文章:
- java中jdbcTemplate的queryForList(坑)
- Java Spring5學(xué)習(xí)之JdbcTemplate詳解
- Java 使用JdbcTemplate 中的queryForList發(fā)生錯(cuò)誤解決辦法
- Java連接sqlserver2008數(shù)據(jù)庫代碼
- JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實(shí)例代碼
- 使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅(qū)動(dòng)方法
- 用Java連接sqlserver數(shù)據(jù)庫時(shí)候幾個(gè)jar包的區(qū)別分析
- Java中String的JdbcTemplate連接SQLServer數(shù)據(jù)庫的方法
相關(guān)文章
IDEA提示:Boolean method ‘xxx‘ is always&nb
這篇文章主要介紹了IDEA提示:Boolean method ‘xxx‘ is always inverted問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
RestTemplat中關(guān)于getForobject方法的使用
這篇文章主要介紹了RestTemplat中關(guān)于getForobject方法的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
java.util.concurrent.ExecutionException 問題解決方法
這篇文章主要介紹了java.util.concurrent.ExecutionException 問題解決方法的相關(guān)資料,需要的朋友可以參考下2016-11-11
Springboot居然可以設(shè)置動(dòng)態(tài)的Banner(推薦)
這篇文章主要介紹了Springboot居然可以設(shè)置動(dòng)態(tài)的Banner,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
聊聊Kotlin?中?lateinit?和?lazy?的原理區(qū)別
使用 Kotlin 進(jìn)行開發(fā),對(duì)于 latelinit 和 lazy 肯定不陌生。但其原理上的區(qū)別,可能鮮少了解過,借著本篇文章普及下這方面的知識(shí),感興趣的朋友一起看看吧2022-07-07
macOS下Spring Boot開發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了macOS下Spring Boot開發(fā)環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Java輸入輸出流復(fù)制文件所用時(shí)間對(duì)比
這篇文章主要介紹了Java輸入輸出流復(fù)制文件所用時(shí)間對(duì)比的相關(guān)資料,非常不錯(cuò),具有參考解決價(jià)值,需要的朋友可以參考下2016-08-08
MyBatis Properties及別名定義實(shí)例詳解
這篇文章主要介紹了MyBatis Properties及別名定義實(shí)例詳解,需要的朋友可以參考下2017-08-08

