Java & MySQL – Environment Setup


Java & MySQL – Environment Setup



”;


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

Install Java

Java SE is available for download for free. To download click here, please download a version compatible with your operating system.

Follow the instructions to download Java, and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories.

Setting Up the Path for Windows 2000/XP

Assuming you have installed Java in c:Program Filesjavajdk directory −

  • Right-click on ”My Computer” and select ”Properties”.

  • Click on the ”Environment variables” button under the ”Advanced” tab.

  • Now, edit the ”Path” variable and add the path to the Java executable directory at the end of it. For example, if the path is currently set to C:WindowsSystem32, then edit it the following way


C:WindowsSystem32;c:Program Filesjavajdkbin

Setting Up the Path for Windows 95/98/ME

Assuming you have installed Java in c:Program Filesjavajdk directory −

  • Edit the ”C:autoexec.bat” file and add the following line at the end −


SET PATH = %PATH%;C:Program Filesjavajdkbin

Setting Up the Path for Linux, UNIX, Solaris, FreeBSD

Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this.

For example, if you use bash as your shell, then you would add the following line at the end of your .bashrc


export PATH = /path/to/java:$PATH''

You automatically get both JDBC packages java.sql and javax.sql, when you install J2SE Development Kit.

Install MySQL Database

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

  • 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. For the purpose of this tutorial we will assume that you have installed the driver at C:Program FilesMySQLmysql-connector-java-5.1.8.

  • Accordingly, set CLASSPATH variable to C:Program FilesMySQLmysql-connector-java-5.1.8mysql-connector-java-5.1.8-bin.jar. Your driver version may vary based on your installation.

Set Database Credential

When we install MySQL database, its administrator ID is set to root and it gives provision to set a password of your choice.

Using root ID and password you can either create another user 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 the JDBC tutorial, we would use MySQL Database with guest as ID and guest123 as password.

If you do not 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 TUTORIALSPOINT database, use the following steps −

Step 1

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


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

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.


C:Program FilesMySQLbin>mysqld
C:Program FilesMySQLbin>

Step 3

Create the TUTORIALSPOINT database by executing the following command −


C:Program FilesMySQLbin> mysqladmin create TUTORIALSPOINT -u guest -p
Enter password: ********
C:Program FilesMySQLbin>

Create Table

To create the Employees table in TUTORIALSPOINT database, use the following steps −

Step 1

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


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

Step 2

Login to the database as follows −


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

Step 3

Create the table Employees as follows −


mysql> use TUTORIALSPOINT;
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 Data Records

Finally you create few records in 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>

For a complete understanding on MySQL database, study the MySQL Tutorial.

Now you are ready to start experimenting with JDBC. Next chapter gives you a sample example on JDBC Programming.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *