从客户端里上传文件,读出文件内容,然后写到数据库里,我直接把文件内容写到内存里,用数组保存下来,然后遍历数组更新数据库。如果数据量大了,将会出现什么问题呢?有没有好点的解决方式?当数据量很大了,我现在做的客户数据量不是很大,最多也就是几兆,即能不用写到临时文件里面,也不用担心内存会溢出,也可能就是速度奇慢的? 或处理这类问题,有没有其他的方案,就是读取文件的内容,更新到数据库里?
调试欢乐多
然后循环读取 ,边读边写(可以是写到文件,也可以是写到数据库). 这样不管你的文件有多大(当然不能比你的硬盘还要大)
都可以把他 分割成 buff.lenght 大小. 如果要控制性能 实际上就是要控制 buff的大小.代码: 可以参看一下.http://blog.csdn.net/caoyinghui1986/archive/2008/04/05/2252772.aspx
剩下的就是用户文件上传,一般我们都是把文件保存在服务器目录中,不保存在数据库中,这样如果用想看到文件的话,我们给连接或让用户下载这样速度会快很多。剩下就是一些其他的处理,如果我没说明白或是lz没理解的话 可以再联系我
看下我这个代码import java.sql.*;
import java.io.*;
public class Mp3Test{
public static Connection con=null;
public static PreparedStatement ps=null;
public static void main(String[] args){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=pubs";
con=DriverManager.getConnection(url,"sa","");
InputStream in=new FileInputStream("f:/1.mp3");
String sql="insert into [Table] values(?)";
ps=con.prepareStatement(sql);
ps.setBinaryStream(1,in,in.available());
System.out.println("insert into Database");
ps.executeUpdate();
OutputStream out=new FileOutputStream("c:/2.mp3");
String sql1="select * from [Table]";
ps=con.prepareStatement(sql1);
ResultSet rs=ps.executeQuery();
rs.next();
InputStream in1=rs.getBinaryStream(2);
byte [] buffer=new byte[1024*2];
int length=0;
while((length=(in1.read(buffer,0,buffer.length)))!=-1){
out.write(buffer,0,buffer.length);
}
System.out.println("ok");
rs.close();
ps.close();
con.close();
}catch(ClassNotFoundException ce){
ce.printStackTrace();
}catch(SQLException se){
se.printStackTrace();
}catch(IOException ie){
ie.printStackTrace();
} }
}
搞懂了就差不多了
如何将db4o中的数据写到别的关系型数据库?