Download Connector/J MySQL JDBC Driver driver from websites or
vistit the link
http://www.mysql.com/downloads/connector/j/
Among the extracted files will be a JAR file with a name like 'mysql-connector-java-3.0.8-stable-bin.jar'. Copy this file to your %JAVA_HOME%/jre/lib/ext folder which on my system happens to be D:\j2sdk1.4.2\jre\lib\ext>.
You should also add the complete path to this JAR file in your CLASSPATH environment variable. In case if you don't know how to do that, go to Start -> Settings -> Control Panel -> System -> Advanced (tab) -> Environment Variables. Double-click 'CLASSPATH' if it is already listed there or click 'New' to create one with the name of 'CLASSPATH'. If you already had a 'CLASSPATH environment variable listed, simply type in (or paste) the complete path including file name in the window that opens up *at the end of* existing CLASSPATH values. If you are creating a new 'CLASSPATH' environment variable then simple enter '.;' and then append the complete path to Connector/J's JAR file.
Note: The CLASSPATH variable contains a list of semi-colon separated folders and JAR files where JVM will search for Java class files. You should always separate paths with semi-colon (;) so that JVM can understand where one path ends and next one begins. Also keep in mind that in CLASSPATH variable paths, the first path is always '.' (single dot) which means current folder, so that JVM starts the search for Java class files from the current folder.
If you've correctly added the path to Connector/J JDBC driver's JAR file in your CLASSPATH environment variable then you are done with installing MySQL Connector/J JDBC driver.
Tip: When you make changes to any of your environment variables (like CLASSPATH), close and re-open any open command prompt windows so that they refresh their environment variable value's cache.
How to test if Connector/J has been installed and configured correctly?
We will now create a simple Java program which will try to connect to our MySQL database server using Connector/J JDBC driver. Create a new JdbcExample1.java file and copy/paste following code in it:
import java.sql.*;
public class JdbcExample1 {
public static void main(String args[]) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");
if(!con.isClosed())
System.out.println("Successfully connected to MySQL server...");
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}
How to configure Connector/J to use named pipes to connect to MySQL Database server?
The above Java program used TCP/IP to connect to MySQL database. We'll now make just one change and this time it will use name pipe to connect to our MySQL server instance.
Simply convert this line in JdbcExample1.java:
con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");
To this one:
con = DriverManager.getConnection("jdbc:mysql:///test" +
"?socketFactory=com.mysql.jdbc.NamedPipeSocketFactory",
"root", "secret");
Compile and run this class, this time it'll use named pipe to connect to MySQL database. If you again get a success message then congratulations you've setup named pipe on both sides (server and client) correctly.
Why and when to use named pipes in place of TCP/IP with MySQL/Connector/J?
Named pipes are a high performane alternative to TCP/IP on a Windows NT/2000/XP/2003 only environment. The Connector/J JDBC driver's documentation suggests an increase in data access speed of 30%-50% as compared to TCP/IP. You can always use named pipes in an intranet environment. So to keep it short, use named pipes when you can and TCP/IP when you cannot.
vistit the link
http://www.mysql.com/downloads/connector/j/
Among the extracted files will be a JAR file with a name like 'mysql-connector-java-3.0.8-stable-bin.jar'. Copy this file to your %JAVA_HOME%/jre/lib/ext folder which on my system happens to be D:\j2sdk1.4.2\jre\lib\ext>.
You should also add the complete path to this JAR file in your CLASSPATH environment variable. In case if you don't know how to do that, go to Start -> Settings -> Control Panel -> System -> Advanced (tab) -> Environment Variables. Double-click 'CLASSPATH' if it is already listed there or click 'New' to create one with the name of 'CLASSPATH'. If you already had a 'CLASSPATH environment variable listed, simply type in (or paste) the complete path including file name in the window that opens up *at the end of* existing CLASSPATH values. If you are creating a new 'CLASSPATH' environment variable then simple enter '.;' and then append the complete path to Connector/J's JAR file.
Note: The CLASSPATH variable contains a list of semi-colon separated folders and JAR files where JVM will search for Java class files. You should always separate paths with semi-colon (;) so that JVM can understand where one path ends and next one begins. Also keep in mind that in CLASSPATH variable paths, the first path is always '.' (single dot) which means current folder, so that JVM starts the search for Java class files from the current folder.
If you've correctly added the path to Connector/J JDBC driver's JAR file in your CLASSPATH environment variable then you are done with installing MySQL Connector/J JDBC driver.
Tip: When you make changes to any of your environment variables (like CLASSPATH), close and re-open any open command prompt windows so that they refresh their environment variable value's cache.
How to test if Connector/J has been installed and configured correctly?
We will now create a simple Java program which will try to connect to our MySQL database server using Connector/J JDBC driver. Create a new JdbcExample1.java file and copy/paste following code in it:
import java.sql.*;
public class JdbcExample1 {
public static void main(String args[]) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");
if(!con.isClosed())
System.out.println("Successfully connected to MySQL server...");
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}
How to configure Connector/J to use named pipes to connect to MySQL Database server?
The above Java program used TCP/IP to connect to MySQL database. We'll now make just one change and this time it will use name pipe to connect to our MySQL server instance.
Simply convert this line in JdbcExample1.java:
con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");
To this one:
con = DriverManager.getConnection("jdbc:mysql:///test" +
"?socketFactory=com.mysql.jdbc.NamedPipeSocketFactory",
"root", "secret");
Compile and run this class, this time it'll use named pipe to connect to MySQL database. If you again get a success message then congratulations you've setup named pipe on both sides (server and client) correctly.
Why and when to use named pipes in place of TCP/IP with MySQL/Connector/J?
Named pipes are a high performane alternative to TCP/IP on a Windows NT/2000/XP/2003 only environment. The Connector/J JDBC driver's documentation suggests an increase in data access speed of 30%-50% as compared to TCP/IP. You can always use named pipes in an intranet environment. So to keep it short, use named pipes when you can and TCP/IP when you cannot.
No comments:
Post a Comment