• We Code
  • We Design
  • We Develope
  • We Write
  • We Share

menu

Tuesday, November 20, 2012

Connection between JDBC Driver And MySQL

This article of java course is explaining  the connection between JDBC driver and MySQL in java.

1. First step is downloading of Mysql JDBC driver

Download jdbc mysql driver by clicking here




2.Here is the code to connect JDBC to MySQL
   Class.forName("com.mysql.jdbc.Driver");
Connection connection = null;
  connection = DriverManager.getConnection(
          "jdbc:mysql://hostname:port/dbname","username", "password");
  connection.close();


3 . Lets see the example of connection between JDBC and MySQL

package com.javacourseblog.common;
 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
 
public class JDBCchecking {
 
 public static void main(String[] argv) {
 
  System.out.println("::::::::::::::::: Connectining MySQL with JDBC :::::::::::::::::::::::::::");
 
  try {
 
   Class.forName("com.mysql.jdbc.Driver");
 
  } catch (ClassNotFoundException e) {
 
   System.out.println("Find out the MySQL JDBC Driver ..?????????????????????????");
   e.printStackTrace();
   return;
 
  }
 
  System.out.println("MySQL JDBC Driver Registered!");
  Connection connection = null;
 
  try {
   connection = DriverManager
     .getConnection("jdbc:mysql://localhost:3600/javacourseblog",
       "root", "password");
 
  } catch (SQLException e) {
   System.out.println("Your Connection is not working !!!!!!!!!!!!!!!!!!!!!!!!!");
   e.printStackTrace();
   return;
  }
 
  if (connection != null) {
   System.out.println("You have connected your JDBC to the MySQL . now you can work on your Database ");
  } else {
   System.out.println("Sorry Dear you could not make connection. Try again !!!!!!!");
  }
 }
}


4. Now its time to execute the program         .          .................

Imagine  JDBCchecking.java is saved inside the folder  c:\java folder, and the MySQL JDBC driver is also stored inside the  c:\java ...............................................
C:\java>java -cp c:\java\mysql-connector-java-5.1.8-bin.jar;c:\java JDBCchecking
::::::::::::::::: Connectining MySQL with JDBC :::::::::::::::::::::::::::
MySQL JDBC Driver Registered!
You have connected your JDBC to the MySQL . now you can work on your Database
This is how we execute the java program of Connection between JDBC Driver And  MySQL
 
C:\java>
Well guys You are done with the connection of JDBC And MySQL

1 comments:

  1. nice blog and good posts! BTW, I also have a blog and a web directory, would you like to exchange links? let me know on emily.kovacs14@gmail.com

    ReplyDelete

...