Tidyup and remove whitespace at end of line
[remote/remote-ws.git] / src / database / SQLHelper.java
blobfd20e44b088c0b3c380f267fa52136784ba70c2d
1 package database;
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)
33 ResultSet rs = null;
34 try {
35 Statement stmt = SQLConnection.createStatement();
36 rs = stmt.executeQuery(query);
37 } catch (Exception e) {
38 e.printStackTrace();
39 System.out.println("Query: " + query);
41 return rs;
44 public ResultSet retrieveRW(String query)
46 ResultSet rs = null;
47 try {
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);
55 return rs;
58 public int update(String query)
60 int count = 0;
61 try {
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);
68 return count;
71 public void execute(String query)
73 try {
74 Statement stmt = SQLConnection.createStatement();
75 stmt.execute(query);
76 } catch (Exception e) {
77 System.out.println("Exception: " + e.toString());
78 System.out.println("Query: " + query);
82 public void closeDB()
84 try {
85 SQLConnection.close();
87 catch (Exception e)
89 System.out.println("Exception: " + e.getMessage());