Install Java:
Install J2SE Development Kit 5.0 (JDK 5.0) to download: Java official website.
Make sure the following environment variables are set as follows:
JAVA_HOME: This environment variable should point to the directory where JDK is installed, for example: C:/Program Files/Java/jdk1.5.0
CLASSPATH: This environment variable should have an appropriate path set, such as: C:/Program Files/Java/jdk1.5.0_20/jre/lib
PATH: This environment variable should point to the appropriate JRE bin, such as: C:/Program Files/Java/jre1.5.0_20/bin.
There may be these variables already set, but just to make sure how to check here.
Go to the control panel and double-click on the system. If you are a Windows XP user, you may want to turn on: "Performance"="Maintenance" and you will see the system icon.
Go to the Advanced tab and click Environment Variables.
Now, select all input variables to set correctly.
The JDBC packages java.sql and javax.sql will be automatically obtained when installing J2SE Development Kit 5.0 (JDK 5.0)
Install the database:
Of course, the most important thing is to actually run the database, which can query and modify tables.
Installing a database is the best fit. There are many options available, the most common ones are:
MySQL DB: MySQL is an open source database. You can download the official MySQL website from here, and it is recommended to download the complete Windows installation.
In addition, download and install MySQL management as well as MySQL query browser. These are GUI-based tools that will make development easier.
Finally, download and unzip MySQL Connector/J (MySQL JDBC Driver) in a convenient directory. For the purpose of this tutorial, we will assume that the driver is already installed at C:/Program Files/MySQL/mysql-connector-java-5.1.8.
Set the CLASSPATH variable accordingly to C:/Program Files/MySQL/mysql-connector-java-5.1.8/mysql-connector-java-5.1.8-bin.jar. It may vary depending on the version of the driver installed.
PostgreSQL DB: PostgreSQL is an open source database. You can download the official PostgreSQL website from here.
The Postgres installation contains a GUI-based management tool called pgAdmin III. JDBC drivers are also included as part of the installation.
Oracle DB: Oracle database is a commercial database sold by Oracle. Assume that the required distribution media is installed.
Oracle installation includes a GUI-based management tool called Enterprise Manager. JDBC drivers are also included as part of the installation.
Install the database driver:
The latest JDK includes the JDBC-ODBC bridge driver, making it possible for most open database connection (ODBC) driver programmers to use the JDBC API.
Now, most database vendors provide corresponding JDBC drivers with database installation. So, you should not worry about this part.
Set up database authentication:
In this tutorial, we will use the MySQL database. When installing any of the above databases, its administrator ID is set to root and gives the specified settings selected password.
With root and password, you can create another user ID and password, or you can use root and password in a JDBC application.
There are various database operations, such as the creation and deletion of the database, which will require an administrator ID and password.
For the rest of the JDBC tutorial, we will use the MySQL database username as the ID and password as the password.
If you do not have enough permissions to create a new user, you can have the Database Administrator (DBA) create a user ID and password for you.
Create a database:
To create an EMP database, use the following steps:
Step 1:
Open a command prompt and change to the installation directory as follows:
The code copy is as follows:
C:/>
C:/>cd Program Files/MySQL/bin
C:/Program Files/MySQL/bin>
Note: This depends on the installation location on the MySQL system, the path to mysqld.exe may vary. You can also view documentation on how to start and stop a database server.
Step 2:
Start the database server by executing the following command if it is not running.
The code copy is as follows:
C:/Program Files/MySQL/bin>mysqld
C:/Program Files/MySQL/bin>
Step 3:
Create database EMP by executing the following command
The code copy is as follows:
C:/Program Files/MySQL/bin> mysqladmin create EMP -u root -p
Enter password: *******
C:/Program Files/MySQL/bin>
Create a table
To create an EMP database in the Employees table, follow these steps:
Step 1:
Open a command prompt and change to the installation directory as follows:
The code copy is as follows:
C:/>
C:/>cd Program Files/MySQL/bin
C:/Program Files/MySQL/bin>
Step 2:
Log in to the database as shown below
The code copy is as follows:
C:/Program Files/MySQL/bin>mysql -u root -p
Enter password: *******
mysql>
Step 3:
Create the Employee table as follows:
The code copy is as follows:
mysql> use EMP;
mysql> create table Employees
-> (
-> id int not null,
-> age int not null,
-> first varchar (255),
-> last varchar (255)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql>
Create a data record
Finally, create some records in the Employee table as follows:
mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
Query OK, 1 row affected (0.00 sec)
mysql>About the complete understanding of MySQL database, learn MySQL tutorials.
Now, you can start trying with JDBC. The next tutorial will give a sample example about JDBC programming.