Details of setting of JDBC environment

  • 2020-04-01 03:10:08
  • OfStack

Install Java:

Install J2SE development kit (JDK 5.0) to download 5.0: (link: http://www.oracle.com/technetwork/java/index.html).

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\ JDK 1.5.0
CLASSPATH: this environment variable should have the 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.

These variables may already be set, but just to make sure that they are checked here.

Go to the control panel and double-click the system. If you are a Windows XP user, you may want to open "performance "=" maintenance "and see the system icon.

Go to the advanced TAB, and then click environment variables.

Now select all input variables set correctly.

Will automatically get the JDBC packages java.sql and javax.sql when J2SE development kit 5.0 (JDK 5.0) is installed

Installation database:

Of course, the most important thing that will be needed is the actual running database with which tables can be queried and modified.

Installing the database is best. There are many choices, the most common are:

MySQL DB: MySQL is an open source database. Can be downloaded from here (link: http://dev.mysql.com/downloads/mysql), recommended download complete Windows installation.

In addition, download and install MySQL admin and MySQL query browser. These are gui-based tools that will make development easier.

Finally, download and unzip the MySQL Connector/J (link: #) in a convenient directory. For the purpose of this tutorial, we will assume that the driver has been 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. Can be downloaded from here (link: http://www.postgresql.org/download/).

The Postgres installation includes a gui-based administration tool called pgAdmin III. The JDBC driver is also included as part of the installation.

Oracle DB: Oracle database is a commercial database sold by Oracle. It is assumed that the required distribution medium is available for installation.

The Oracle installation includes a gui-based administration tool called Enterprise Manager. The JDBC driver is also included as part of the installation.

Install the database driver:

The latest JDK includes the jdbc-odbc bridge driver, which makes the JDBC API available to most open database connection (ODBC) driver programmers.

Most database vendors now provide the appropriate JDBC driver with the installation of the database. So, you shouldn't worry about that part.

Set up database authentication:

In this tutorial, we will use the MySQL database. When any of the above databases is installed, its administrator ID is set to root and the password specified for the Settings selected is given.

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 database creation and deletion, which will require an administrator ID and password.

For the rest of the JDBC tutorial, we'll use the MySQL database username as the ID and the password as the password.

If you don't have enough privileges to create a new user, ask the database administrator (DBA) to create a user ID and password for you.

Create database:

To create an EMP database, use the following steps:

Step 1:
Open the command prompt and change to the installation directory as follows:


C:> 
C:>cd Program FilesMySQLbin 
C:Program FilesMySQLbin>


Note: depending on the installation location on the MySQL system, the path to mysqld.exe may vary. You can also view the documentation on how to start and stop the database server.

Step 2:
Start the database server by executing the following command if it is not running.


C:Program FilesMySQLbin>mysqld 
C:Program FilesMySQLbin>


Step 3:
Create the database EMP by executing the following command


C:Program FilesMySQLbin> mysqladmin create EMP -u root -p 
Enter password: ******** 
C:Program FilesMySQLbin>


Create a table
To create the database for EMP in the Employees table, do the following:

Step 1:
Open the command prompt and change to the installation directory as follows:

The same code at the page code block index 0

Step 2:
Log into the database as shown below


C:Program FilesMySQLbin>mysql -u root -p 
Enter password: ******** 
mysql>


Step 3:
Create the Employee table 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 secant)

Mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');
Query OK, 1 row affected (0.00 secant)

Mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
Query OK, 1 row affected (0.00 secant)

Mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
Query OK, 1 row affected (0.00 secant)

Mysql> For a complete understanding of the MySQL database, learn the MySQL tutorial.

Now you can start experimenting with JDBC. The following tutorial gives a sample example of JDBC programming.


Related articles: