3 import java
.sql
.Connection
;
4 import java
.sql
.ResultSet
;
5 import java
.sql
.Statement
;
7 import javax
.naming
.Context
;
8 import javax
.naming
.InitialContext
;
9 import javax
.sql
.DataSource
;
11 public class SQLHelper
{
13 private Connection SQLConnection
;
14 private DataSource datasource
;
16 public SQLHelper() throws Exception
18 // Obtain our environment naming context
19 Context initCtx
= new InitialContext();
20 Context envCtx
= (Context
) initCtx
.lookup("java:comp/env");
22 // Look up our data source
23 datasource
= (DataSource
) envCtx
.lookup("jdbc/REMOTE");
26 public void openDB() throws Exception
28 SQLConnection
= datasource
.getConnection();
31 public ResultSet
retrieve(String query
)
35 Statement stmt
= SQLConnection
.createStatement();
36 rs
= stmt
.executeQuery(query
);
37 } catch (Exception e
) {
39 System
.out
.println("Query: " + query
);
44 public ResultSet
retrieveRW(String query
)
48 Statement stmt
= SQLConnection
.createStatement(java
.sql
.ResultSet
.TYPE_SCROLL_SENSITIVE
,
49 java
.sql
.ResultSet
.CONCUR_UPDATABLE
);
50 rs
= stmt
.executeQuery(query
);
51 } catch (Exception e
) {
52 System
.out
.println("Exception: " + e
.toString());
53 System
.out
.println("Query: " + query
);
58 public int update(String query
)
62 Statement stmt
= SQLConnection
.createStatement();
63 count
= stmt
.executeUpdate(query
);
64 } catch (Exception e
) {
65 System
.out
.println("Exception: " + e
.toString());
66 System
.out
.println("Query: " + query
);
71 public void execute(String query
)
74 Statement stmt
= SQLConnection
.createStatement();
76 } catch (Exception e
) {
77 System
.out
.println("Exception: " + e
.toString());
78 System
.out
.println("Query: " + query
);
85 SQLConnection
.close();
89 System
.out
.println("Exception: " + e
.getMessage());