java sqlserver text 類型字段讀取方法
更新時(shí)間:2012年11月30日 09:26:05 作者:
有這樣一個(gè)需求,需要將原本存儲(chǔ)在數(shù)據(jù)庫(kù)中的文檔轉(zhuǎn)存至文件系統(tǒng)中,于是寫(xiě)了一個(gè)簡(jiǎn)單的程序完成此功能
有這樣一個(gè)需求,需要將原本存儲(chǔ)在數(shù)據(jù)庫(kù)中的文檔轉(zhuǎn)存至文件系統(tǒng)中,于是寫(xiě)了一個(gè)簡(jiǎn)單的程序完成此功能,代碼如下:
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ù)庫(kù)代碼
- JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼
- 使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅(qū)動(dòng)方法
- 用Java連接sqlserver數(shù)據(jù)庫(kù)時(shí)候幾個(gè)jar包的區(qū)別分析
- Java中String的JdbcTemplate連接SQLServer數(shù)據(jù)庫(kù)的方法
相關(guān)文章
SpringBoot中配置雙數(shù)據(jù)源的實(shí)現(xiàn)示例
在許多應(yīng)用程序中,可能會(huì)遇到需要連接多個(gè)數(shù)據(jù)庫(kù)的情況,本文主要介紹了SpringBoot中配置雙數(shù)據(jù)源的實(shí)現(xiàn)示例,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08springcloud gateway設(shè)置context-path的操作
這篇文章主要介紹了springcloud gateway設(shè)置context-path的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07十大常見(jiàn)Java String問(wèn)題_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
本文介紹Java中關(guān)于String最常見(jiàn)的10個(gè)問(wèn)題,需要的朋友參考下吧2017-04-04springMVC實(shí)現(xiàn)圖形驗(yàn)證碼(kaptcha)代碼實(shí)例
這篇文章主要介紹了springMVC實(shí)現(xiàn)圖形驗(yàn)證碼(kaptcha)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值2019-09-09jenkins+Maven從SVN上構(gòu)建項(xiàng)目的方法
這篇文章主要介紹了jenkins+Maven從SVN上構(gòu)建項(xiàng)目,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09spring為java.util.Properties類型的屬性進(jìn)行賦值過(guò)程解析
這篇文章主要介紹了spring為java.util.Properties類型的屬性進(jìn)行賦值過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01