java sqlserver text 类型字段读取方法

2016-02-19 11:36 44 1 收藏

下面是个超简单的java sqlserver text 类型字段读取方法教程,图老师小编精心挑选推荐,大家行行好,多给几个赞吧,小编吐血跪求~

【 tulaoshi.com - 编程语言 】

有这样一个需求,需要将原本存储在数据库中的文档转存至文件系统中,于是写了一个简单的程序完成此功能,代码如下:
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();
}
/**
* 根据byte数组,生成文件
*/
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();
}
}
}
}
}

来源:https://www.tulaoshi.com/n/20160219/1597979.html

延伸阅读
1、LONG 数据类型中存储的是可变长字符串,最大长度限制是2GB。 2、对于超出一定长度的文本,基本只能用LONG类型来存储,数据字典中很多对象的定义就是用LONG来存储的。 3、LONG类型主要用于不需要作字符串搜索的长串数据,如果要进行字符搜索就要用varchar2类型。 4、很多工具,包括SQL*Plus,处理LONG 数据类型都是很困难的。 5、LONG 数...
由MySQL支持的列类型列在下面。下列代码字母用于描述中: M 指出最大的显示尺寸。最大的合法的显示尺寸是 255 。 D 适用于浮点类型并且指出跟随在十进制小数点后的数码的数量。最大可能的值是30,但是应该不大于M-2。 方括号(“[”和“]”)指出可选的类型修饰符的部分。 注意,如果你指定一个了为ZEROFILL,MySQL将为该列自动地增加UNSI...
〔config.properties〕 [CMS properties] cmsServerName=cms cmsTemplateDirectoryName=template [time out:minute] time_out=300000 [administrator setting] administrator=SA admingroup=ADMINROLE [web path setting] innerresource=/AccessControl/jsp/innerresource/ ...
   declare @i  int    set @i='a'    set @i=cast('a' as int)    set @i=convert(int, 'a')    print @i                           &n...
  作者信息: 曾青松 zengqingsong@sohu.com 中山大学数学与计算科学学院 信息系统与计算机网络方向硕士研究生  程序源代码:   }     public Connection getConnection(String userName, String passWord) throws       SQLException {   &...

经验教程

631

收藏

25
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部