Connecting Java and MySQL

Hi

As you all know Java is one of the best programming languages around and MySQL is the world’s most popular open source database. Often these are used to build business applications. There arises the need to connect both of these technologies. But for a newbie like me it is quite a daunting task to make them talk to each other. So, I am putting forward a tutorial showing how to accomplish the task.

I am assuming that the reader have installed both Java and MySQL.

First of all we need MySQL Connector/J driver. It is the official JDBC driver for MySQL. Which can be downloaded from  http://dev.mysql.com/downloads/connector/j/ .Moreover I have enclosed the driver used for this tutorial.

It was the easy part. Now the most confusing part if you don’t know how to set classpath. Classpath can be set in more than one way. I shall use the method which I prefer the most.

The file name of the MySQL Connector/J driver I have downloaded is

mysql-connector-java-5.1.11-bin.jar

I have kept this file in C:\Program Files\Java\jdk1.6.0_12\lib in my system. You can place it anywhere you like.

JAR file (or Java ARchive) collection of many files into one. Java developers generally use .jar files to distribute Java applications or libraries, in the form of classes and associated metadata and resources. In this case mysql-connector-java-5.1.11-bin.jar file contains all the classes required for Java, MySQL connection.



Now I shall set the classpath so that our program can use this library to connect the MySQL database.

  1. Click Start or the Windows icon in the bottom left corner of your screen. Then Select Computer, Properties.
  2. jmcimg1.jpg

     

  3. Now Select Advanced System Settings
  4. jmcimg2.jpg

  5. Select Environment Variable in the System Properties Window
  6. jmcimg3.jpg

     

  7. Choose ClassPath from the System Variable Pane and click Edit Button.
  8. jmcimg4.jpg

     

  9. Enter the Path of mysql-connector-java-5.1.11-bin.jar file after putting a semicolon in case there are previous settings. In most cases there you would find some. Click Ok couples of time. That’s all.  You have set the classpath. In some instances you might have to restart your system.

Now we shall write the code to connect MySQL database.

 

import java.sql.*;

public class TestConnection{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "testjava";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"
    String password = "";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
    catch (Exception e) {
      e.printStackTrace();
    }
  }

AttachmentSize
mysql-connector-java-5.1.11-bin.jar715.49 KB

Comments

Beginners questions

- on my computer it only has something called class and not classpath - do i create something called classpath in there ?

Also I have stored it in C:\ should the path read C:\ ?

exception in 1st line in program

I am using netbeans 6.5 and i faced an exception in this line
Class.forName("com.mysql.jdbc.Driver");
I have set the classpath= mysql-connector-java-5.1.7-bin.jar;
but still the exception is there. Can u please help me on this....?

Do you have MySql Installed

Hi,
It appears that there are some issue with connection. I would recommend you to check your mysql server. If it is working fine then report. I will help you out on this.

I am using netbeans 6.5 and i

I am using netbeans 6.5 and i faced an exception in this line
Class.forName("com.mysql.jdbc.Driver");
I have set the classpath= mysql-connector-java-5.1.7-bin.jar;
but still the exception is there. Can u please help me on this....?

The best Tutorial i've seen thus far

Thank you very much, your explanation was detailed. Please explain more on the String Urls