import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author he
*/
public class SaveER {
public void save(String file){
{
FileInputStream fis = null;
File f=new File(file);
Connection conn = null;
PreparedStatement pstmt = null;
try {
String sql = "insert into ss values(15,?)";
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement(sql);
fis = new FileInputStream(f);
// pstmt.setBinaryStream(1, fis,fis.available());//保存普通數據
pstmt.setBinaryStream(1, fis, (int)f.length());//保存圖片採用該句話
pstmt.execute();
System.out.println("保存成功!");
} catch (IOException ex) {
Logger.getLogger(SaveER.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(SaveER.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
try {
fis.close();
DBUtil.close(null, pstmt, conn);
} catch (IOException ex) {
Logger.getLogger(SaveER.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void main(String args[]){
SaveER er=new SaveER();
er.save("D:/1.jpg");
}
}
用到的數據表爲
create table ss(
id int(4),
pic blob);