如何上传文件到数据库的blob字段?(jsp)

jinglihua 2003-07-31 10:09:57
各位大虾,小弟有问题求助。是涉及到如何上传文件到数据库的blob字段的问题。在这里,有关很多这个问题的程序,但我不太看的懂,看的懂的却也不太能运用。这里很多都是涉及到java上传文件到blob字段,现在我想学习的是jsp上传文件到blob字段,该如何来实现呢??

假如上传页面的一个表单是如下所示:

<FORM METHOD=POST ACTION="upload.jsp">
<INPUT TYPE="file" NAME="image"><BR>
<INPUT TYPE="submit"></FORM>

那对应的upload.jsp程序该如何来编写呢????有测试成功的例子最好了,如果测试成功,满分赠送!


还有,在帮我看看以下这个问题[我是这个论坛上草写来的],就是每次执行PreparedStatement ps = gisconn.prepareStatement("insert into img (id,pic) values('1','2')"); 这个语句的时候,老是报错。

<%
//获取pic的路径
String pic="c:\\a.gif";
//转换成file格式
File file = new File(pic);
//将文件的长度读出,并转换成Long型
long l1=file.length();
int l2=(int)l1;
//以流的格式赋值
//FileInputStream fis=new FileInputStream(file);
FileInputStream fis=null;
fis = new FileInputStream(file);
//预编译SQL语句
PreparedStatement ps = gisconn.prepareStatement("insert into img (id,pic) values('1','2')");
%>

这个preparedstatement这么烦啊!
...全文
584 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunnf 2004-03-19
  • 打赏
  • 举报
回复
PreparedStatement pst=gisconn.prepareStatement("insert into tn(id,pic) values ?,?)");
这个语法有错误。
PreparedStatement pst=gisconn.prepareStatement("insert into tn(id,pic) values (?,?)");
neuyqy 2003-07-31
  • 打赏
  • 举报
回复
gisconn是你的连接?检查一下标点符号是不是有问题!
Hodex 2003-07-31
  • 打赏
  • 举报
回复
你的gisconn是从哪来的

//Connection conn=此处是你的数据库连接;
//conn.setAutoCommit(false);
为什么是注释,源代码?
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
对于jsp处理blob问题,我现在也了解了,但有一个困难老是困惑着我,就是为什么这句sql老是出错呢?

PreparedStatement pst=gisconn.prepareStatement( "insert into tn(id,pic) values(123,empty_blob()) ");

错误信息:
cannot resolve symbol
symbol : method prepareStatement (java.lang.String)
location: class bbs_class.bbs_connection
PreparedStatement pst=gisconn.prepareStatement( "insert into tn(id,pic) values(123,empty_blob()) ");
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
conning333(chen),你说的很对,在这个论坛上是有很多关于这个,但是关于jsp处理blob很少,都是关于java处理blob的,我现在想学的是jsp处理blob这个问题,而不想是java处理blob的!
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
<%@ page language="java" import="java.sql.*,java.lang.*" %>
<jsp:useBean id="gisconn" scope="page" class="bbs_class.bbs_connection" />
<%@ page import="java.io.*" %>
<%
//Connection conn=此处是你的数据库连接;
//conn.setAutoCommit(false);
//向tn表中插入一条记录
PreparedStatement pst=gisconn.prepareStatement( "insert into tn(id,pic) values(123,empty_blob()) ");
pst.executeUpdate();
pst.close();
%>

还是出现我以前的错误:PreparedStatement pst=gisconn.prepareStatement( "insert into tn(id,pic) values(123,empty_blob()) "); 这句话出问题。
conning333 2003-07-31
  • 打赏
  • 举报
回复
其实关于clob,blob的问题已经问了很多了,我想在csdn上一定有很多现成的可以参考
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
谢谢,比较清楚了!我试试,成功一定实现诺言给你加分
Hodex 2003-07-31
  • 打赏
  • 举报
回复
出错信息是什么
neuyqy 2003-07-31
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>上传文件</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
<%
Connection conn=此处是你的数据库连接;
conn.setAutoCommit(false);


//向tn表中插入一条记录
PreparedStatement pst=conn.prepareStatement("insert into tn(id,pic) values(123,empty_blob())");
pst.executeUpdate();
pst.close();

//返回blob字段的句柄
String sql_findblob="select pic from tn where id=123";
Statement st3=conn.createStatement();
ResultSet rs3=st3.executeQuery(sql_findblob);
OutputStream ops=null;
if(rs3.next()){
Blob blob =rs3.getBlob(1);
ops=((oracle.sql.BLOB)blob).getBinaryOutputStream();
}
rs3.close();
st3.close();
//写blob
File file=new File(你要上传的文件名);
FileInputStream is=new FileInputStream(file);
int a=(int)filelength;
byte[] b=new byte[a];
int i = 0;
int itotal = 0;
for (; itotal < a; itotal = i + itotal) {
i = is.read(b, itotal, a- itotal);
}
is.close();
ops.write(b);
ops.close();
conn.commit();
conn.close();
%>
</body>
</html>


jinglihua 2003-07-31
  • 打赏
  • 举报
回复
没有人知道这个问题吗?
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
楼上的程序有问题啊!
我的上传页面程序是
<FORM METHOD=POST ACTION="upload.jsp">
<INPUT TYPE="file" NAME="image"><BR>
<INPUT TYPE="submit"></FORM>

