第一、利用FileInputStream.read(byte[])方法把内容读取到byte[]数组中,比如图片是由二进制数组成的,就可以定义为一个字节数组。
第二、在数据库中对应记录字段应该设置为blob类型,这样就能够顺利保存了
事例代码如下:
PreparedStatement
stmt
=
connection.generatePreparedStatement("INSERT
INTO
...
")
stmt.setBytes(1,
yourByteArray)
其中,yourByteArray是你读出来的字符数组。
以java代码为例:try{
conn = this.getConnection()
conn.setAutoCommit(false)
java.sql.Statement st = conn.createStatement()
String sql1 = "insert into test_image(test_id,image) values ("123",empty_blob())"
System.out.println("--------->"+sql1)
String stl2 = "select image from test_image where test_id='"+test.getId()+"' for update"
ResultSet rs = st.executeQuery(stl2)
OutputStream outStream = null
if (rs.next())
{
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("image")
outStream = blob.getBinaryOutputStream()
outStream.write(byte[],0, byte[].length)
}
outStream.flush()
outStream.close()
conn.commit()
conn.close()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)