數(shù)據(jù)庫驅(qū)動更新為classes12-9i.jar
修改hibernate的配置如下
修改hibernate的配置如下
代碼 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
在有clob類型sessionFactory(org.springframework.com
<property name="lobHandler"><ref bean="oracleLobHandle"/><
在需要clob的JavaBean對應(yīng)的hbm.xml中,改對應(yīng)的clob類型的字段
代碼 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
JavaBean的對應(yīng)的屬性類型為String
------------------------------------------------------------------------------
----hibernate3 使用blob 和 clob 演例代碼-----
hibernate3 使用blob 和 clob 演例代碼:
org link: http://www.matrix.org.cn/thread.shtml?topicId=27138&forumId=23
1
import java.io.File;
2
import java.io.FileInputStream;
3
import java.io.OutputStream;
4
import java.io.Writer;
5
import java.sql.Clob;
6
import oracle.sql.CLOB;
7
import org.hibernate.LockMode;
8
import org.hibernate.Transaction;
9
import org.hibernate.Session;
10
import org.hibernate.Hibernate;
11
import org.hibernate.lob.SerializableBlob;
12
import org.hibernate.lob.SerializableClob;
13
14
15
public class test1
{
16
17
/** *//**
18
* @param args
19
*/
20
static Session session = HibernateSessionFactory.currentSession();
21
22
public boolean inserted(TStu obj)
{
23
24
Transaction tran = session.beginTransaction();
25
session.save(obj);
26
session.flush();
27
session.refresh(obj,LockMode.UPGRADE);
28
try
29
{
30
SerializableBlob sb = (SerializableBlob)obj.getImg();
31
java.sql.Blob wrapblob = sb.getWrappedBlob();
32
if(wrapblob instanceof oracle.sql.BLOB)
{
33
oracle.sql.BLOB blob = (oracle.sql.BLOB) wrapblob;
34
OutputStream bout = blob.getBinaryOutputStream();
35
FileInputStream fin = new FileInputStream("d:\\a.jpg");//修改你要存入的圖片
36
byte [] buf = new byte[10240];//做為10K的緩存寫入
37
int len;
38
while((len = fin.read(buf))>0)
{
39
bout.write(buf,0,len);
40
}
41
bout.close();
42
fin.close();
43
}
44
45
SerializableClob sc = (SerializableClob)obj.getResu();
46
Clob wrapclob = sc.getWrappedClob();
47
if(wrapclob instanceof CLOB)
{
48
CLOB clob = (CLOB)wrapclob;
49
Writer cout = clob.getCharacterOutputStream();
50
File file = new File("C:\\log_2005_8.txt");//修改你要存如的文本
51
FileInputStream fin = new FileInputStream(file);
52
int read;
53
while((read = fin.read())!= -1)
{
54
cout.write(read);
55
}
56
fin.close();
57
cout.close();
58
}
59
60
tran.commit();
61
return true;
62
}catch(Exception ex)
{
63
ex.printStackTrace();
64
tran.rollback();
65
return false;
66
}
67
}
68
69
public static void main(String[] args)
{
70
// TODO Auto-generated method stub
71
test1 t = new test1();
72
TStu stu = new TStu();
73
stu.setAge(new Integer("23"));
74
stu.setName("lilei");
75
stu.setImg(Hibernate.createBlob(new byte[1]));
76
stu.setResu(Hibernate.createClob(" "));
77
t.inserted(stu);
78
}
79
}
80
81

2

3

4

5

6

7

8

9

10

11

12

13

14

15



16

17


18

19

20

21

22



23

24

25

26

27

28

29



30

31

32



33

34

35

36

37

38



39

40

41

42

43

44

45

46

47



48

49

50

51

52

53



54

55

56

57

58

59

60

61

62



63

64

65

66

67

68

69



70

71

72

73

74

75

76

77

78

79

80

81

org link: http://www.matrix.org.cn