谁帮忙把下面的例子改成向数据库中添加一条记录而不是update

fzdcn 2003-09-29 07:54:52


下面上传到数据库中的例子是更新一条记录,而不是插入一条新的纪录。
谁有向数据库中插入新的记录的例子 (要求上载的文件也保存到数据库中,感激不尽)
原来的代码是:
<%@ page language="java" import="java.sql.*,com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<HTML>
<BODY BGCOLOR="white">
<H1>jspSmartUpload : Sample 4</H1>
<HR>
<%
// Variables
int count=0;

// Connect to the database
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql:///test");

// SQL Request
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM TFILES WHERE ID=1");

// if the resultset is not null
if (rs.next()){

// Initialization
mySmartUpload.initialize(pageContext);

// Upload
mySmartUpload.upload();

// upload file in the DB if this file is not missing
if (!mySmartUpload.getFiles().getFile(0).isMissing()){

try {

rs.updateString("FILENAME",mySmartUpload.getFiles().getFile(0).getFileName());

// Add the current file in the DB field
mySmartUpload.getFiles().getFile(0).fileToField(rs,"FILE");

// Update
rs.updateRow();
count++;

} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}

}

}

// Display the number of files uploaded
out.println(count + " file(s) uploaded in the database.");

rs.close();
stmt.close();
con.close();

%></BODY></HTML>
...全文
34 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cen8535 2003-10-09
  • 打赏
  • 举报
回复
try {
rs.moveToInsertRow();
rs.updateString("FILENAME",mySmartUpload.getFiles().getFile(0).getFileName());
mySmartUpload.getFiles().getFile(0).fileToField(rs,"FILE");
rs.insertRow();
count++;
} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}

81,091

社区成员

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

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