001: //DBBean.java
002:
003: package applis.ejb.perfs;
004:
005: import java.rmi.RemoteException;
006: import java.sql.Connection;
007: import java.sql.PreparedStatement;
008: import java.sql.Statement;
009:
010: import javax.ejb.EJBException;
011: import javax.ejb.SessionBean;
012: import javax.ejb.SessionContext;
013: import javax.naming.Context;
014: import javax.naming.InitialContext;
015: import javax.rmi.PortableRemoteObject;
016: import javax.sql.DataSource;
017:
018: public class DBBean implements SessionBean {
019:
020: SessionContext sc = null;
021: private DataSource dataSource = null;
022: Connection conn = null;
023: Statement stmt;
024: PreparedStatement pStmt;
025:
026: void dropTable(String name) throws RemoteException {
027: try {
028: conn = dataSource.getConnection();
029: stmt = conn.createStatement();
030: stmt.execute("DROP TABLE " + name);
031: stmt.close();
032: } catch (Exception e) {
033: System.err.println("Exception in dropTable : " + e);
034: //throw new RemoteException("Exception in dropTable : "+e);
035: }
036: }
037:
038: /**
039: * used in CMP2 only (table is created by JORM)
040: */
041: public void initTable(int x, boolean irc) throws RemoteException {
042: PerfsHome home = null;
043: Context initialContext = null;
044: String beanName = irc ? "Perfs3Home_" + x : "Perfs2Home_" + x;
045: Object obj = null;
046: try {
047: initialContext = new InitialContext();
048: obj = initialContext.lookup(beanName);
049: home = (PerfsHome) PortableRemoteObject.narrow(obj,
050: PerfsHome.class);
051: } catch (Exception e) {
052: System.err.println("DBBean inittable: Cannot lookup "
053: + beanName + ": " + e);
054: System.err.println("JNDI Object Class = "
055: + obj.getClass().getName());
056: return;
057: }
058:
059: // Create 16 entities
060: try {
061: for (int n = 1; n <= 16; n++) {
062: String s = null;
063: String value = "";
064: switch (n) {
065: case 1:
066: s = "AAAAAAAAAA";
067: break;
068: case 2:
069: s = "BBBBBBBBBB";
070: break;
071: case 3:
072: s = "CCCCCCCCCC";
073: break;
074: case 4:
075: s = "DDDDDDDDDD";
076: break;
077: case 5:
078: s = "EEEEEEEEEE";
079: break;
080: case 6:
081: s = "FFFFFFFFFF";
082: break;
083: case 7:
084: s = "GGGGGGGGGG";
085: break;
086: case 8:
087: s = "HHHHHHHHHH";
088: break;
089: case 9:
090: s = "IIIIIIIIII";
091: break;
092: case 10:
093: s = "JJJJJJJJJJ";
094: break;
095: case 11:
096: s = "KKKKKKKKKK";
097: break;
098: case 12:
099: s = "LLLLLLLLLL";
100: break;
101: case 13:
102: s = "MMMMMMMMMM";
103: break;
104: case 14:
105: s = "NNNNNNNNNN";
106: break;
107: case 15:
108: s = "OOOOOOOOOO";
109: break;
110: case 16:
111: s = "PPPPPPPPPP";
112: break;
113: }
114: for (int i = 0; i < 5 * n; i++) {
115: value = value.concat(s);
116: }
117: home.create(n, value);
118: }
119: } catch (Exception e) {
120: System.err.println("Exception in initTable : " + e);
121: throw new RemoteException("Exception in initTable : " + e);
122: }
123: }
124:
125: /**
126: * used in CMP1 only
127: */
128: public void createTable(String name) throws RemoteException {
129: // Drop the Table
130: dropTable(name);
131: // Create the table
132: // Insert rows
133: try {
134: stmt = conn.createStatement();
135: stmt.execute("CREATE TABLE " + name + "("
136: + "v_code INTEGER NOT NULL PRIMARY KEY,"
137: + "v_value VARCHAR(1024) )");
138: stmt.execute("INSERT INTO " + name + " VALUES(1, 'A')");
139: stmt.execute("INSERT INTO " + name + " VALUES(2, 'B')");
140: stmt.execute("INSERT INTO " + name + " VALUES(3, 'C')");
141: stmt.execute("INSERT INTO " + name + " VALUES(4, 'D')");
142: stmt.execute("INSERT INTO " + name + " VALUES(5, 'E')");
143: stmt.execute("INSERT INTO " + name + " VALUES(6, 'F')");
144: stmt.execute("INSERT INTO " + name + " VALUES(7, 'G')");
145: stmt.execute("INSERT INTO " + name + " VALUES(8, 'H')");
146: stmt.execute("INSERT INTO " + name + " VALUES(9, 'I')");
147: stmt.execute("INSERT INTO " + name + " VALUES(10, 'J')");
148: stmt.execute("INSERT INTO " + name + " VALUES(11, 'K')");
149: stmt.execute("INSERT INTO " + name + " VALUES(12, 'L')");
150: stmt.execute("INSERT INTO " + name + " VALUES(13, 'M')");
151: stmt.execute("INSERT INTO " + name + " VALUES(14, 'N')");
152: stmt.execute("INSERT INTO " + name + " VALUES(15, 'O')");
153: stmt.execute("INSERT INTO " + name + " VALUES(16, 'P')");
154: stmt.close();
155: } catch (Exception e) {
156: System.err.println("Exception in createTable (1er part) : "
157: + e);
158: throw new RemoteException(
159: "Exception in createTable (1er part): " + e);
160: }
161:
162: // Init rows with good values
163: try {
164: for (int n = 1; n <= 16; n++) {
165: String s;
166: String value = "";
167: if (n == 1) {
168: s = "AAAAAAAAAA";
169: } else if (n == 2) {
170: s = "BBBBBBBBBB";
171: } else if (n == 3) {
172: s = "CCCCCCCCCC";
173: } else if (n == 4) {
174: s = "DDDDDDDDDD";
175: } else if (n == 5) {
176: s = "EEEEEEEEEE";
177: } else if (n == 6) {
178: s = "FFFFFFFFFF";
179: } else if (n == 7) {
180: s = "GGGGGGGGGG";
181: } else if (n == 8) {
182: s = "HHHHHHHHHH";
183: } else if (n == 9) {
184: s = "IIIIIIIIII";
185: } else if (n == 10) {
186: s = "JJJJJJJJJJ";
187: } else if (n == 11) {
188: s = "KKKKKKKKKK";
189: } else if (n == 12) {
190: s = "LLLLLLLLLL";
191: } else if (n == 13) {
192: s = "MMMMMMMMMM";
193: } else if (n == 14) {
194: s = "NNNNNNNNNN";
195: } else if (n == 15) {
196: s = "OOOOOOOOOO";
197: } else if (n == 16) {
198: s = "PPPPPPPPPP";
199: } else {
200: s = "..........";
201: }
202: for (int i = 0; i < 5 * n; i++) {
203: value = value.concat(s);
204: }
205: pStmt = conn.prepareStatement("update " + name
206: + " set v_value=? where v_code=?");
207: pStmt.setString(1, value);
208: pStmt.setInt(2, n);
209: pStmt.executeUpdate();
210: pStmt.close();
211: }
212: conn.close();
213: } catch (Exception e) {
214: System.err.println("Exception in createTable (2nd part): "
215: + e);
216: throw new RemoteException(
217: "Exception in createTable (2nd part): " + e);
218: }
219: System.out.println("Table " + name + " created");
220: }
221:
222: public void ejbCreate() {
223: }
224:
225: public void ejbRemove() {
226: }
227:
228: public void ejbPassivate() {
229: }
230:
231: public void ejbActivate() {
232: }
233:
234: public void setSessionContext(SessionContext ctx) {
235: sc = ctx;
236: // Finds DataSource from JNDI
237: if (dataSource == null) {
238: try {
239: InitialContext ictx = new InitialContext();
240: dataSource = (DataSource) PortableRemoteObject.narrow(
241: ictx.lookup("java:comp/env/jdbc/perfs"),
242: DataSource.class);
243: } catch (Exception e) {
244: System.err
245: .println("cannot lookup java:comp/env/jdbc/perfs");
246: throw new EJBException(e);
247: }
248: }
249: }
250: }
|