如何把文本文件存入SqlServer的image 字段---java

adisheng 2009-09-14 10:47:49
我已经把从Oracle中取出的Clob数据转换成String类型,然后保存为E:\\outBulletinData.txt文件中了。现在想把该文件插入到SqlServer的image 字段。如果不需这么麻烦,有没有更捷径的办法。请高手指教,等待中,谢谢....
...全文
228 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lookhang 2009-09-14
  • 打赏
  • 举报
回复
public class X_FILE implements Serializable{
private int fileId;
private int typeId;
private int version;
private InputStream menuFile;
private String fileName;
private String createTime;
private String remark;

private String size;
public X_FILE(int fileId, int typeId, int version, InputStream menuFile,
String fileName, String remark) {
super();
this.fileId = fileId;
this.typeId = typeId;
this.version = version;
this.menuFile = menuFile;
this.fileName = fileName;
this.remark = remark;
}
public X_FILE(int fileId, int typeId, int version, InputStream menuFile,
String fileName, String createTime, String remark) {
super();
this.fileId = fileId;
this.typeId = typeId;
this.version = version;
this.menuFile = menuFile;
this.fileName = fileName;
this.createTime = createTime;
this.remark = remark;
}
public X_FILE(int typeId, int version, InputStream menuFile, String fileName,
String remark) {
super();
this.typeId = typeId;
this.version = version;
this.menuFile = menuFile;
this.fileName = fileName;
this.remark = remark;
}
public int getFileId() {
return fileId;
}
public void setFileId(int fileId) {
this.fileId = fileId;
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public InputStream getMenuFile() {
return menuFile;
}
public void setMenuFile(InputStream menuFile) {
this.menuFile = menuFile;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}

}


顺便把X_FILE也贴了吧~~
lookhang 2009-09-14
  • 打赏
  • 举报
回复
public void  insetX_FILE(X_FILE x_file){
sql = "insert into X_FILE (TypeID,Version,MenuFile,FileName,Remark) values(?,?,?,?,?)";
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,x_file.getTypeId());
pstmt.setInt(2,x_file.getVersion());
try {
pstmt.setBinaryStream(3,x_file.getMenuFile(),x_file.getMenuFile().available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt.setString(4,x_file.getFileName());
pstmt.setString(5,x_file.getRemark());
pstmt.execute();
log.debug("执行"+sql+"完成!");
} catch (SQLException e) {
// TODO Auto-generated catch block
log.debug("执行" + sql + "出现异常!");
e.printStackTrace();
} finally {
freeResouce();
}
}


pstmt.setBinaryStream(3,x_file.getMenuFile(),x_file.getMenuFile().available());
这句是关键~
diggywang 2009-09-14
  • 打赏
  • 举报
回复
写入字节流啊
adisheng 2009-09-14
  • 打赏
  • 举报
回复
谢谢各位大哥,问题解决了。一下是我的部分代码:

File file = new File("E:\\outBulletinData.txt");
InputStream is = new FileInputStream(file);
byte[] textByte;
textByte = new byte[is.available()];
is.read(textByte);
is.close();
pstmt.setBinaryStream(7, (new ByteArrayInputStream(textByte)),textByte.length);

再次谢谢各位....

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