Database connection with mysql in Java



Today I will show you how to connect the Java application with the MySQL DB.

MySQL and Java both are the open source and freeware.


For the java and mysql connectivity I use the “mysql-connector-java-5.0.7.jar” file
You can download the mysql-connector-java-5.07.jar Click here

Syntax for the jdbc-mysql connection


Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:mysql://hostname:port/dbname","username", "password");

Following code demonstrate the connection between jdbc-mysql .Here I create a file on which I save the connection string on a encrypted format and use the encrypted data for the further connection.

public Connection db_con=null;
public String ip_address;
public String user_name;
public String password;
public String db_name;

public void DB_Connect()
{
try
{
       String fname="Osettings.sys";
        File Setting_file=new File(fname);
       if(Setting_file.exists())
       {
             if(Setting_file.length()>0)
              {
                  BufferedReader reader = new BufferedReader(new FileReader(Setting_file));
                  //Read file and stored the data on Vector such as user name , password etc..
                   Vector setting_Data=new Vector();
                   String line;
                   while ((line = reader.readLine()) != null)
                   {
                       setting_Data.addElement(line);
                    }

                   reader.close();
                  BASE64Decoder decoder = new BASE64Decoder();
                  byte[] Host_name = decoder.decodeBuffer(setting_Data.elementAt(0).toString());
                  byte[] User_name =decoder.decodeBuffer(setting_Data.elementAt(1).toString());
                  byte[] Password =decoder.decodeBuffer(setting_Data.elementAt(2).toString());
                  byte[] DB_name =decoder.decodeBuffer(setting_Data.elementAt(3).toString());
                  ip_address = new String(Host_name); // setting_Data.elementAt(0).toString();
                  user_name = new String(User_name) ; // setting_Data.elementAt(1).toString();
                  password = new String(Password); // setting_Data.elementAt(2).toString();
                  db_name = new String(DB_name); // setting_Data.elementAt(3).toString();
                  Class.forName ("com.mysql.jdbc.Driver");
                  db_con=(Connection)         DriverManager.getConnection("jdbc:mysql://"+ip_address+"?zeroDateTimeBehavior=convertToNull", user_name, password);
          System.out.println("Connection OK");
     }
     else
     {
          Here I open the javax swing screen which will ask for the host name , user name & password for the   connection . If file contain any wrong data.
      }
  }
else
{
        Here I open the javax swing screen which will ask for the host name , user name & password for the connection . If file not created .
   }
}
  catch(Exception e)
  {
         e.printStackTrace();
   }
}

// This function Disconnect database connection between jdbc and mysql

public void DB_Dissconnect()
{
        try
      {
          DB_Connect();
          db_con.close();
          System.exit(0);
    }
    catch(Exception e)
   {
      e.printStackTrace();
    }
}


Comments

Post a Comment

Popular posts from this blog

JTable Connection with JDBC-MYSQL and Pagination Over JTable

Java Interview Materials