Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Tutorials
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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. 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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.