KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > applis > ejb > perfs > DBBean


1 //DBBean.java
2

3 package applis.ejb.perfs;
4
5
6 import java.rmi.RemoteException;
7 import java.sql.Connection;
8 import java.sql.PreparedStatement;
9 import java.sql.Statement;
10
11 import javax.ejb.EJBException;
12 import javax.ejb.SessionBean;
13 import javax.ejb.SessionContext;
14 import javax.naming.Context;
15 import javax.naming.InitialContext;
16 import javax.rmi.PortableRemoteObject;
17 import javax.sql.DataSource;
18
19
20 public class DBBean implements SessionBean {
21
22     SessionContext sc = null;
23     private DataSource dataSource = null;
24     Connection conn = null;
25     Statement stmt;
26     PreparedStatement pStmt;
27
28
29     void dropTable(String name) throws RemoteException{
30         try {
31             conn = dataSource.getConnection();
32             stmt = conn.createStatement();
33             stmt.execute("DROP TABLE "+name);
34             stmt.close();
35         } catch(Exception e) {
36             System.err.println("Exception in dropTable : "+e);
37             //throw new RemoteException("Exception in dropTable : "+e);
38
}
39     }
40         
41     /**
42      * used in CMP2 only (table is created by JORM)
43      */

44     public void initTable(int x, boolean irc) throws RemoteException {
45         PerfsHome home = null;
46         Context initialContext = null;
47         String beanName = irc ? "Perfs3Home_" + x : "Perfs2Home_" + x;
48         Object obj = null;
49         try {
50             initialContext = new InitialContext();
51             obj = initialContext.lookup(beanName);
52             home = (PerfsHome) PortableRemoteObject.narrow(obj, PerfsHome.class);
53         } catch (Exception e) {
54             System.err.println("DBBean inittable: Cannot lookup "+beanName+": "+e);
55             System.err.println("JNDI Object Class = "+obj.getClass().getName());
56             return;
57         }
58
59         // Create 16 entities
60
try {
61             for (int n = 1; n <= 16; n++) {
62                 String s = null;
63                 String value = "";
64                 switch (n) {
65                 case 1:
66                     s = "AAAAAAAAAA";
67                     break;
68                 case 2:
69                     s = "BBBBBBBBBB";
70                     break;
71                 case 3:
72                     s = "CCCCCCCCCC";
73                     break;
74                 case 4:
75                     s = "DDDDDDDDDD";
76                     break;
77                 case 5:
78                     s = "EEEEEEEEEE";
79                     break;
80                 case 6:
81                     s = "FFFFFFFFFF";
82                     break;
83                 case 7:
84                     s = "GGGGGGGGGG";
85                     break;
86                 case 8:
87                     s = "HHHHHHHHHH";
88                     break;
89                 case 9:
90                     s = "IIIIIIIIII";
91                     break;
92                 case 10:
93                     s = "JJJJJJJJJJ";
94                     break;
95                 case 11:
96                     s = "KKKKKKKKKK";
97                     break;
98                 case 12:
99                     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) : "+e);
157             throw new RemoteException("Exception in createTable (1er part): "+e);
158         }
159
160         // Init rows with good values
161
try {
162             for (int n=1; n<=16; n++) {
163                 String s;
164                 String value = "";
165                 if (n==1) {
166                     s = "AAAAAAAAAA";
167                 } else if (n==2) {
168                     s = "BBBBBBBBBB";
169                 } else if (n==3) {
170                     s = "CCCCCCCCCC";
171                 } else if (n==4) {
172                     s = "DDDDDDDDDD";
173                 } else if (n==5) {
174                     s = "EEEEEEEEEE";
175                 } else if (n==6) {
176                     s = "FFFFFFFFFF";
177                 } else if (n==7) {
178                     s = "GGGGGGGGGG";
179                 } else if (n==8) {
180                     s = "HHHHHHHHHH";
181                 } else if (n==9) {
182                     s = "IIIIIIIIII";
183                 } else if (n==10) {
184                     s = "JJJJJJJJJJ";
185                 } else if (n==11) {
186                     s = "KKKKKKKKKK";
187                 } else if (n==12) {
188                     s = "LLLLLLLLLL";
189                 } else if (n==13) {
190                     s = "MMMMMMMMMM";
191                 } else if (n==14) {
192                     s = "NNNNNNNNNN";
193                 } else if (n==15) {
194                     s = "OOOOOOOOOO";
195                 } else if (n==16) {
196                     s = "PPPPPPPPPP";
197                 } else {
198                     s = "..........";
199                 }
200                 for (int i=0; i<5*n; i++) {
201                     value = value.concat(s);
202                 }
203                 pStmt = conn.prepareStatement("update "+name+" set v_value=? where v_code=?");
204                 pStmt.setString(1, value);
205                 pStmt.setInt(2, n);
206                 pStmt.executeUpdate();
207                 pStmt.close();
208             }
209             conn.close();
210         } catch (Exception e) {
211             System.err.println("Exception in createTable (2nd part): "+e);
212             throw new RemoteException("Exception in createTable (2nd part): "+e);
213         }
214         System.out.println("Table "+name+" created");
215     }
216
217     public void ejbCreate() {
218     }
219
220     public void ejbRemove() {
221     }
222
223     public void ejbPassivate() {
224     }
225
226     public void ejbActivate() {
227     }
228     
229     public void setSessionContext(SessionContext ctx) {
230         sc = ctx;
231         // Finds DataSource from JNDI
232
if (dataSource == null) {
233             try {
234                 InitialContext ictx = new InitialContext();
235                 dataSource = (DataSource) PortableRemoteObject.narrow(ictx.lookup("java:comp/env/jdbc/perfs"), DataSource.class);
236             } catch (Exception e) {
237                 System.err.println("cannot lookup java:comp/env/jdbc/perfs");
238                 throw new EJBException(e);
239             }
240         }
241     }
242 }
243
Popular Tags