headermask image

header image

JDBC connectorj for MySql Database

Java connects to MySql Database using JDBC driver named Connector/J. I found it a bit interesting in configuring and testing the driver. I previously hadn’t connected using Connector/J. In my personal view it’s much more easier to connect using php to MySql when you first try your hand. Let me get to the details.

I downloaded Connector/J driver from the ConnectorJ_5.0
You need to download the latest driver. Its around 8 MB which contains both the source and the binary.You are allowed to download either a windows version (Zip) or a Linux version (tar.gz). Anyhow both are compressed form and you need to uncompress for both platform. I used windows hence i will explain the steps involved in windows platform.

First copy the jar file, com and org folders contained in the zip file into one of classpath folders where JVM can find it. I copied mysql-connector-java-5.0.5-bin.jar, com and org folder into a folder named as connectorj in d: drive.

In my case i didn’t had the classpath set.
I created a new variable named classpath in the Environmental variable.
Variable name:classpath
Variable value:.;D:\connectorj\mysql-connector-java-5.0.5-bin.jar

For setting classpath and for more information see classpath info in Sun Site or Javapedia

Then i tested the driver using the code,

public class TestCon {
public static void main(String args[]) {
try {
class.forName(”com.mysql.jdbc.Driver”).newInstance();
System.out.println(”JVM is able to find the driver. Test success”);
} catch (Exception e) {
System.out.println(”Driver error. Cann’t find the driver”);
e.printStackTrace();
}
}
}

Compiled using javac TestCon.java and executed by java TestCon for which i got the
JVM is able to find the driver. Test success
This demonstrated that i was able to connect to connectorj driver.

Hope you would also have the same result.

If you liked my post, feel free to subscribe to my rss feeds

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*