Oracle中插入图片并显示(用BLOB类型)
要在oracle里面存入图片 用 blob类型
首先在数据库里建立:
--连接到管理员
conn sys/tbsoft as sysdba;
--为scott用户授权
grant create any directory to scott;
--回到scott用户
conn scott/tiger;
--创建存储图片的表
CREATE TABLE IMAGE_LOB (T_ID VARCHAR2 (5) NOT NULL,T_IMAGE BLOB NOT NULL);
--创建存储图片的目录
CREATE OR REPLACE DIRECTORY IMAGES AS 'C:\\picture';
--在c:下自己建一个叫picture的文件夹
CREATE OR REPLACE PROCEDURE IMG_INSERT (TID VARCHAR2,FILENAME VARCHAR2) AS
F_LOB BFILE;--文件类型
B_LOB BLOB;
BEGIN
--插入空的blob
iNSERT INTO IMAGE_LOB (T_ID, T_IMAGE)
VALUES (TID,EMPTY_BLOB ()) RETURN T_IMAGE INTO B_LOB;
--获取指定目录下的文件
F_LOB:= BFILENAME ('IMAGES', FILENAME);
--以只读的方式打开文件
DBMS_LOB.FILEOPEN(F_LOB, DBMS_LOB.FILE_READONLY);
--传递对象
DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB,DBMS_LOB.GETLENGTH (F_LOB));
--关闭原始文件
DBMS_LOB.FILECLOSE (F_LOB);
COMMIT;
END;
/
--在C:\\picture下放一张图片
--将该图片存入表
然后创建一个web项目 连接数据库后 创建一个BlobDAO类 用来取出表中的blob类型图片
Java代码 收藏代码
public class BlobDAO {
private static final BlobDAO instance = new BlobDAO();
private Connection conn = null;
private BlobDAO() {
}
public static BlobDAO getInstance() {
return instance;
}
private void initConn() {
conn = DBAccess.getInstance().getConn();
}
public byte[] getImage(String imgname) {
BufferedInputStream ins;//取得BLOB的IO流
byte[] bt = null;
initConn();
Blob bo = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select T_IMAGE from IMAGE_LOB where t_id=?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, imgname);
rs = ps.executeQuery();
if (rs.next()) {
bo = rs.getBlob("T_IMAGE");
try {
ins = new BufferedInputStream(bo.getBinaryStream());
int bufferSize = (int) bo.length();//取得BLOB的长度
bt = new byte[bufferSize];
try {
ins.read(bt, 0, bufferSize);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//建立字节缓存
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bt;
}
}
在action里面调用getImage()方法并显示图片在页面上
Java代码 收藏代码
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
BlobDAO blobDAO = BlobDAO.getInstance();
byte[] bs = blobDAO.getImage("1");
try {
response.getOutputStream().write(bs);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
添加图片到数据库
请在c盘下放入图片
Java代码 收藏代码
public void savaImg(String imgId) {
//传的是存入数据库图片的id
initConn();
Statement st = null;
BLOB blob = null; //图片类型
OutputStream outputStream = null; //输出流
File file = null; //文件
InputStream inputStream = null; //输入流
ResultSet rs = null;
try {
conn.setAutoCommit(false); //事物由程序员操作
st = conn.createStatement();
st.executeQuery("insert into IMAGE_LOB values('"+ imgId +"',empty_blob())");
rs = st.executeQuery("select T_IMAGE from IMAGE_LOB where t_id='"+ imgId +"' for update");
if (rs.next()) {
blob = (BLOB) rs.getBlob(1);
outputStream = blob.getBinaryOutputStream();
inputStream = new FileInputStream(file);
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
System.out.println(len);
outputStream.write(b, 0, len);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.flush();
outputStream.close();
rs.close();
st.close();
conn.commit();
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
操作完毕!
示例二:
插入oracle blob
1.package db;
2.
3.import java.io.BufferedInputStream;
4.import java.io.FileInputStream;
5.import java.io.PrintStream;
6.import java.sql.Connection;
7.import java.sql.DriverManager;
8.import java.sql.ResultSet;
9.import java.sql.SQLException;
10.import java.sql.Statement;
11.
12.public class Test {
13. private Connection conn;
14.
15. /**
16. * 得到一个数据库的连接
17. *
18. * @return 返加Connection对象
19. */
20. public Connection getConnection() {
21. try {
22. Class.forName("oracle.jdbc.driver.OracleDriver");
23. conn = DriverManager.getConnection(
24. "jdbc:oracle:thin:@IP地址:1521:实例", "用户名", "密码");
25. } catch (ClassNotFoundException e) {
26. e.printStackTrace();
27. } catch (SQLException e) {
28. e.printStackTrace();
29. }
30. return conn;
31. }
32.
33. /**
34. * 向表中插入二进制数据
35. * @param path数据所在的路径
36. * @return 成功或失败
37. */
38. public int insertImage(String path) throws Exception {
39. int i = 0;
40. Statement st = null;
41. ResultSet rs = null;
42. conn=this.getConnection();
43.
44. conn.setAutoCommit(false);//设置数据库为不自动提交,必须
45. st = conn.createStatement();
46. //先插入一个空对象,这里插入Empty_BLOB()函数 stuid text
47. i = st.executeUpdate("insert into testbo(stuid,text) values(1,Empty_BLOB())");
48. //进行行级锁
49. rs = st.executeQuery("select text from testbo where stuid=1 for update");
50. if (rs.next()) {
51. //得到流
52. oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
53. //从得到的低级流构造一个高级流
54. PrintStream ps = new PrintStream(blob.getBinaryOutputStream());
55. BufferedInputStream bis = new BufferedInputStream(
56. new FileInputStream(path));
57. byte[] buff = new byte[512];
58. int n = 0;
59. //从输入到输出
60. while ((n = bis.read(buff)) != -1) {
61. ps.write(buff, 0, n);
62. }
63. //清空流的缓存
. ps.flush();
65. //关闭流,注意一定要关
66. ps.close();
67. bis.close();
68. }
69. rs.close();
70. st.close();
71. conn.close();
72. return i;
73. }
74.
75. public static void main(String[] args) throws Exception {
76. Test test=new Test();
77. test.insertImage("C:/MyDocuments/kettledemo/ANKG.SN.2008309124211.00.BHZ");
78. System.out.println("YES");
79. }
80.}
package db;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
private Connection conn;
/**
* 得到一个数据库的连接
*
* @return 返加Connection对象
*/
public Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@IP地址:1521:实例", "用户名", "密码");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 向表中插入二进制数据
* @param path数据所在的路径
* @return 成功或失败
*/
public int insertImage(String path) throws Exception {
int i = 0;
Statement st = null;
ResultSet rs = null;
conn=this.getConnection();
conn.setAutoCommit(false);//设置数据库为不自动提交,必须
st = conn.createStatement();
//先插入一个空对象,这里插入Empty_BLOB()函数 stuid text
i = st.executeUpdate("insert into testbo(stuid,text) values(1,Empty_BLOB())");
//进行行级锁
rs = st.executeQuery("select text from testbo where stuid=1 for update");
if (rs.next()) {
//得到流
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
//从得到的低级流构造一个高级流
PrintStream ps = new PrintStream(blob.getBinaryOutputStream());
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(path));
byte[] buff = new byte[512];
int n = 0;
//从输入到输出
while ((n = bis.read(buff)) != -1) {
ps.write(buff, 0, n);
}
//清空流的缓存
ps.flush();
//关闭流,注意一定要关
ps.close();
bis.close();
}
rs.close();
st.close();
conn.close();
return i;
}
public static void main(String[] args) throws Exception {
Test test=new Test();
test.insertImage("C:/MyDocuments/kettledemo/ANKG.SN.2008309124211.00.BHZ");
System.out.println("YES");
}
}