Source Code Cross Referenced for ExampleDatabasePut.java in  » JMX » je » persist » gettingStarted » 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 » JMX » je » persist.gettingStarted 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package persist.gettingStarted;
002:
003:        import java.io.BufferedReader;
004:        import java.io.File;
005:        import java.io.FileInputStream;
006:        import java.io.FileNotFoundException;
007:        import java.io.IOException;
008:        import java.io.InputStreamReader;
009:        import java.util.ArrayList;
010:        import java.util.List;
011:
012:        import com.sleepycat.je.DatabaseException;
013:        import com.sleepycat.je.Transaction;
014:
015:        public class ExampleDatabasePut {
016:
017:            private static File myDbEnvPath = new File("/tmp/JEDB");
018:            private static File inventoryFile = new File("./inventory.txt");
019:            private static File vendorsFile = new File("./vendors.txt");
020:
021:            private DataAccessor da;
022:
023:            // Encapsulates the environment and data store.
024:            private static MyDbEnv myDbEnv = new MyDbEnv();
025:
026:            private static void usage() {
027:                System.out.println("ExampleDatabasePut [-h <env directory>]");
028:                System.out
029:                        .println("      [-i <inventory file>] [-v <vendors file>]");
030:                System.exit(-1);
031:            }
032:
033:            public static void main(String args[]) {
034:                ExampleDatabasePut edp = new ExampleDatabasePut();
035:                try {
036:                    edp.run(args);
037:                } catch (DatabaseException dbe) {
038:                    System.err.println("ExampleDatabasePut: " + dbe.toString());
039:                    dbe.printStackTrace();
040:                    dbe.printStackTrace();
041:                } catch (Exception e) {
042:                    System.out.println("Exception: " + e.toString());
043:                    e.printStackTrace();
044:                } finally {
045:                    myDbEnv.close();
046:                }
047:                System.out.println("All done.");
048:            }
049:
050:            private void run(String args[]) throws DatabaseException {
051:                // Parse the arguments list
052:                parseArgs(args);
053:
054:                myDbEnv.setup(myDbEnvPath, // Path to the environment home 
055:                        false); // Environment read-only?
056:
057:                // Open the data accessor. This is used to store
058:                // persistent objects.
059:                da = new DataAccessor(myDbEnv.getEntityStore());
060:
061:                System.out.println("loading vendors db....");
062:                loadVendorsDb();
063:
064:                System.out.println("loading inventory db....");
065:                loadInventoryDb();
066:            }
067:
068:            private void loadVendorsDb() throws DatabaseException {
069:
070:                // loadFile opens a flat-text file that contains our data
071:                // and loads it into a list for us to work with. The integer
072:                // parameter represents the number of fields expected in the
073:                // file.
074:                List vendors = loadFile(vendorsFile, 8);
075:
076:                // Now load the data into the store.
077:                for (int i = 0; i < vendors.size(); i++) {
078:                    String[] sArray = (String[]) vendors.get(i);
079:                    Vendor theVendor = new Vendor();
080:                    theVendor.setVendorName(sArray[0]);
081:                    theVendor.setAddress(sArray[1]);
082:                    theVendor.setCity(sArray[2]);
083:                    theVendor.setState(sArray[3]);
084:                    theVendor.setZipcode(sArray[4]);
085:                    theVendor.setBusinessPhoneNumber(sArray[5]);
086:                    theVendor.setRepName(sArray[6]);
087:                    theVendor.setRepPhoneNumber(sArray[7]);
088:
089:                    // Put it in the store. Because we do not explicitly set
090:                    // a transaction here, and because the store was opened
091:                    // with transactional support, auto commit is used for each
092:                    // write to the store.
093:                    da.vendorByName.put(theVendor);
094:                }
095:            }
096:
097:            private void loadInventoryDb() throws DatabaseException {
098:
099:                // loadFile opens a flat-text file that contains our data
100:                // and loads it into a list for us to work with. The integer
101:                // parameter represents the number of fields expected in the
102:                // file.
103:                List inventoryArray = loadFile(inventoryFile, 6);
104:
105:                // Now load the data into the store. The item's sku is the
106:                // key, and the data is an Inventory class object.
107:
108:                // Start a transaction. All inventory items get loaded using a
109:                // single transaction.
110:                Transaction txn = myDbEnv.getEnv().beginTransaction(null, null);
111:
112:                for (int i = 0; i < inventoryArray.size(); i++) {
113:                    String[] sArray = (String[]) inventoryArray.get(i);
114:                    String sku = sArray[1];
115:
116:                    Inventory theInventory = new Inventory();
117:                    theInventory.setItemName(sArray[0]);
118:                    theInventory.setSku(sArray[1]);
119:                    theInventory.setVendorPrice((new Float(sArray[2]))
120:                            .floatValue());
121:                    theInventory.setVendorInventory((new Integer(sArray[3]))
122:                            .intValue());
123:                    theInventory.setCategory(sArray[4]);
124:                    theInventory.setVendor(sArray[5]);
125:
126:                    // Put it in the store. Note that this causes our secondary key
127:                    // to be automatically updated for us.
128:                    try {
129:                        da.inventoryBySku.put(txn, theInventory);
130:                    } catch (DatabaseException dbe) {
131:                        try {
132:                            System.out.println("Error putting entry "
133:                                    + sku.getBytes("UTF-8"));
134:                        } catch (IOException willNeverOccur) {
135:                        }
136:                        txn.abort();
137:                        throw dbe;
138:                    }
139:                }
140:                // Commit the transaction. The data is now safely written to the
141:                // store.
142:                txn.commit();
143:            }
144:
145:            private static void parseArgs(String args[]) {
146:                for (int i = 0; i < args.length; ++i) {
147:                    if (args[i].startsWith("-")) {
148:                        switch (args[i].charAt(1)) {
149:                        case 'h':
150:                            myDbEnvPath = new File(args[++i]);
151:                            break;
152:                        case 'i':
153:                            inventoryFile = new File(args[++i]);
154:                            break;
155:                        case 'v':
156:                            vendorsFile = new File(args[++i]);
157:                            break;
158:                        default:
159:                            usage();
160:                        }
161:                    }
162:                }
163:            }
164:
165:            private List loadFile(File theFile, int numFields) {
166:                List<String[]> records = new ArrayList<String[]>();
167:                try {
168:                    String theLine = null;
169:                    FileInputStream fis = new FileInputStream(theFile);
170:                    BufferedReader br = new BufferedReader(
171:                            new InputStreamReader(fis));
172:                    while ((theLine = br.readLine()) != null) {
173:                        String[] theLineArray = theLine.split("#");
174:                        if (theLineArray.length != numFields) {
175:                            System.out.println("Malformed line found in "
176:                                    + theFile.getPath());
177:                            System.out.println("Line was: '" + theLine);
178:                            System.out.println("length found was: "
179:                                    + theLineArray.length);
180:                            System.exit(-1);
181:                        }
182:                        records.add(theLineArray);
183:                    }
184:                    // Close the input stream handle
185:                    fis.close();
186:                } catch (FileNotFoundException e) {
187:                    System.err.println(theFile.getPath() + " does not exist.");
188:                    e.printStackTrace();
189:                    usage();
190:                } catch (IOException e) {
191:                    System.err.println("IO Exception: " + e.toString());
192:                    e.printStackTrace();
193:                    System.exit(-1);
194:                }
195:                return records;
196:            }
197:
198:            protected ExampleDatabasePut() {
199:            }
200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.