JDBC Environment Setup

« Previous Chapter Next Chapter »

To start developing with JDBC setup your JDBC environment by following the steps shown below. We assume that you are working on a Windows platform.

Install Java:

Install J2SE Development Kit from Java Official Site.

Make sure following environment variables are set as described below:

  • JAVA_HOME: This environment variable should point to the directory where you installed the JDK, e.g. C:\Program Files\Java\jdk1.8.0_25

  • CLASSPATH: This environment variable should has appropriate paths set, e.g. C:\Program Files\Java\jdk1.8.0_25\jre\lib

  • PATH: This environment variable should point to appropriate JRE bin, e.g. C:\Program Files\Java\jdk1.8.0_25\bin.

Just Check these variables are set:

  • Go to the control panel and double-click on System. For Windows XP users goto Performance and Maintenance before you will see the System icon.

  • Go to the Advanced tab and click on Environment Variables.

Install Database:

The most important thing you will need, of course is an actual running database with a table that you can query and modify.

Install a database that is most suitable for you. You can have plenty of choices and most common are:

  1. MySQL DB: MySQL is an open source database. You can download it from MySQL Official Site. We recommend downloading the full Windows installation.

    In addition, download and install MySQL Administrator as well as MySQL Query Browser. These are GUI based tools that will make your development much easier.

    Finally, download and unzip MySQL Connector/J (the MySQL JDBC driver) in a convenient directory. Lets assume that you have installed the driver at C:\Program Files\MySQL\mysql-connector-java-5.1.38.

    Accordingly set CLASSPATH variable to C:\Program Files\MySQL\mysql-connector-java-5.1.38\mysql-connector-java-5.1.38-bin.jar. Your driver version may vary based on your installation.

  2. PostgreSQL DB: PostgreSQL is an open source database. You can download it from PostgreSQL Official Site.

    The Postgres installation contains a GUI based administrative tool called pgAdmin III. JDBC drivers are also included as part of the installation.

  3. Oracle DB: Oracle DB is an commercial database sold by Oracle . We assume that you have the necessary distribution media to install it.

    Oracle installation includes a GUI based administrative tool called Enterprise Manager. JDBC drivers are also included as part of the installation.

Install Database Drivers:

The latest JDK includes a JDBC-ODBC Bridge driver that makes most Open Database Connectivity (ODBC) drivers available to programmers using the JDBC API.

Now a days most of the Database vendors are supplying appropriate JDBC drivers along with Database installation. So you should not worry about this part.

Set Database Credential:

In this Chapter we are going to use MySQL database. When you install any of the above database, its administrator ID is set to root and gives provision to set a password of your choice.

Using root ID and password you can either create another users ID and password or you can use root ID and password for your JDBC application.

There are various database operations like database creation and deletion, which would need administrator ID and password.

For rest of JDBC tutorial we would use MySQL Database with username as ID and password as password.

If you do'nt have sufficient privilege to create new users then you can ask your Database Administrator (DBA) to create a user ID and password for you.

Create Database:

To create the EMP database, use the following steps:

Step 1:

Open a Command Prompt and change to the installation directory as follows:

Example

C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>

Note: The path to mysqld.exe may vary depending on the install location of MySQL on your system. You can also check documentation on how to start and stop your database server.

Step 2:

Start the database server by executing the following command, if it is already not running.

Example

C:\Program Files\MySQL\bin>mysqld
C:\Program Files\MySQL\bin>

Step 3:

Create the EMP database by executing the following command

Example

C:\Program Files\MySQL\bin> mysqladmin create EMP -u root -p
Enter password: ********
C:\Program Files\MySQL\bin>

Create Table

To create the Friends table in FRND database, use the following steps:

Step 1:

Open a Command Prompt and change to the installation directory as follows:

Example

C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>

Step 2:

Login to database as follows

Example

C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>

Step 3:

Create the table Friend as follows:

Example

mysql> use EMP;
mysql> create table Friends
    -> (
    -> id int not null,
    -> age int not null,
    -> first varchar (255),
    -> last varchar (255)
    -> );
Query OK, 0 rows affected (0.08 sec)
mysql>

Create Data Records

Finally you create few records in Friend table as follows:

Example

mysql> INSERT INTO Friends VALUES (100, 18, 'Sophia', 'Lomela');
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO Friends VALUES (101, 25, 'Katrina', 'Kane');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Friends VALUES (102, 30, 'Guru', 'Morkel');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Friends VALUES (103, 28, 'Simone', 'Washer');
Query OK, 1 row affected (0.00 sec)

mysql>

Complete Reference on MySQL database, study MySQL Tutorial.

In Next chapter Learn A sample example on JDBC Programming.


« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!