AspectJ In Action
The source code is provided to ease compiling and running examples in
the "AspectJ in Action"
by Ramnivas Laddad. The sources files are structured to allow
easy experimentation. Towards this goal, each section contains all
the necessary files and modifying a file will not affect the remaining
sections.
THE SOURCE CODE AND ACCOMPANYING FILES ARE PROVIDED WITHOUT ANY
WARRANTY, WRITTEN OR IMPLIED.
Running the scripts
Each section contains a batch file (run.bat) and an Ant build file
(build.xml) that will compile and run the example. Executing either form
of the script requires you to modify the CLASSPATH and other environment
variables as described in the next section.
Note that in certain cases, compilation is supposed to produce an error
and therefore the script will not attempt to run the example. Further,
certain sections contain multiple ways to compile and run the examples, in which
case the batch file names or Ant targets are appropriated changed.
Obtaining and
installing external tools and packages
Setting up the database table
Running the examples in chapter 7 and chapter 11 requires the use of a
database. Here are general instruction to set up the example database
for MySQL and MS Access. You should be able to use a similar process for
other databases.
Chapter 7 setup
The examples in chapter 7 simply prints the content of a database table
and as such you may use any new or existing database. The instructions here creates a table that match the one in
the book
MySQL
- Create a new database named "stock" using mysqladmin tool.
> mysqladmin create stock
- Create and populate a table in the "stock" database by issuing the
following commands
> mysql stock
mysql> create table price (symbol VARCHAR(8), price FLOAT);
mysql> insert into price VALUES ('SUNW', 5.0);
mysql> insert into price VALUES ('IBM', 85.0);
mysql> flush table price;
mysql> quit
- Modify Test.java to use JDBC driver, JDBC URL, user name, and
password that match your system
The Test.main() method should resemble the following:
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
printTable("jdbc:mysql://localhost/stock", "price",
"user1", "password1");
printTable("jdbc:mysql://localhost/stock", "price",
"user2", "password2");
printTable("jdbc:mysql://localhost/stock", "price",
"user1", "password1");
printTable("jdbc:mysql://localhost/stock", "price",
"user2", "password2");
}
- Modify CLASSPATH to include MySQL JDBC driver
Add jar file containing a MySQL JDBC driver (for example,
mysql-connector-java-3.0.8-stable-bin.jar) to your classpath.
- Compile and run the program.
Microsoft Access
- Create a new database named "stock"
- Open MS Access and using File/New menu and selecting "Database"
from the options offered.
- Specify a file name where you want to store the database file
such as stock.mdb
- Create database table using "Design View"
- Create and populate table in "stock" database
- In the design view, enter two fields: symbol with datatype
"Text" and price with datatype "Number"
- Save. When offered to specify table name, enter "price". Ignore
the message about primary key (choose "No").
- Choose "price" table to enter data. Enter the following
data
- Make the database available as ODBC Data Source
- Open "Control Panel" and double-click on "Data Source (ODBC)".
- Click on "Add". From the options available, choose "Microsoft
Access Driver (*.mdb)"
- Type "stock" as "Data Source Name" and choose the file that you
have saved database to using "Select..."
- Compile and run the test program
Chapter 11 setup
MySQL
- Create a new database named "bank" using mysqladmin tool.
> mysqladmin create bank
- Create and populate table in the "bank" database by issuing the
following commands
> mysql bank
mysql> create table accounts (accountNumber INTEGER, balance FLOAT);
mysql> insert into accounts VALUES (1, 0);
mysql> insert into accounts VALUES (2, 0);
mysql> insert into accounts VALUES (3, 0);
mysql> flush table accounts;
mysql> quit
- Modify DatabaseHelper.java to use JDBC driver, JDBC URL, user
name, and password that match your system
The DatabaseHelper.java should resemble the following:
package banking;
import java.sql.*;
public class DatabaseHelper {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception ex) {
// ignore...
}
}
public static Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost/bank";
String user = "user1";
String password = "password1";
Connection connection =
DriverManager.getConnection(url, user, password);
connection.setAutoCommit(true);
return connection;
}
}
- Modify CLASSPATH to include MySQL JDBC driver
Add jar file containing a MySQL JDBC driver (for example,
mysql-connector-java-3.0.8-stable-bin.jar) to your classpath.
- Compile and run the program.
Microsoft Access
- Create a new database named "bank"
- Open MS Access and using File/New menu and selecting "Database"
from the options offered.
- Specify a file name where you want to store the database file
such as bank.mdb
- Create database table using "Design View"
- Create and populate table in "bank" database
- In the design view, enter two fields: accountNumber with
datatype "Number" and balance with datatype "Number"
- Save. When offered to specify table name, enter "accounts".
Ignore the message about primary key (choose "No").
- Choose "account" table to enter data. Enter the following
data
- Make the database available as ODBC Data Source
- Open "Control Panel" and double-click on "Data Source (ODBC)".
- Click on "Add". From the options available, choose "Microsoft
Access Driver (*.mdb)"
- Type "bank" as "Data Source Name" and choose the file that you
have saved database to using "Select..."
- Compile and run the test program.