Source Code Cross Referenced for DBBean.java in  » J2EE » JOnAS-4.8.6 » applis » ejb » perfs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » applis.ejb.perfs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.