Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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

                         

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.

                         

» View all related tutorials
Related Tags: c ide list interface script object io ip vi factory trigger instance int this id ai job shutdown if example

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.