Java J2ee Tutorials

mysql-connector.jar for Connecting Java to MySQL

Tuesday, February 7, 2012

This blog helps one how to connect java to mysql database, there are some steps to follow at first check for the java if java is not installed download latest jdk from sun java, This is where you can get latest java development kit http://java.sun.com/javase/downloads/index.jsp be sure that you installed java, you can check that via command prompt or terminal use this command without quote “java -version” so you must see like this java version “1.6.0_16″, Basically java has no jar file or pre built class for mysql connection which is called mysql-connector i mean driver class which supports for mysql connection so its better to download package called mysql connector so this plays a major role during connection so download that from a mysql web site http://www.mysql.com/products/connector/ they given more version look for latest version and download ans place it in a know place say(c:\mysql or /home/mysql) but don’t forget to unzip mysql-connector and i believe that mysql-server service already started in system.

This is program check the connectivity of mysql-server through localhost

import java.sql.*;

public class DatabaseConnectionExample {
public static void main(String args[]) {
System.out.println("Database Connection Example");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
//String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
//con = DriverManager.getConnection(url + dbName,userName,password);
con = DriverManager.getConnection(url,userName,password);
System.out.println("Database Connected");
con.close();
System.out.println("Connection Colsed.");
}
catch (Exception e){
System.out.println("Error: "+e);
}
}
}

Save as DatabaseConnectionExample.java, while compile it you have to include classpath of java to know more about classpath sun java is best guide http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/classpath.html

Now it time to compile open command prompt go to the location of DatabaseConnectionExample.java if your your program is under c:\

use c: also may be at /home for tux users now compile it using javac -classpath /home/mysql/mysql-connector.bin.jar DatabaseConnectionExample.java so this command has three part javac, -calsspath, filepath, here javac is a compiler and we included a classpath of mysql-connector jar file locate that wherever it is for an example i given as /home/mysql/mysql-connector.bin.jar and next to that is java file. after compilation program needs to run so java interpreter shall do this job if this command is used java -classpath /home/mysql/mysql-connector.bin.jar: DatabaseConnectionExample. in my second I used colon so its specific for linux user unless windows use ;(semi-colon)

For Eclipse users, use can import mysql-connector.jar file to classpath before creating a class file, right click the project go to properties and choose java build path and then choose libraries tab and add external jar file.

Program shows this output
Database Connected.
Connection Closed.

Free Career Predictions

0 comments:

Post a Comment