1 /***
2 * This class is the interface between the agent and its underlying database
3 * Ultimately its role will be to encapsulate the EJB server
4 */
5
6 package zeus.ext;
7
8 import java.sql.*;
9 import java.util.*;
10
11 import zeus.concepts.*;
12
13
14 public class DbConnector implements java.io.Serializable
15 {
16
17 protected Connection dbConn = null;
18
19
20 protected String username = null;
21 protected String password = null;
22 protected String JDBCdriverName = null;
23 protected String JDBCconnectionName = null;
24
25
26 public DbConnector(String user, String pw, String driver, String conn)
27 {
28 username = user;
29 password = pw;
30 JDBCdriverName = driver;
31 JDBCconnectionName = conn;
32 }
33
34
35 /***
36 * Connect to the db
37 * @return true if connection is successful, false otherwise
38 */
39
40 public boolean connect() throws SQLException
41 {
42 try
43 {
44 java.util.Properties props = new java.util.Properties();
45 if (username != null) props.put("user", username);
46 if (password != null) props.put("password", password);
47 if (JDBCdriverName == null || JDBCconnectionName == null)
48 throw new SQLException("JDBC connection attempted without driver and connection being specified");
49 Class.forName(JDBCdriverName).newInstance();
50 dbConn = DriverManager.getConnection(JDBCconnectionName, props);
51 }
52 catch(Exception e)
53 {
54 e.printStackTrace();
55 throw new SQLException("Cannot create database driver... exiting");
56 }
57 return true;
58 }
59
60 /***
61 * Get the connection object for the database
62 * This is used to initiate calleable statements for PL/SQL calls
63 * @return the connection object to the db
64 */
65
66 public Connection getConnection() throws SQLException {
67 if(dbConn == null || dbConn.isClosed())
68 connect();
69 return dbConn;
70 }
71
72
73 /***
74 * Get a statement associated with the connection
75 * @return a statement object or null if none can be created
76 */
77
78 public Statement getStatement() throws SQLException {
79 if(dbConn == null || dbConn.isClosed())
80 connect();
81 return dbConn.createStatement();
82 }
83
84 /***
85 * Check to see if an active connection exists
86 * @return true if a connection is live, false otherwise
87 */
88
89 public boolean activeConnection() throws SQLException {
90 if(dbConn == null || dbConn.isClosed())
91 return false;
92 else
93 return true;
94 }
95
96 /***
97 * Close the connection to the db
98 */
99
100 public void close() {
101 try {
102 if(dbConn != null && !dbConn.isClosed())
103 dbConn.close();
104 } catch(SQLException e) {
105 System.out.println("SQL exception thrown while trying to close a db connection");
106 }
107 }
108
109 /***
110 * Execute an sql query
111 * @param query the sql query to execute
112 * @return the ResultSet from the query
113 */
114
115 public ResultSet sqlQuery(String query) throws SQLException {
116 if(!activeConnection())
117 connect();
118 Statement stmt = dbConn.createStatement();
119 return stmt.executeQuery(query);
120 }
121
122 /***
123 * Execute an SQL command
124 * @param command the sql command to execute
125 */
126
127 public void sqlCommand(String command) throws SQLException {
128 if(!activeConnection())
129 connect();
130 Statement stmt = dbConn.createStatement();
131 stmt.execute(command);
132 }
133
134
135
136 /*** The CRUD methods ***************************************************/
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public static void main(String[] Args)
164 {
165 System.out.println("Starting DbTest");
166
167
168
169
170
171 DbConnector dbc = new DbConnector ("database","tiger", "oracle.jdbc.driver.OracleDriver",
172 "jdbc:oracle:oci8:@(description=(address=(host=tb-toledo)(protocol=tcp)(port=1521))(connect_data=(sid=visitor)))");
173 try
174 {
175 ResultSet rs = dbc.sqlQuery("select count(*) from table1");
176 while(rs.next() != false)
177 {
178 int val = rs.getInt("count(*)");
179 System.out.println("result = " + val);
180 }
181 }
182 catch(SQLException e)
183 {
184 System.out.println("Ooops: " + e);
185 }
186 }
187 }