Source code: mill/db/ORAconnect.java
1 package mill.db;
2
3 import java.sql.SQLException;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.DriverManager;
7 import java.sql.Driver;
8 import java.io.OutputStreamWriter;
9 import java.io.FileOutputStream;
10
11 import java.lang.Class;
12
13 import java.util.Enumeration;
14
15 import oracle.jdbc.driver.*;
16 import oracle.sql.*;
17
18 import mill.startup.InitJSPparam;
19
20 //import mill.debug.Debug;
21
22 /**
23 Класс ORAconnect прденазначен для коннекта к оракловской базе данных.
24
25 $Date: 28/09/2000 $
26 */
27 public class ORAconnect extends DBconnect
28 {
29
30 public boolean getIsClosed()
31 throws SQLException
32 {
33 if (conn == null)
34 return true;
35 return conn.isClosed();
36 }
37
38 public int getMaxLengthStringField() {
39 return 4000;
40 }
41
42 protected void finalize() throws Throwable
43 {
44 if ( isDynamicConnect )
45 {
46 try {
47 conn.close();
48 conn = null;
49 }catch(Exception e){}
50 }
51 super.finalize();
52 }
53
54 public String getClobField(ResultSet rs, String nameField)
55 throws SQLException
56 {
57 return getClobField(rs, nameField, 20000);
58 }
59
60 public String getClobField(ResultSet rs, String nameField, int maxLength)
61 throws SQLException
62 {
63 CLOB clob = ((OracleResultSet)rs).getCLOB (nameField);
64
65 if (clob==null)
66 return null;
67
68 return clob.getSubString(1, maxLength);
69 }
70
71 public long getSequenceNextValue( String s )
72 throws SQLException
73 {
74 long id_ = -1;
75
76 String sql_ =
77 "select "+ s.trim() + ".nextval from dual";
78 PreparedStatement ps = null;
79 ResultSet rs = null;
80 try {
81 ps = this.conn.prepareStatement( sql_ );
82
83 rs = ps.executeQuery();
84
85 if (rs.next())
86 id_ = rs.getLong(1);
87
88 }
89 finally
90 {
91 if (rs != null)
92 {
93 try {
94 rs.close();
95 rs = null;
96 }
97 catch(SQLException e){}
98 }
99 if (ps != null)
100 {
101 try {
102 ps.close();
103 ps = null;
104 }
105 catch(SQLException e1){}
106 }
107 }
108
109 return id_;
110 }
111
112 public String getFirstValueString( String t, String f, String w, String o)
113 throws SQLException
114 {
115 // Debug db = new Debug();
116
117 String id_ = null;
118
119 String v_s = "select " + f + " from " + t;
120
121 if (o != null)
122 {
123 v_s += (w==null)? "": " "+w;
124 v_s += (" order by " + o);
125 }
126 else
127 {
128 v_s += (w==null)?
129 "":
130 " "+w+" and rownum<2 ";
131 }
132
133 // db.aM(v_s);
134
135
136 PreparedStatement prepStatement = null;
137 ResultSet rset = null;
138 try {
139 prepStatement = this.conn.prepareStatement(v_s);
140
141 rset = prepStatement.executeQuery();
142
143 if (rset.next())
144 id_ = rset.getString(1);
145
146 }
147 finally
148 {
149 if (rset != null)
150 {
151 try {
152 rset.close();
153 rset = null;
154 }
155 catch(SQLException e){}
156 }
157 if (prepStatement != null)
158 {
159 try {
160 prepStatement.close();
161 prepStatement = null;
162 }
163 catch(SQLException e1){}
164 }
165 }
166
167 return id_;
168
169 // return fromDB(id_);
170 }
171
172 public long getFirstValue( String t, String f, String w, String o)
173 throws SQLException
174 {
175 // Debug db = new Debug();
176
177 long id_ = -1;
178
179 String v_s = "select " + f + " from " + t;
180
181 if (o != null)
182 {
183 v_s += (w==null)? "": " "+w;
184 v_s += (" order by " + o);
185 }
186 else
187 {
188 v_s += (w==null)?
189 "":
190 " "+w+" and rownum<2 ";
191 }
192
193 // db.aM(v_s);
194
195
196 PreparedStatement prepStatement = null;
197 ResultSet rset = null;
198 try {
199 prepStatement = this.conn.prepareStatement(v_s);
200
201 rset = prepStatement.executeQuery();
202
203 if (rset.next())
204 id_ = rset.getLong(1);
205 }
206 finally
207 {
208 if (rset != null)
209 {
210 try {
211 rset.close();
212 rset = null;
213 }
214 catch(SQLException e){}
215 }
216 if (prepStatement != null)
217 {
218 try {
219 prepStatement.close();
220 prepStatement = null;
221 }
222 catch(SQLException e1){}
223 }
224 }
225
226 return id_;
227 }
228
229 public boolean testExceptionTableNotFound( Exception e )
230 {
231
232 if ((e instanceof SQLException) &&
233 (e.toString().indexOf("ORA-00942") != -1))
234 return true;
235 return false;
236 }
237
238 public boolean testExceptionIndexUniqueKey( Exception e, String index)
239 {
240 if ((e instanceof SQLException) &&
241 ((e.toString().indexOf("ORA-00001") != -1) &&
242 (e.toString().indexOf(index) != -1)) )
243
244 return true;
245
246 return false;
247 }
248
249
250 public void nop()
251 {
252 int i = 0;
253 }
254
255 /**
256 Этот метод создает коннект к серверу с указанным объектом типа ConnectionData.<br>
257
258 Параметры:
259 <blockquote>
260 cd - объект типа mill.db.ConnectionData<br>
261 </blockquote>
262 */
263 public ORAconnect(ConnectionData cd)
264 throws Exception
265 {
266 if (cd == null)
267 throw new SQLException("#21.001: ConnectionData data not initialized.");
268
269 if (!isDriverLoaded)
270 {
271 Class cl_ = Class.forName ( "oracle.jdbc.driver.OracleDriver" );
272 isDriverLoaded = true;
273 }
274
275 conn = (OracleConnection)DriverManager.getConnection
276 ("jdbc:oracle:thin:@" + cd.host, cd.login, cd.pass);
277
278 conn.setAutoCommit (cd.isCommit);
279
280 }
281
282
283 public ORAconnect()
284 throws Exception
285 {
286 this( getConnectionData() );
287 }
288
289 }