Establish a Connection with MySQL Database

In this section, we will learn coding methods of establishing the connection between MySQL database and quartz application for updating and manipulating the data of MySQL database tables.

Establish a Connection with MySQL Database

Establish a Connection with MySQL Database

     

In this section, we will learn coding methods of establishing the connection between MySQL database and quartz application for updating and manipulating the data of MySQL database tables. Here, we are going to provide a simple way for establishing the connection with the help of following program. See below for detail information.

Description of program:

As we know a quartz application needs two classes: first is scheduler class (ConnectionScheduler.java) and second one is job class (ConnectionJob.java) that implement in the Job interface. We will require the SchedulerFactory instance for implementing the scheduler. After that we have to start the scheduler by using the start() method then we will add jobs with the help of JobDetail() method. Which contains the name of jobs, job groups and the class name that has some job to be performed by users. Then we have to create a trigger to perform the job at a specified time. Both (JobDetail and CronTrigger) objects are added in the quartz scheduler to the scheduleJob() method. By following the entire process we will implement the scheduler class. Now, we will require the job class that establishes the connection with the MySQL database by using the JDBC driver in forName() method. If the JDBC driver doesn't support then exception is occurred that can be thrown by the ClassNotFoundException and it will display a message "Class not found!". After getting the JDBC driver we can give the string type 'url' in the getConnection() method. If we get the connection then it displays the connection with a message "Connection is created!" and also shows the job name, group name, trigger name and its firing time. 

Here is the code of Scheduler Class (ConnectionScheduler.java):

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class ConnectionScheduler {
  public static void main(String arg[]){
  try{
  new ConnectionScheduler();
  }
  catch(Exception e){
  e.printStackTrace();
  }
  }
  public ConnectionScheduler()throws Exception{
  SchedulerFactory sf = new StdSchedulerFactory();
  Scheduler sche = sf.getScheduler();
  sche.start();
  JobDetail jDetail = new JobDetail(
  "JDBC Connection"
,"mysql",ConnectionJob.class);
  CronTrigger crTrigger = new CronTrigger(
  "cronTrigger"
,"mysql","0/8 * * * * ?");
  sche.scheduleJob(jDetail, crTrigger);
  }
}

 Download this code.

Here is the code of Job Class (ConnectionJob.java):

import java.sql.DriverManager;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class ConnectionJob implements Job {
  
  public void execute(JobExecutionContext context
  throws 
JobExecutionException {
  // TODO Auto-generated method stub
  System.out.println("Job name:"+context.getJobDetail().getName());
  System.out.println("Group name:"+context.getJobDetail().getGroup());
  System.out.println("Trigger name:"+context.getTrigger().getName());
  System.out.println("Firing Time:"+context.getFireTime());
  try{
  Class.forName("com.mysql.jdbc.Driver");
  }
  catch(ClassNotFoundException c){
  System.out.println("Class not found!");
  }
  try{
  String url = 
   "jdbc:mysql://localhost/JDBCquartz?user=root&password=root"
;
  
  Connection con = (Connection)DriverManager.getConnection(url);
  System.out.println("Connection :"+con);
  System.out.println("Connection is created!");

  con.close();
  System.exit(0);
  }
  catch(Exception s){
  System.out.println("Doesn't establish the connection!");
  System.exit(0);
  }
  }
}

 Download this code.