My Headlines

Sunday, February 3, 2013

Konfigurasi MySQL Connector JDBC Di Ubuntu dan Windows

Step 1 : Kebutuhan 
    1. Jdk

  1. Xampp / Lampp
    Microsoft Windows (XP/Vista/7)
    Install : Double klik File xampp-win32-1.7.3.exe
    Ubuntu (Linux)
    Install :
    dnd@riyaniezt:~$ sudo tar xvfz xampp-linux-1.7.3a.tar.gz -C /opt
  2. JDBC
    Download “MySQL Connector For Java” : http://dev.mysql.com/downloads/connector/j/
Step 2 : Install
Microsoft Windows (XP/Vista/7)
  1. Ekstrak file “mysql-connector-java-5.1.18.tar.gz”.
  2. copy-kan file “mysql-connector-java-5.1.18-bin.jar” ke directory : “C:\Program Files\Java\jdk1.7.0_02\lib”.
Ubuntu (Linux)
  1. Ekstrak file “mysql-connector-java-5.1.18.tar.gz”
  2. Copy-kan file “mysql-connector-java-5.1.18-bin.jar” ke directory : “/opt/java/jdk1.7.0_02/lib/”,Console :
    dnd@riyaniezt:~$ sudo cp  mysql-connector-java-5.1.18-bin.jar /opt/java/jdk1.7.0_02/lib/
Step 3 : Konfigurasi CLASSPATH
Microsoft Windows (XP/Vista/7)
  1. Klik kanan icon My Computer, kemudian klik Properties,
  2. Kemudian pilih Advanced System Setting,
  3. Setelah muncul window System Properties,
  4. Pilih tab Advanced klik Environment Variables, maka akan muncul window Environment Variables ,
  5. Untuk menambahkan CLASSPATH, Pada bagian System variables klik New ,
  6. Pada text box Variable name isi dengan CLASSPATH,
  7. Pada Variable value isi dengan Alamat dari directory dimana MySQL Conector diletakkan,
    .;C:\Program Files\Java\jdk1.7.0_02\libmysql-connector-java-5.1.18-bin.jar
  8. Klik OK sebanyak tiga kali berturut-turut,
  9. Untuk memastikan setingan sudah berhasil, buka Command Prompt (CMD)
    echo %CLASSPATH%
  10. Restart komputer.
Ubuntu (Linux)
    1. File Dependencies,
      dnd@riyaniezt:~$ sudo apt-get install libmysql-java
    2. Edit File “Environment”,
      dnd@riyaniezt:~$ sudo gedit /etc/environment
    3. Tambahkan baris perintah berikut ke dalam file “Environment” :
      CLASSPATH=".:/opt/java/jdk1.7.0_02/lib/mysql-connector-java-5.1.18-bin.jar"
kemudian Save.
  1. Tulis perintah berikut (untuk memastikan bahwa setingan JDBC telah berhasil)
    dnd@riyaniezt:~$ echo $CLASSPATH
    .:/opt/java/jdk1.7.0_02/lib/mysql-connector-java-5.1.18-bin.jar
    dnd@riyaniezt:~$
  2. Restart komputer.
    dnd@riyaniezt:~$ sudo reboot
Step 4 : Test Koneksi JDBC & MySQL
import java.sql.Connection;
import java.sql.DriverManager;

class TestJDBC
{
  private static final String url = "jdbc:mysql://localhost";
  private static final String user = "[Insert User to access MySQL]";
  private static final String password = "[Insert password to access MySQL]";

  public static void main(String args[])
  {
    try
     {
       Connection con = DriverManager.getConnection(url, user, password);
       System.out.println("JDBC & MySQL successfully connected");
       System.out.println("By : http://sq.web.id");
     }
    catch (Exception e)
     {
       e.printStackTrace();
     }
  }
}
Output :
* … Good Luck !… *