采用大对象类型。
mysql 大对象存取:
类型一般应该用mediumblod,
blob只能存2的16次方个byte,
mediumblod是24次方,
一般来说够用了.longblob是32次方有些大.
MYSQL默认配置只能存1M大小的文件,要修改配置,WIN版本的在mysql.ini文件中
修改max_allowed_packet,net_buffer_length等几个参数,或直接SET GLOBAL varName=value.
linux版本可以在启动参数后加-max_allowed_packet=xxM等几个参数.
MYSQL存大对象最好直接就setBinaryStream,又快又方便.
而不要先插入空再造型成BLOB然后再setBlob
例子:
import java.sql.*;
import java.io.*;
public class DBTest {
static String driver = "org.gjt.mm.mysql.Driver";
static String url = "jdbc:mysql://localhost:3306/test";
static String user = "root";
static String passwd = "passwd";
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,passwd);
int op = 1;
//插入
if (op == 0) {
PreparedStatement ps = conn.prepareStatement("insert into tb_file values (?,?)");
ps.setString(1, "aaa.exe");
InputStream in = new FileInputStream("d:/aaa.exe");
ps.setBinaryStream(2,in,in.available());
ps.executeUpdate();
ps.close();
}
else {
//取出
PreparedStatement ps = conn.prepareStatement("select * from tb_file where filename = ?");
ps.setString(1, "aaa.exe");
ResultSet rs = ps.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("filecontent");
System.out.println(in.available());
FileOutputStream out = new FileOutputStream("d:/bbb.exe");
byte[] b = new byte[1024];
int len = 0;
while ( (len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
rs.close();
ps.close();
}
}
catch (Exception ex) {
ex.printStackTrace(System.out);
}
finally {
try {conn.close();}
catch (Exception ex) { }
}
}
}