ASP与Oracle 8i之间如何把附件写入数据库的BLOB字段中以及如何从中读取附件

stardrift 2003-09-30 09:04:30
有那位高手能给点建议或者给点代码吗?在下将不甚感激。
如果分不够我可以再加
等待着你的回答
...全文
29 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qxm 2003-10-22
  • 打赏
  • 举报
回复
GZ
tigerwen01 2003-10-13
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/read_article.asp?id=18955
stardrift 2003-10-13
  • 打赏
  • 举报
回复
问题是我在ASP中还是不知道如何来操作啊

有谁知道在ASP中如何处理吗?
caodavid2003 2003-09-30
  • 打赏
  • 举报
回复
我可以给提供java程序的写法供你参考:
/**
* 查询所有附件
*/
public void doGetAllAccessory()throws java.sql.SQLException {
System.out.println("In AccessoryBean.doGetAllAccessory()");

System.out.println("bugID:" + bugID);
bugID=bugID.trim();

String sql = "select * from Accessory where BugID='" + bugID +"' order by ACCESSORYNO";
String sql2 = "select BugForm.bugID,BugForm.tester,Project.Name,ProjectModule.name,TestingPoint.TestingPointName,FunctionBlock.FunctionBlockName from BugForm,ProjectModule,Project,TestingPoint,FunctionBlock where BugForm.project=project.id and BugForm.moduleID=ProjectModule.id and BugForm.TestingPointID=TestingPoint.TestingPointID and TestingPoint.FunctionBlockID=FunctionBlock.FunctionBlockID and BugForm.bugID='"+bugID+"'";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try{
conn = getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();


accessoryList = new java.util.ArrayList();
while(rs.next()){
AccessoryBean tep = new AccessoryBean();
tep.bugID = rs.getString("bugID").trim() ;
tep.accessoryNo = new Integer(rs.getInt("accessoryNo"));
tep.accessoryName = rs.getString("accessoryName");
//tep.setKey(rs.getString("bugID").trim()+";"+new Integer(rs.getInt("OperatorNo")));
accessoryList.add(tep);
}
rs.close();
rs=null;
pstmt.close();
pstmt = conn.prepareStatement(sql2);
rs = pstmt.executeQuery();
while(rs.next()){
this.setBugID(rs.getString(1));
this.setTesterName(rs.getString(2));
//this.setTestPointName(rs.getString(3));
this.setProjectName(rs.getString(3));
this.setModuleName(rs.getString(4));
this.setTestPointName(rs.getString(5));
this.setFuncName(rs.getString(6));
}


}catch(java.sql.SQLException sqle){
throw sqle;
}finally{
if(rs != null ) rs.close();
if(pstmt != null ) pstmt.close();
if(conn != null) conn.close();
}
}

/**
* 保存附件到数据库
*/
public void doSaveAccessory()throws java.sql.SQLException, IOException{
System.out.println("In AccessoryBean.doSaveAccessory()");


//插入新记录
String sql = "insert into Accessory(accessoryno, bugid, accessoryName, mimeName,content) values(?, ?, ?, ?,EMPTY_BLOB())";
Connection conn = null;
OraclePreparedStatement pstmt = null;
OracleResultSet ors = null;


try {
System.out.println("now to insert data");
mimeName = file.getContentType();//取得文件的mime类型

conn = getConnection();
pstmt = (OraclePreparedStatement)conn.prepareStatement(sql);
//插入数据到数据库
pstmt.setInt (1, accessoryNo.intValue());
pstmt.setString(2, bugID);
pstmt.setString(3, accessoryName);
pstmt.setString(4, mimeName);
pstmt.executeUpdate();
conn.commit();
pstmt.clearParameters();
System.out.println("insert data successful");
//插入BLOB字段数据
conn.setAutoCommit(false);
sql = "select content from Accessory where bugID='" + bugID + "' and accessoryNo=" + accessoryNo +" for update";

System.out.println("sql:" + sql);

pstmt = (OraclePreparedStatement)conn.prepareStatement(sql);
ors = (OracleResultSet)pstmt.executeQuery();

if(!ors.next()){
throw new java.sql.SQLException("没有记录!");
}

System.out.println("get blob...");
oracle.sql.BLOB blob=ors.getBLOB(1);//得到BLOB字段

//读取客户端上传的文件内容
//取得文件字节
InputStream stream = file.getInputStream();
//文件大小限制在4MB以下
int filesize = file.getFileSize();
if (filesize < 0x3e8000) {
System.out.println("filesize:"+filesize);
byte[]buffer = new byte[filesize];
int readed = 0;
if( (readed = stream.read(buffer, 0, filesize)) != -1){
System.out.println("readed:"+readed);
//将文件数据值入数据库
blob.putBytes(1, buffer);
}

conn.commit();

}else{
throw new IOException("文件不能大于4M!");
}
System.out.println("save file successful!!!");
}catch(FileNotFoundException fnfe) {
fnfe.printStackTrace();
}catch(SQLException sqle){
throw sqle;
}catch(Exception e){
e.printStackTrace();
}finally{
if(ors != null) ors.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}
}
/**
* 删除某个附件
*/
public void doDeleteAccessory()throws java.sql.SQLException {
System.out.println("In AccessoryBean.doDeleteAccessory()");

String sql = "delete from Accessory where BugID='" + bugID +"' and accessoryNo=" + accessoryNo;
Connection conn = null;
PreparedStatement pstmt = null;

try{
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.execute();

}catch(java.sql.SQLException sqle){
throw sqle;
}finally{
if(pstmt != null ) pstmt.close();
if(conn != null ) conn.close();
}
}


28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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