Java Tutorial - Java Script :
Install the MySQL JDBC Driver
The MySQL JDBC driver is downloaded separately from the MySQL database distribution. The same file is used for Linux and Windows. The file can be downloaded from http://www.mysql.com and is normally named something
like:
MySQL-connector-java-version.jar
This is a .jar file that has the source code and documentation for the driver along with a .jar file that contains the driver itself. To clarify, the driver is a jar file that is contained within the downloaded .jar file. To use the driver, you will first need to extract the driver .jar file from the downloaded jar file.
The .jar tool provided with the JDK can be used to extract the entire downloaded .jar file.
jar -xvf MySQL-connector-java-version.jar
This creates a directory tree that contains the needed .jar file in the topmost directory. The driver is named:
mysql-connector-java-version-bin.jar
The following program can be used to test the driver and make certain it can connect to the database:
import java.sql.*;
public class TestJdbc
{
public TestJdbc()
{
}
public static void main(String[] args)
{
String connectionURL = “jdbc:mysql://localhost:3306/test”;
String driverClass= “com.mysql.jdbc.Driver”;
String dbUser = “root”;
String dbPassword = “”;
Connection conn = null;
try
{
Class.forName(driverClass);
conn = DriverManager.getConnection(
connectionURL, dbUser, dbPassword );
DatabaseMetaData md = conn.getMetaData();
System.out.println( “Database=”+md.getDatabaseProductName());
System.out.println( “Driver=”+md.getDriverName());
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
try
{
if( conn != null )
conn.close();
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
}