JDBC Demo project

Install Maven

  • Unzip the directory maven-2.0.7 to c:\tools.
  • Create a new environment variable M2_HOME with the value c:\tools\maven-2.0.4
  • Add a new directory to the Path system variable, namely the %M2_HOME%\bin directory.
  • In a DOS Window run "mvn -version"
  • Copy the file settings.xml to D:\Documents and Settings\training\.m2

    This demo illustrates how to build and test database access, which is based on JDBC, and uses minimal yet reasonable amount of supplementary services (connection pool, configurable connection params, simple transactions) to make it realistic.

Preparing the Eclipse project

  1. The Subversion location of this code: https://java-eim.googlecode.com/svn/trunk/java-eim-demo-jdbc Check out this as a separate Eclipse project.
  2. Install Maven and define M2_REPO variable to indicate the directory, where Maven is installed (e.g. c:\tools\maven-2.0.7 ).
  3. Comment out the fragment parent.../parent from the pom.xml file in the project's root directory.
  4. Run the following command:
    mvn eclipse:eclipse
    

    (This generates the .project and .classpath files)

  5. Refresh the "java-eim-demo-jdbc" project. The compilation errors should disappear.

Sequence of actions for Oracle XE

Open the Oracle SQL console. Type the following commands:

SQL> CONNECT system/Student007;
SQL> CREATE USER sampleuser IDENTIFIED BY samplepassword;
SQL> GRANT CONNECT, RESOURCE TO sampleuser;
SQL> DISCONNECT;

  Exit the SQL console (type "exit"); open another console. 
  Enter the following commands:

SQL> CONNECT sampleuser/samplepassword;  

SQL> @D:\workspace\java-eim-demo-jdbc\src\main\resources\init.sql

Run BookmarkDAOTest as JUnit test from Eclipse and see that it is green. Comment out "@After" annotation for "tearDown()" method and run the tests again (this time they do not clean the database after each testcase).

Issue the following command in the SQL console:

SQL> SELECT title FROM bookmark;

You should see two records.

Appendix: Sequence of actions for MySQL

In a DOS console connect to a MySQL database as a root:

C:\> mysql -u root -pStudent007

After that, execute these MySQL commands:

create database jdbc_sample;
create user 'sampleuser'@'localhost' identified by 'samplepassword';
grant all on jdbc_sample.* to 'sampleuser'@'localhost';
flush privileges;
exit

Exit the session as "root". Log in as "sampleuser":

C:\> mysql  -u sampleuser -psamplepassword 

USE jdbc_sample;
CREATE TABLE bookmark (
    bookmark_id BIGINT NOT NULL AUTO_INCREMENT, 
    url VARCHAR(255) NOT NULL UNIQUE, 
    title VARCHAR(50) NOT NULL, 
    last_modified DATETIME,
    content MEDIUMTEXT, 
    checksum BIGINT, 
    description VARCHAR(255), 
    PRIMARY KEY(bookmark_id));