现在我想知道upload.jsp程序!!!!!!!!!
我的数据表是这样的:
表名:tn
字段名:id number
pic blob

那这个upload.jsp程序如何来编写呢?
wsyab 2003-07-31
  • 打赏
  • 举报
回复
做记号
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
你有QQ号吗?我有点问题想问你

7866635
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
谢谢,我先测试一下,如果同了,一定给你加分,先谢谢了!
neuyqy 2003-07-31
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<%@ page import="template.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>上传文件</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
<%
Connection conn=dbconn.getconn();
conn.setAutoCommit(false);

//取用户的登陆名和cateid
Statement st=conn.createStatement();
ResultSet rs=null;
//String slogname=session.getAttribute("slogname").toString();
String slogname="111";
//String sadminipassword=session.getAttribute("sadminipassword").toString();//cateid
String sadminipassword="4";
//取标题
String biaoti=request.getParameter("title");

//取得文件名
String filename=request.getParameter("filename");
filename=Chinese.toChinese(filename);
File nfile=new File(filename);
String nfilename=nfile.getName();
long filelength=yqy_blob.getFilelength(filename);
//取得说明的内容
String remark=request.getParameter("remark");
//向t_documentinfo表中插入一条记录
PreparedStatement pst=conn.prepareStatement("insert into t_documentinfo(username,biaoti,cateid,others,content) values('"+slogname+"','"+biaoti+"','"+sadminipassword+"','"+remark+"',empty_blob())");
pst.executeUpdate();
pst.close();
//找到这条新记录的行号
String sql_findrow="select rowid from t_documentinfo where tdate=(select max(tdate) from t_documentinfo) and username='"+slogname+"'";
Statement st2=conn.createStatement();
ResultSet rs2=st2.executeQuery(sql_findrow);
rs2.next();
String rowid=rs2.getString(1);
rs2.close();
st2.close();
System.err.println(rowid);
//返回blob字段的句柄
String sql_findblob="select content from t_documentinfo where rowid='"+rowid+"'";
Statement st3=conn.createStatement();
ResultSet rs3=st3.executeQuery(sql_findblob);
OutputStream ops=null;
if(rs3.next()){
Blob blob =rs3.getBlob(1);
ops=((oracle.sql.BLOB)blob).getBinaryOutputStream();
}
rs3.close();
st3.close();
//写blob
File file=new File(filename);
FileInputStream is=new FileInputStream(file);
int a=(int)filelength;
byte[] b=new byte[a];
int i = 0;
int itotal = 0;
for (; itotal < a; itotal = i + itotal) {
i = is.read(b, itotal, a- itotal);
}
is.close();
ops.write(b);
ops.close();
conn.commit();
conn.close();
%>
</body>
</html>


好用的,测试通过了!
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
我们可以看简单的例子:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="java.sql.*,java.lang.*,java.io.*" %>
<jsp:useBean id="gisconn" scope="page" class="bbs_class.bbs_connection" />



<%
//向tn表中插入一条记录
PreparedStatement pst=gisconn.prepareStatement("insert into tn(id,pic) values ?,?)");
pst.setString(1, "123");
pst.setBlob(2, empty_blob());
pst.executeUpdate();
pst.close();
%>

为什么老是PreparedStatement pst=gisconn.prepareStatement("insert into tn(id,pic) values ?,?)"); 这句话抱错呢!
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
完整的程序代码如下:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="java.sql.*,java.lang.*,java.io.*" %>
<jsp:useBean id="gisconn" scope="page" class="bbs_class.bbs_connection" />
//以上的gisconn是数据库连接bean

<%
//向tn表中插入一条记录
PreparedStatement pst=conn.prepareStatement( "insert into tn(id,pic) values(123,empty_blob()) ");
pst.executeUpdate();
pst.close();

//返回blob字段的句柄
String sql_findblob= "select pic from tn where id=123 ";
Statement st3=conn.createStatement();
ResultSet rs3=st3.executeQuery(sql_findblob);
OutputStream ops=null;
if(rs3.next()){
Blob blob =rs3.getBlob(1);
ops=((oracle.sql.BLOB)blob).getBinaryOutputStream();
}
rs3.close();
st3.close();
//写blob
File file=new File(你要上传的文件名);
FileInputStream is=new FileInputStream(file);
int a=(int)filelength;
byte[] b=new byte[a];
int i = 0;
int itotal = 0;
for (; itotal < a; itotal = i + itotal) {
i = is.read(b, itotal, a- itotal);
}
is.close();
ops.write(b);
ops.close();
conn.commit();
conn.close();
%>


为什么老是执行这句话PreparedStatement pst=conn.prepareStatement( "insert into tn(id,pic) values(123,empty_blob()) "); 出现错误??
错误形式:
cannot resolve symbol
symbol : method prepareStatement (java.lang.String)
location: class bbs_class.bbs_connection
PreparedStatement pst=gisconn.prepareStatement( "insert into tn(id,pic) values(123,empty_blob()) ");


望你们能帮我看看威何出错!
jinglihua 2003-07-31
  • 打赏
  • 举报
回复
gisconn是我的数据库连接Bean啊

81,090

社区成员

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

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