public class ExamJdbc11 {
public static void method1() {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = SQLHelper.getConnection();
String sql = "insert into tb_file values(?,?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, "c:/1.jpg");
InputStream in = new FileInputStream("c:/1.jpg");
stmt.setBinaryStream(2, in, in.available());//存入图片的大小,这样才可以取得
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
SQLHelper.close(null, stmt, conn);
}
}
//取出来
public static void method2() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = SQLHelper.getConnection();
String sql = "select * from tb_file where filename = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, "c:/1.jpg");
rs = stmt.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("filecontent");
FileOutputStream fout = new FileOutputStream("d:/2.jpg");
byte[] buff = new byte[1024];
int length = 0;
while ((length = in.read(buff)) != -1) {
fout.write(buff, 0, length);
fout.flush();
}
fout.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
SQLHelper.close(rs, stmt, conn);
}
}
public static void main(String[] args){
method2();
}
}
android 将图片保存到数据库
最新推荐文章于 2024-04-28 16:58:16 发布