Installation and Testing of MySQL DB from Java

1. Download:
Common URL    : http://dev.mysql.com/downloads/mysql/5.1.html#downloads
MacOSX (10.6)  : http://dev.mysql.com/downloads/mirror.php?id=406393#mirrors
(mysql-5.1.61-osx10.6-x86.tar.gz)
Windows (32 bit): http://dev.mysql.com/downloads/mirror.php?id=406810#mirrors
(mysql-noinstall-5.1.61-win32.zip)
2. Install:
Unzip the zip to proper location, for example
/usr/local/mysql5
3. Configure:
i) Start mysql using below command to start it with root user
/usr/local/mysql5/bin/mysql -u root
ii) Create new Database 
mysql>CREATE DATABASE airlinedb;
iii) setup mySQL JDBC driver
a) Download JDBC Driver (Platform independent) 
b) Add mySQL jdbc driver to your project classpath 
mysql-connector-java-5.1.18-bin.jar
4.Testing:
i) Use below Java test to confirm the working   
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException; 
public class TestMySQLDB {
       public static void main(String[] argv) {
       System.out.println("Trying to connect mySQL database…");
              try { 
                     Class.forName("com.mysql.jdbc.Driver");
              } catch (ClassNotFoundException e) { 
                     System.out.println("Missing mySQL Driver?");
                     e.printStackTrace();
                     return;
              } 
              System.out.println("Registered mySQL JDBC driver successfully.");
              Connection connection = null;
              try {
                     connection = DriverManager
                                   .getConnection("jdbc:mysql://127.0.0.1:3306/airlinedb",
                                                 "root", "");

              } catch (SQLException e) {
                     System.out.println("Failed to Connect !");
                     e.printStackTrace();
                     return;
              }
               if (connection != null) {
                     System.out.println(" Connected Successfully !!!");
              } else {
                     System.out.println("Failed to Connect !");
              }
       }
}
5. Output:
Trying to connect mySQL database…

Registered mySQL JDBC driver successfully.
 Connected Successfully !!!

No comments :

Post a Comment