Q
qiqijsp
Unregistered / Unconfirmed
GUEST, unregistred user!
我用jsp+sqlserver2000,上传图片用SmartUpload的Bean(只是先传到服务器一个目录),
可是在传入数据库时出了问题,共有四个字段(id(自动加一),name,password,picture)
,以下是源文件,出现空点错误[8D]
<html>
<head>
<title>存储图片</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%@ page language="java" contentType="text/html;charset=gb2312" %>
<%@ page import="java.io.*,java.sql.*,sql.*,com.jspsmart.upload.SmartUpload" %>
<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String aaaa="0";
int count=0;
myUpload.initialize(pageContext);
//这里使用了SmartUpload的Bean;
myUpload.setTotalMaxFileSize(512000);
myUpload.upload();
String names=request.getParameter("name");
//这里进行了中文转换;
byte[] temp_names=names.getBytes("ISO8859_1");
names=new String(temp_names);
String password=request.getParameter("password");
byte[] temppassword=password.getBytes("ISO8859_1");
password=new String(temppassword);
//连接数据库;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbcdbc:conn";
Connection conn=DriverManager.getConnection(url,"sa","sa");
Statement stmt=conn.createStatement();
String sql="insert into testid (name,password) values('"+names+"','"+password+"')";
stmt.executeUpdate(sql);
String selectsql="select @@identity as 'maxnum' from testid";//选出数据库中id的最大值,以便向这个记录插入图片;
ResultSet rs=stmt.executeQuery(selectsql);
if(rs.next()){
aaaa=rs.getString("maxnum");
//返回最大值;
}
try{
int num=Integer.parseInt(aaaa);
count=myUpload.save("/upload");
File file=new File("/upload/"+myUpload.getFiles().getFile(0).getFileName());
InputStream in =new FileInputStream(file);
int filelength=(int)file.length();
PreparedStatement stm=conn.prepareStatement("update testid set picture=? where id="+num);
stm.setBinaryStream(1,in,filelength);
stm.executeUpdate();
}
catch(Exception e){}
out.print("success");
%>
</body>
</html>
那个SmartUpload组件有向数据库传图片的方法,可是向sqlserver2000好像不好使,我只有
使用上面的方法。如果那位大侠有更好的方法,希望你们能告诉我,谢谢。
可是在传入数据库时出了问题,共有四个字段(id(自动加一),name,password,picture)
,以下是源文件,出现空点错误[8D]
<html>
<head>
<title>存储图片</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%@ page language="java" contentType="text/html;charset=gb2312" %>
<%@ page import="java.io.*,java.sql.*,sql.*,com.jspsmart.upload.SmartUpload" %>
<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String aaaa="0";
int count=0;
myUpload.initialize(pageContext);
//这里使用了SmartUpload的Bean;
myUpload.setTotalMaxFileSize(512000);
myUpload.upload();
String names=request.getParameter("name");
//这里进行了中文转换;
byte[] temp_names=names.getBytes("ISO8859_1");
names=new String(temp_names);
String password=request.getParameter("password");
byte[] temppassword=password.getBytes("ISO8859_1");
password=new String(temppassword);
//连接数据库;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbcdbc:conn";
Connection conn=DriverManager.getConnection(url,"sa","sa");
Statement stmt=conn.createStatement();
String sql="insert into testid (name,password) values('"+names+"','"+password+"')";
stmt.executeUpdate(sql);
String selectsql="select @@identity as 'maxnum' from testid";//选出数据库中id的最大值,以便向这个记录插入图片;
ResultSet rs=stmt.executeQuery(selectsql);
if(rs.next()){
aaaa=rs.getString("maxnum");
//返回最大值;
}
try{
int num=Integer.parseInt(aaaa);
count=myUpload.save("/upload");
File file=new File("/upload/"+myUpload.getFiles().getFile(0).getFileName());
InputStream in =new FileInputStream(file);
int filelength=(int)file.length();
PreparedStatement stm=conn.prepareStatement("update testid set picture=? where id="+num);
stm.setBinaryStream(1,in,filelength);
stm.executeUpdate();
}
catch(Exception e){}
out.print("success");
%>
</body>
</html>
那个SmartUpload组件有向数据库传图片的方法,可是向sqlserver2000好像不好使,我只有
使用上面的方法。如果那位大侠有更好的方法,希望你们能告诉我,谢谢。