mysql存取图片 博客分类: Java MySQLSQLJava
程序员文章站
2024-02-05 17:58:28
...
存入:
读取:
Connection conn = DBFactory.getTourie(); FileInputStream str = new FileInputStream("d:/aa.jpg"); String sql= "insert into test_img(filename,content) values(?,?) "; PreparedStatement pstmt= conn.prepareStatement(sql); pstmt.setString(1,"aaa"); pstmt.setBinaryStream(2,str,str.available()); pstmt.execute(); pstmt.close();
读取:
String sql = "select content from test_img where id=1 "; PreparedStatement pstmt =null; ResultSet rs=null; pstmt=conn.prepareStatement(sql); rs=pstmt.executeQuery(sql); while(rs.next()) { InputStream in = rs.getBinaryStream(1); FileOutputStream fos = new FileOutputStream("e://aa.jpg"); int temp = in.read(); while(temp != -1) { fos.write(temp); temp = in.read(); } fos.close(); in.close(); } rs.close(); pstmt.close(); conn.close();