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