1
package chapter30;
2
3
import java.io.UnsupportedEncodingException;
4
import java.sql.Connection;
5
import java.sql.DriverManager;
6
import java.sql.ResultSet;
7
import java.sql.SQLException;
8
import java.sql.Statement;
9
10
import com.checker.ValueChecker;
11
12
/**
13
* Title:
14
* Description:
15
* Dec 14, 2007 11:13:12 AM
16
* version:
17
* @author: Louis
18
*/
19
20
public class DBManager
21
{
22
private String className = "oracle.jdbc.driver.OracleDriver";
23
private String url = "jdbc:oracle:thin:@172.29.21.40:1521:eprodb";
24
private String uid = "scott";
25
private String pwd = "tiger";
26
private Connection conn;
27
private Statement stmt;
28
private String sql;
29
private ResultSet rset;
30
31
/**
32
* Title: constructor
33
* Description:
34
* @param:
35
* @exception:
36
*/
37
public DBManager()
38
{
39
try
40
{
41
Class.forName(className);// Load the driver class.
42
conn = DriverManager.getConnection(url, uid, pwd);// Create a connection through the JDBC method
43
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);// Create a statement.
44
}
45
catch (ClassNotFoundException cnfe)
46
{
47
cnfe.printStackTrace();
48
}
49
catch (SQLException sqle)
50
{
51
sqle.printStackTrace();
52
}
53
}
54
55
public void setSql(String cond)
56
{
57
sql = cond;
58
sql = ValueChecker.stringNullZeroProcess(sql);
59
try
60
{
61
sql = new String(sql.getBytes("iso-8859-1"));
62
}
63
catch (UnsupportedEncodingException uee)
64
{
65
uee.printStackTrace();
66
}
67
}
68
69
public Connection getConn()
70
{
71
return conn;
72
}
73
74
/**
75
* Title: Query operation and return result sets
76
* Description:
77
* @param:
78
* @return: ResultSet
79
* @exception:
80
*/
81
public ResultSet query(String cond)
82
{
83
setSql(cond);// Calling setSql() method.
84
try
85
{
86
if (stmt != null && !ValueChecker.stringIsNullZero(sql))
87
{
88
rset = stmt.executeQuery(sql);
89
}
90
}
91
catch (SQLException sqle)
92
{
93
sqle.printStackTrace();
94
}
95
return rset;
96
}
97
98
/**
99
* Title: delete, update, insert, drop operations and return row count of operation.
100
* Description:
101
* @param:
102
* @return: boolean
103
* @exception:
104
*/
105
public boolean update(String cond)
106
{
107
boolean result = false;
108
setSql(cond);// Calling setSql() method.
109
try
110
{
111
if (stmt != null && !ValueChecker.stringIsNullZero(sql))
112
{
113
result = stmt.executeUpdate(this.sql) > 0 ? true : false;
114
}
115
}
116
catch (SQLException sqle)
117
{
118
sqle.printStackTrace();
119
}
120
return result;
121
}
122
123
/**
124
* Title: Close connection
125
* Description:
126
* @param:
127
* @return: void
128
* @exception:
129
*/
130
public void close()
131
{
132
try
133
{
134
if (rset != null)
135
rset.close();
136
if (stmt != null)
137
stmt.close();
138
if (conn != null)
139
conn.close();
140
}
141
catch (SQLException sqle)
142
{
143
sqle.printStackTrace();
144
}
145
}
146
}

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

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146
