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):
Here is the code of Job Class (ConnectionJob.java):