SQL Script for Creating the User Store on Cloudscape Databases -- USER_TYPES table CREATE TABLE USER_TYPES USER_TYPE_ID INT PRIMARY KEY NOT NULL, USER_TYPE VARCHAR20 NOT NULL ; -- OF
Trang 110.2.1 Web Applications
A web application, in the context of this book, is meant to refer to a J2EE-centric application programming model This means that servlets are used for application logic, and either provide presentation on their own or defer to some other presentation technology Popular options in this area are JavaServer Pages (JSP), frameworks like Apache Turbine ( http://jakarta.apache.org/turbine ), and Apache Cocoon ( http://xml.apache.org/cocoon ) While these are just a few of many examples for handling content and presentation, they all build on the J2EE core APIs (usually servlets), and well-understood Java and XML APIs like SAX, DOM, and JDOM
If you don't know what type of front-end you want to provide to the end user, this is almost certainly the best choice It is the most common, and as a result you can easily find resources
on the relevant technologies Books like Java Servlet Programming and JavaServer Pages
(both from O'Reilly) provide good introductions to these APIs, and will get you quickly up and running Additionally, building web services and other front-ends can most easily be done upon an existing web application And, obviously, a web application provides access through any standard web browser, as well as providing easy inroads into web-enabled phones, PDAs, and other mobile devices
If you do decide to move into the web application world, you should employ the same principles endorsed in this book Start with a layer for application logic, using servlets Ensure that any content is handled independently of presentation details, so the same data can be shown to an HTML browser, an XML viewer, or a WML phone; this is in the same vein as the clean separation of data and business logic detailed in this book From there, move into presentation technologies, and try not to code in such a way that only specifically formatted content is accepted In other words, developing an engine for converting data into presentation
is a better idea than simply taking very specific data and creating a very specific screen for
viewing I will focus on these very ideas in Volume II of this series, Web Applications
10.2.2 Web Services
Coming in a close second to the traditional web application is the web services paradigm I should start by saying that web services are still young enough that any predictions here or anywhere else are just that: predictions Don't depend on them, as things could easily change a year, a month, or even a week from now That said, it does appear that web services are going
Trang 2of semi-standards like WSDL and full-blown standards like UDDI and SOAP offer a lot of promise to a higher degree of application interoperation than what is currently available
If you do want to web-service-enable your application, you should probably start by adding some application logic on top of what was discussed in this book Remember, the more context you can add to your data, the more useful it becomes to other companies A book is more useful than a collection of titles and ISBN numbers, and a "library" (in some object form) is generally more useful than just a book Although it is certainly possible to expose EJBs directly as web services, it is generally a better idea to add some layer of functionality
on top of these beans
You should also begin to dig into the various web services specifications, like SOAP and
UDDI You may also want to check out related books, like Programming Web Services with
SOAP (O'Reilly), a language-independent look at web applications, and Java & XML
(O'Reilly), which covers SOAP, WSDL, and UDDI for Java specifically You can also find example code online at locations like http://xml.apache.org/soap Many application server vendors, like BEA Weblogic and Lutris Enhydra, also offer web services "packs" or add-ons
to their server products, which provide tools to aid in conversion from beans and Java classes
to web services I won't cover web services in detail until Volume III of this series, in order to let some of the glitter shake off the paradigm in favor of stability and proven techniques
10.3 What's Next?
Despite all of the information covered, it can still be confusing to decide what to do next With over a thousand lines of code in this book, you still do not have a complete application And while I hope you pick up Volume II of this series when it becomes available, I certainly wouldn't advise you not to press on in the interim So, in closing, I want to provide some suggestions on how to proceed in your enterprise application programming
First, take some time to understand the supplemental code in Appendix E While I didn't cover all of this in detail in the various chapters, there is quite a bit of information stuffed in between curly braces and brackets The code (particularly when downloaded from
http://www.newinstance.com/ ) is loaded with comments and Javadoc, and illustrates some concepts in addition to those explicitly covered in the text of the book You can also gain a good bit of insight about container-managed persistence in EJB 2.0, the nuts and bolts of the Java Message Service and message-driven beans, and more I've included all of this code in printed form in Appendix E , so take advantage of the listings
Next, try to find something at your job to apply these concepts to Your own assignments in the enterprise application space should allow you a test bed for the concepts mentioned here and for techniques of your own My ideas all stem from actual problems I've had to solve; you should assemble your own toolkit of similar ideas and programming idioms In other words, practice makes perfect, and you need to do more than simply read through this book to master application programming in Java
Finally, don't wait on a book to start building out your application Develop a servlet end, code up some JSPs, or delve into web services, using either the Forethought backbone or
front-an application of your own This will put you ahead of those who won't venture into new territory without a roadmap, and you may find yourself teaching them before long Future
Trang 3184
volumes of this series, or other books, may cause you to make changes down the line; however, you will have a sound understanding of what led you to these changes, and that experience is invaluable Most of all, enjoy yourself, and I'll see you online
Trang 4Appendix A SQL Scripts
This appendix contains the SQL scripts that are specific to a variety of different databases In addition to adding enhancements that will improve performance on a specific database, these scripts omit any constructs that are not supported by a specific database (for example, the InstantDB script does not employ foreign keys, a feature still under development) If you are having trouble with the standard SQL scripts provided in Chapter 3 or Chapter 5 , check to see if a script for your specific database is included here.[A] All of the scripts shown here are available for download online by visiting http://www.newinstance.com/
Additionally, many of these scripts contain information that is not SQL, but instead is an instruction set for the database For example, the Cloudscape database needs connection information specified at the head of any SQL script run against it, and InstantDB needs information about the JDBC driver to use For each of the databases covered here, specific deployment details are covered in Appendix B Any additional information specified in the examples in this appendix is also used in Appendix B as part of deployment
Finally, the scripts used to create the accounts storage assume that the scripts to create the user store have already been run In other words, if you are using Cloudscape, you must execute the script in Example A-1 before executing the one in Example A-6 This is because the foreign key constraint on the USER_ID in the ACCOUNTS table references a column that must already exist from the USERS table Errors will result if the user store has not already been created Again, following the steps outlined in Appendix B will ensure these problems
do not arise
A If your database is not covered here, please feel free to send a working SQL script (for both the users and accounts storage) to me directly at
brett@newInstance.com If I can ensure that it works, I will be happy to include it online and in updated versions of this book I welcome people
Trang 5186
A.1 The User Store
The SQL scripts in this section duplicate the functionality of the script shown in Example 3-1 , and create the storage for user information
A.1.1 Cloudscape
Example A-1 is a version of the SQL script that works on the Cloudscape Java database
Example A-1 SQL Script for Creating the User Store on Cloudscape Databases
USER_TYPES table
CREATE TABLE USER_TYPES (
USER_TYPE_ID INT PRIMARY KEY NOT NULL,
USER_TYPE VARCHAR(20) NOT NULL
);
OFFICES table
CREATE TABLE OFFICES (
OFFICE_ID INT PRIMARY KEY NOT NULL,
CITY VARCHAR(20) NOT NULL,
STATE CHAR(2) NOT NULL
);
USERS table
CREATE TABLE USERS (
USER_ID INT PRIMARY KEY NOT NULL,
OFFICE_ID INT NOT NULL,
USER_DN VARCHAR(100) NOT NULL,
USER_TYPE_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
CONSTRAINT OFFICE_ID_FK FOREIGN KEY (OFFICE_ID)
REFERENCES OFFICES (OFFICE_ID),
CONSTRAINT USER_TYPE_ID_FK FOREIGN KEY (USER_TYPE_ID)
REFERENCES USER_TYPES (USER_TYPE_ID)
);
Trang 6A.1.2 InstantDB
Example A-2 is a version of the SQL script that creates the user store for an InstantDB database, and in particular work on the version of InstantDB that ships with Lutris EAS, Version 4.1
Example A-2 SQL Script for Creating the User Store on InstantDB Databases
; Load InstantDB JDBC drivers
d org.enhydra.instantdb.jdbc.idbDriver;
o jdbc:idb=forethought.prp;
; USER_TYPES table
e CREATE TABLE USER_TYPES (
USER_TYPE_ID INT PRIMARY KEY NOT NULL,
USER_TYPE VARCHAR(20) NOT NULL
);
; OFFICES table
e CREATE TABLE OFFICES (
OFFICE_ID INT PRIMARY KEY NOT NULL,
CITY VARCHAR(20) NOT NULL,
STATE CHAR(2) NOT NULL
);
; USERS table
e CREATE TABLE USERS (
USER_ID INT PRIMARY KEY NOT NULL,
OFFICE_ID INT NOT NULL,
USER_DN VARCHAR(100) NOT NULL,
USER_TYPE_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL
);
; Close up
c close;
Trang 7188
A.1.3 MySQL
Example A-3 is a version of the SQL script that creates the user store on a MySQL database
Example A-3 SQL Script for Creating the User Store on MySQL Databases
USER_TYPES table
CREATE TABLE USER_TYPES (
USER_TYPE_ID INT PRIMARY KEY NOT NULL,
USER_TYPE VARCHAR(20) NOT NULL
);
OFFICES table
CREATE TABLE OFFICES (
OFFICE_ID INT PRIMARY KEY NOT NULL,
CITY VARCHAR(20) NOT NULL,
STATE CHAR(2) NOT NULL
);
USERS table
CREATE TABLE USERS (
USER_ID INT PRIMARY KEY NOT NULL,
OFFICE_ID INT NOT NULL,
USER_DN VARCHAR(100) NOT NULL,
USER_TYPE_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
CONSTRAINT OFFICE_ID_FK FOREIGN KEY (OFFICE_ID)
REFERENCES OFFICES (OFFICE_ID),
CONSTRAINT USER_TYPE_ID_FK FOREIGN KEY (USER_TYPE_ID)
REFERENCES USER_TYPES (USER_TYPE_ID)
);
Trang 8CREATE TABLE USER_TYPES (
USER_TYPE_ID INTEGER PRIMARY KEY NOT NULL,
USER_TYPE VARCHAR2(20) NOT NULL
);
OFFICES table
CREATE TABLE OFFICES (
OFFICE_ID INTEGER PRIMARY KEY NOT NULL,
CITY VARCHAR2(20) NOT NULL,
STATE CHAR(2) NOT NULL
);
USERS table
CREATE TABLE USERS (
USER_ID INTEGER PRIMARY KEY NOT NULL,
OFFICE_ID INTEGER NOT NULL,
USER_DN VARCHAR2(100) NOT NULL,
USER_TYPE_ID INTEGER NOT NULL,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(30) NOT NULL,
CONSTRAINT OFFICE_ID_FK FOREIGN KEY (OFFICE_ID)
REFERENCES OFFICES (OFFICE_ID),
CONSTRAINT USER_TYPE_ID_FK FOREIGN KEY (USER_TYPE_ID)
REFERENCES USER_TYPES (USER_TYPE_ID)
);
Trang 9190
A.1.5 PostgreSQL
Example A-5 is an SQL script that creates the user store on PostgreSQL databases
Example A-5 SQL Script for Creating the User Store on PostgreSQL Databases
USER_TYPES table
CREATE TABLE USER_TYPES (
USER_TYPE_ID INT PRIMARY KEY NOT NULL,
USER_TYPE VARCHAR(20) NOT NULL
);
OFFICES table
CREATE TABLE OFFICES (
OFFICE_ID INT PRIMARY KEY NOT NULL,
CITY VARCHAR(20) NOT NULL,
STATE CHAR(2) NOT NULL
);
USERS table
CREATE TABLE USERS (
USER_ID INT PRIMARY KEY NOT NULL,
OFFICE_ID INT NOT NULL,
USER_DN VARCHAR(100) NOT NULL,
USER_TYPE_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
CONSTRAINT OFFICE_ID_FK FOREIGN KEY (OFFICE_ID)
REFERENCES OFFICES (OFFICE_ID),
CONSTRAINT USER_TYPE_ID_FK FOREIGN KEY (USER_TYPE_ID)
REFERENCES USER_TYPES (USER_TYPE_ID)
);
Trang 10A.2 The Accounts Store
The SQL scripts in this section handle the creation of the accounts store under different databases, and are equivalent to the script shown in Example 3-3 of the text
CREATE TABLE ACCOUNT_TYPES (
ACCOUNT_TYPE_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_TYPE VARCHAR(20) NOT NULL
);
ACCOUNTS table
CREATE TABLE ACCOUNTS (
ACCOUNT_ID INT PRIMARY KEY NOT NULL,
USER_ID INT NOT NULL,
ACCOUNT_TYPE_ID INT NOT NULL,
BALANCE FLOAT NOT NULL,
CONSTRAINT USER_ID_FK FOREIGN KEY (USER_ID)
REFERENCES USERS (USER_ID),
CONSTRAINT ACCOUNT_TYPE_ID_FK FOREIGN KEY (ACCOUNT_TYPE_ID)
REFERENCES ACCOUNT_TYPES (ACCOUNT_TYPE_ID)
);
TRANSACTIONS table
CREATE TABLE TRANSACTIONS (
TRANSACTION_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_ID INT NOT NULL,
AMOUNT FLOAT NOT NULL,
DATE_TIME DATE NOT NULL,
CONSTRAINT ACCOUNT_ID_FK FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);
FUNDS table
CREATE TABLE FUNDS (
FUND_ID INT PRIMARY KEY NOT NULL,
NAME VARCHAR(20) NOT NULL,
DESCRIPTION VARCHAR(200)
);
INVESTMENTS table
CREATE TABLE INVESTMENTS (
INVESTMENT_ID INT PRIMARY KEY NOT NULL,
FUND_ID INT NOT NULL,
ACCOUNT_ID INT NOT NULL,
INITIAL_AMOUNT FLOAT NOT NULL,
YIELD FLOAT,
CONSTRAINT FUND_ID_FK FOREIGN KEY (FUND_ID)
REFERENCES FUNDS (FUND_ID),
CONSTRAINT ACCOUNT_ID_FK2 FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);
Trang 11192
A.2.2 InstantDB
Example A-7 is an SQL script that creates the accounts store for InstantDB databases
Example A-7 SQL Script for Creating the Accounts Store on InstantDB Databases
; Load InstantDB JDBC drivers
d org.enhydra.instantdb.jdbc.idbDriver;
o jdbc:idb=forethought.prp;
; ACCOUNT_TYPES table
e CREATE TABLE ACCOUNT_TYPES (
ACCOUNT_TYPE_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_TYPE VARCHAR(20) NOT NULL
);
; ACCOUNTS table
e CREATE TABLE ACCOUNTS (
ACCOUNT_ID INT PRIMARY KEY NOT NULL,
USER_ID INT NOT NULL,
ACCOUNT_TYPE_ID INT NOT NULL,
BALANCE FLOAT NOT NULL
);
; TRANSACTIONS table
e CREATE TABLE TRANSACTIONS (
TRANSACTION_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_ID INT NOT NULL,
AMOUNT FLOAT NOT NULL,
DATE_TIME DATE NOT NULL
);
; FUNDS table
e CREATE TABLE FUNDS (
FUND_ID INT PRIMARY KEY NOT NULL,
NAME VARCHAR(20) NOT NULL,
DESCRIPTION VARCHAR(200)
);
; INVESTMENTS table
CREATE TABLE INVESTMENTS (
INVESTMENT_ID INT PRIMARY KEY NOT NULL,
FUND_ID INT NOT NULL,
ACCOUNT_ID INT NOT NULL,
INITIAL_AMOUNT FLOAT NOT NULL,
YIELD FLOAT
);
; Close up
c close;
Trang 12A.2.3 MySQL
Example A-8 is an SQL script that creates the accounts store on MySQL databases
Example A-8 SQL Script for Creating the Accounts Store on MySQL Databases
ACCOUNT_TYPES table
CREATE TABLE ACCOUNT_TYPES (
ACCOUNT_TYPE_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_TYPE VARCHAR(20) NOT NULL
);
ACCOUNTS table
CREATE TABLE ACCOUNTS (
ACCOUNT_ID INT PRIMARY KEY NOT NULL,
USER_ID INT NOT NULL,
ACCOUNT_TYPE_ID INT NOT NULL,
BALANCE FLOAT NOT NULL,
CONSTRAINT USER_ID_FK FOREIGN KEY (USER_ID)
REFERENCES USERS (USER_ID),
CONSTRAINT ACCOUNT_TYPE_ID_FK FOREIGN KEY (ACCOUNT_TYPE_ID)
REFERENCES ACCOUNT_TYPES (ACCOUNT_TYPE_ID)
);
TRANSACTIONS table
CREATE TABLE TRANSACTIONS (
TRANSACTION_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_ID INT NOT NULL,
AMOUNT FLOAT NOT NULL,
DATE_TIME DATE NOT NULL,
CONSTRAINT ACCOUNT_ID_FK FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);
FUNDS table
CREATE TABLE FUNDS (
FUND_ID INT PRIMARY KEY NOT NULL,
NAME VARCHAR(20) NOT NULL,
DESCRIPTION VARCHAR(200)
);
INVESTMENTS table
CREATE TABLE INVESTMENTS (
INVESTMENT_ID INT PRIMARY KEY NOT NULL,
FUND_ID INT NOT NULL,
ACCOUNT_ID INT NOT NULL,
INITIAL_AMOUNT FLOAT NOT NULL,
YIELD FLOAT,
CONSTRAINT FUND_ID_FK FOREIGN KEY (FUND_ID)
REFERENCES FUNDS (FUND_ID),
CONSTRAINT ACCOUNT_ID_FK2 FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);
Trang 13CREATE TABLE ACCOUNT_TYPES (
ACCOUNT_TYPE_ID INTEGER PRIMARY KEY NOT NULL,
ACCOUNT_TYPE VARCHAR2(20) NOT NULL
);
ACCOUNTS table
CREATE TABLE ACCOUNTS (
ACCOUNT_ID INTEGER PRIMARY KEY NOT NULL,
USER_ID INTEGER NOT NULL,
ACCOUNT_TYPE_ID INTEGER NOT NULL,
BALANCE FLOAT NOT NULL,
CONSTRAINT USER_ID_FK FOREIGN KEY (USER_ID)
REFERENCES USERS (USER_ID),
CONSTRAINT ACCOUNT_TYPE_ID_FK FOREIGN KEY (ACCOUNT_TYPE_ID)
REFERENCES ACCOUNT_TYPES (ACCOUNT_TYPE_ID)
);
TRANSACTIONS table
CREATE TABLE TRANSACTIONS (
TRANSACTION_ID INTEGER PRIMARY KEY NOT NULL,
ACCOUNT_ID INTEGER NOT NULL,
AMOUNT FLOAT NOT NULL,
DATE_TIME DATE NOT NULL,
CONSTRAINT ACCOUNT_ID_FK FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);
FUNDS table
CREATE TABLE FUNDS (
FUND_ID INTEGER PRIMARY KEY NOT NULL,
NAME VARCHAR2(20) NOT NULL,
DESCRIPTION VARCHAR2(200)
);
INVESTMENTS table
CREATE TABLE INVESTMENTS (
INVESTMENT_ID INTEGER PRIMARY KEY NOT NULL,
FUND_ID INTEGER NOT NULL,
ACCOUNT_ID INTEGER NOT NULL,
INITIAL_AMOUNT FLOAT NOT NULL,
YIELD FLOAT,
CONSTRAINT FUND_ID_FK FOREIGN KEY (FUND_ID)
REFERENCES FUNDS (FUND_ID),
CONSTRAINT ACCOUNT_ID_FK2 FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);
Trang 14A.2.5 PostgreSQL
Example A-10 is an SQL script that creates the accounts store on PostgreSQL databases
Example A-10 SQL Script for Creating the Accounts Store on PostgreSQL Databases
ACCOUNT_TYPES table
CREATE TABLE ACCOUNT_TYPES (
ACCOUNT_TYPE_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_TYPE VARCHAR(20) NOT NULL
);
ACCOUNTS table
CREATE TABLE ACCOUNTS (
ACCOUNT_ID INT PRIMARY KEY NOT NULL,
USER_ID INT NOT NULL,
ACCOUNT_TYPE_ID INT NOT NULL,
BALANCE FLOAT NOT NULL,
CONSTRAINT USER_ID_FK FOREIGN KEY (USER_ID)
REFERENCES USERS (USER_ID),
CONSTRAINT ACCOUNT_TYPE_ID_FK FOREIGN KEY (ACCOUNT_TYPE_ID)
REFERENCES ACCOUNT_TYPES (ACCOUNT_TYPE_ID)
);
TRANSACTIONS table
CREATE TABLE TRANSACTIONS (
TRANSACTION_ID INT PRIMARY KEY NOT NULL,
ACCOUNT_ID INT NOT NULL,
AMOUNT FLOAT NOT NULL,
DATE_TIME DATE NOT NULL,
CONSTRAINT ACCOUNT_ID_FK FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);
FUNDS table
CREATE TABLE FUNDS (
FUND_ID INT PRIMARY KEY NOT NULL,
NAME VARCHAR(20) NOT NULL,
DESCRIPTION VARCHAR(200)
);
INVESTMENTS table
CREATE TABLE INVESTMENTS (
INVESTMENT_ID INT PRIMARY KEY NOT NULL,
FUND_ID INT NOT NULL,
ACCOUNT_ID INT NOT NULL,
INITIAL_AMOUNT FLOAT NOT NULL,
YIELD FLOAT,
CONSTRAINT FUND_ID_FK FOREIGN KEY (FUND_ID)
REFERENCES FUNDS (FUND_ID),
CONSTRAINT ACCOUNT_ID_FK2 FOREIGN KEY (ACCOUNT_ID)
REFERENCES ACCOUNTS (ACCOUNT_ID)
);