Java Reference
In-Depth Information
2.5.1
Setting up the database
For the purpose of the sample application, we will use a My
SQL
database. The i
BA-
TIS
framework works with any database, as long as it has a
JDBC
-compliant driver.
You simply need to supply a driver class name and a
JDBC
URL
in the configuration.
Setting up a database server is beyond the scope of this topic, so we will only
provide you with what you need to do on the assumption that the database is
already set up and functional. Here is the My
SQL
script that creates the table we
will use and adds some sample data to it:
#
# Table structure for table 'user'
#
CREATE TABLE USER_ACCOUNT (
USERID INT(3) NOT NULL AUTO_INCREMENT,
USERNAME VARCHAR(10) NOT NULL,
PASSSWORD VARCHAR(30) NOT NULL,
GROUPNAME VARCHAR(10),
PRIMARY KEY (USERID)
);
#
# Data for table 'user'
#
INSERT INTO USER_ACCOUNT (USERNAME, PASSSWORD, GROUPNAME)
VALUES ('LMEADORS', 'PICKLE', 'EMPLOYEE');
INSERT INTO USER_ACCOUNT (USERNAME, PASSSWORD, GROUPNAME)
VALUES ('JDOE', 'TEST', 'EMPLOYEE');
COMMIT;
If you have a different database server already set up with other data that you
would like to execute some
SQL
queries over, feel free to use it for the example.
You will need to modify the query in the
SqlMap.xml
file to have your
SQL
in it and
will also need to modify the
SqlMapConfig.xml
file to configure i
BATIS
to use your
database instead. To make it work, you have to know the driver name, the
JDBC
URL
, and a username and password to connect with.
2.5.2
Writing the code
Because this application is our first full example, and an introduction to using
i
BATIS
, the code will be much simpler than a real application would be. We dis-
cuss type safety and exception handling later, so we will not be considering those
topics here. Listing 2.4 contains the complete code.
