Showing
1 changed file
with
23 additions
and
13 deletions
1 | package com.sitech.ismp.coll; | 1 | package com.sitech.ismp.coll; |
2 | 2 | ||
3 | +import org.apache.log4j.Logger; | ||
4 | + | ||
3 | import java.sql.Connection; | 5 | import java.sql.Connection; |
4 | import java.sql.DriverManager; | 6 | import java.sql.DriverManager; |
5 | import java.sql.PreparedStatement; | 7 | import java.sql.PreparedStatement; |
@@ -10,8 +12,6 @@ import java.util.HashMap; | @@ -10,8 +12,6 @@ import java.util.HashMap; | ||
10 | import java.util.Map; | 12 | import java.util.Map; |
11 | import java.util.Vector; | 13 | import java.util.Vector; |
12 | 14 | ||
13 | -import org.apache.log4j.Logger; | ||
14 | - | ||
15 | public class SQLTarget extends CollBase { | 15 | public class SQLTarget extends CollBase { |
16 | Logger logger = Logger.getLogger(SQLTarget.class); | 16 | Logger logger = Logger.getLogger(SQLTarget.class); |
17 | String ObjectIP = null; | 17 | String ObjectIP = null; |
@@ -189,19 +189,31 @@ public class SQLTarget extends CollBase { | @@ -189,19 +189,31 @@ public class SQLTarget extends CollBase { | ||
189 | Vector recordSet = new Vector(); | 189 | Vector recordSet = new Vector(); |
190 | PreparedStatement pstmt = null; | 190 | PreparedStatement pstmt = null; |
191 | ResultSet result = null; | 191 | ResultSet result = null; |
192 | - try { | ||
193 | if (!getState()) { | 192 | if (!getState()) { |
194 | return recordSet; | 193 | return recordSet; |
195 | } | 194 | } |
196 | 195 | ||
196 | + try { | ||
197 | pstmt = dbConnection.prepareStatement(sql); | 197 | pstmt = dbConnection.prepareStatement(sql); |
198 | - boolean hasResult = pstmt.execute(); | 198 | + } catch (SQLException e) { |
199 | + throw new RuntimeException("Exception while exec sql :" + sql, e); | ||
200 | + } | ||
201 | + | ||
202 | + boolean hasResult = false; | ||
203 | + try { | ||
204 | + hasResult = pstmt.execute(); | ||
205 | + } catch (SQLException e) { | ||
206 | + logger.error("pstmt.execute() has error!",e); | ||
207 | + e.printStackTrace(); | ||
208 | + } | ||
209 | + try { | ||
210 | + | ||
199 | if (hasResult) { | 211 | if (hasResult) { |
200 | result = pstmt.getResultSet(); | 212 | result = pstmt.getResultSet(); |
201 | ResultSetMetaData rsmd = result.getMetaData(); | 213 | ResultSetMetaData rsmd = result.getMetaData(); |
202 | int iColumnCount = rsmd.getColumnCount(); | 214 | int iColumnCount = rsmd.getColumnCount(); |
203 | while (result.next()) { | 215 | while (result.next()) { |
204 | - java.util.Vector vTemp = new Vector(); | 216 | + Vector vTemp = new Vector(); |
205 | for (int i = 0; i < iColumnCount; i++) { | 217 | for (int i = 0; i < iColumnCount; i++) { |
206 | Object oTemp = result.getObject(i + 1); | 218 | Object oTemp = result.getObject(i + 1); |
207 | String sTemp = oTemp == null ? "" : oTemp.toString(); | 219 | String sTemp = oTemp == null ? "" : oTemp.toString(); |
@@ -212,14 +224,9 @@ public class SQLTarget extends CollBase { | @@ -212,14 +224,9 @@ public class SQLTarget extends CollBase { | ||
212 | result.close(); | 224 | result.close(); |
213 | } | 225 | } |
214 | pstmt.close(); | 226 | pstmt.close(); |
215 | -// if (!"MySQL".equals(DBType)) { | ||
216 | -// dbConnection.commit(); | ||
217 | -// } | ||
218 | } catch (Exception e) { | 227 | } catch (Exception e) { |
219 | throw new RuntimeException("Exception while exec sql :" + sql, e); | 228 | throw new RuntimeException("Exception while exec sql :" + sql, e); |
220 | } finally { | 229 | } finally { |
221 | -// releaseResources(); | ||
222 | - | ||
223 | try { | 230 | try { |
224 | if(pstmt!=null){ | 231 | if(pstmt!=null){ |
225 | pstmt.close(); | 232 | pstmt.close(); |
@@ -239,11 +246,11 @@ public class SQLTarget extends CollBase { | @@ -239,11 +246,11 @@ public class SQLTarget extends CollBase { | ||
239 | getConnection(); | 246 | getConnection(); |
240 | try { | 247 | try { |
241 | if (!state) { | 248 | if (!state) { |
242 | - java.lang.Thread.sleep(5000);// 间隔5s重试第二次连接 | 249 | + Thread.sleep(5000);// 间隔5s重试第二次连接 |
243 | System.err.println("--second--conn error----"); | 250 | System.err.println("--second--conn error----"); |
244 | getConnection(); | 251 | getConnection(); |
245 | if (!state) { | 252 | if (!state) { |
246 | - java.lang.Thread.sleep(5000);// 再间隔5s重试第三次连接 | 253 | + Thread.sleep(5000);// 再间隔5s重试第三次连接 |
247 | System.err.println("--third--conn error----"); | 254 | System.err.println("--third--conn error----"); |
248 | getConnection(); | 255 | getConnection(); |
249 | } | 256 | } |
@@ -326,8 +333,11 @@ public class SQLTarget extends CollBase { | @@ -326,8 +333,11 @@ public class SQLTarget extends CollBase { | ||
326 | return kpiinfo; | 333 | return kpiinfo; |
327 | } | 334 | } |
328 | 335 | ||
336 | + /** | ||
337 | + * 释放数据库连接 | ||
338 | + */ | ||
329 | public void release() { | 339 | public void release() { |
330 | -// releaseResources(); | 340 | + releaseResources(); |
331 | } | 341 | } |
332 | 342 | ||
333 | } | 343 | } |
-
Please register or login to post a comment