Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Build a Derby calendar, Part 1: Understanding JDBC

Nicholas Chase (ibm@nicholaschase.com), Freelance Writer, Backstop Media
Nicholas Chase has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, an Oracle instructor, and the Chief Technology Officer of an interactive communications company. He is the author of several books, including XML Primer Plus .

Summary:  Build a calendar and reminder application using the Java™ language and the Apache Derby database in Part 1 of this three-part tutorial series. This tutorial starts by showing you how to install the database and exploring interactions with it through Java Database Connectivity (JDBC). Parts 2 and 3 will examine different options for including Derby in your application and Derby's transactional capabilities as we move from a single-user to a multiuser system. Finally, we'll move to a multimode system that includes a Web interface.

Date:  13 Sep 2005
Level:  Intermediate PDF:  A4 and Letter (111 KB | 34 pages)Get Adobe® Reader®

Activity:  14259 views
Comments:  

Setting reminders

Now that you can create, update, and delete events, you need a way to remind people about them.

The reminder class

Create the reminder.java file, and add the basic class (see Listing 26).


Listing 26. The reminder class
                      public class Reminder{

   public void sendMessage(String reminderTo, String title, 
                           String description, String dateString) {
   }

   public void sendAllReminders(int eventMonth, 
                         int eventDay, int eventYear) {
   }

   public static void main (String args[]){
      Reminder rem = new Reminder();
   }

}

Once again, use the main() method to test the code. Before proceeding, make sure that mail.jar and activation.jar are in your class path.


Sending an e-mail

Sending an e-mail using JavaMail is a powerful, yet straightforward process (see Listing 27).


Listing 27. Sending an e-mail
                    
                    import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Transport;
import javax.mail.Message;

public class Reminder{

   public void sendMessage(String reminderTo, String title, 
                           String description, String dateString) {
      try {
            String smtp = "earthlink.net";
            String from = "calendar@nicholaschase.com";
            Properties props = System.getProperties();
            props.put("mail.smtp.host", smtp);
            Session session = 
                  Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom( new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(reminderTo));
            message.setSubject(title);
            String bodyText = "Just to reminder for you of the "+
                 "following event to take place on "+
                 dateString + ":\n\n"; 
            bodyText = bodyText + description;
            message.setText(bodyText);
            Transport.send(message);
      } catch (Exception e){
            e.printStackTrace();
      }
   }

   public void sendAllReminders(int eventMonth, 
                         int eventDay, int eventYear) {
   }

   public static void main (String args[]){
      Reminder rem = new Reminder();
      rem.sendMessage("questions@nicholaschase.com", "Test event", 
                                "Test description", "8/26/2005");
    }

}

First, define how and from where you're sending the message. The mail.smtp.host Property defines the Simple Mail Transfer Protocol (SMTP) server that sends the mail. Contact your ISP for information on this value, or simply check the settings on your mail client. Next, create a session and assign the Properties to it.

The next step is to create the actual message. After associating it with the session and, thus, with the Properties, you can set information, such as the from, to, subject, and body. Finally, you can send the message.


Setting all reminders

Once you can send a single reminder, you can send multiple reminders based on records in the database (see Listing 28).


Listing 28. Setting all reminders
                    
                    import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.ResultSet;

import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Transport;
import javax.mail.Message;


public class Reminder{

   public static String driver = 
                  "org.apache.derby.jdbc.EmbeddedDriver";

   public void sendMessage(String reminderTo, String title, 
                           String description, String dateString) {
...
   }

   public void sendAllReminders(int eventMonth, 
                         int eventDay, int eventYear) {
      try {
         Class.forName(driver).newInstance();
         Connection conn = null;
         conn = DriverManager.getConnection(
                        "jdbc:derby:c:\\derby\\calendar");

         Statement s = conn.createStatement();
         ResultSet rs = s.executeQuery("SELECT * FROM Event "+
                            "where eventMonth="+eventMonth+
                            " and eventDay="+eventDay+" and "+
                            "eventYear="+eventYear);
         if (!rs.next()){
            System.out.println("There are no events for "+
                        eventMonth+"/"+eventDay+"/"+eventYear);
         } else {


            while (rs.next()) {
               String title = rs.getString(2);
               String description = rs.getString(3);
               String remindersTo = rs.getString(4);
               String dateString = rs.getString(5)+"/"
                     +rs.getString(6)+"/"+rs.getString(7);
               sendMessage(remindersTo, title, description,
                                                 dateString);
            }
         }

         rs.close();
         s.close();
         conn.close();
         try {
            DriverManager.getConnection(
                              "jdbc:derby:;shutdown=true");
         } catch (SQLException se) { }

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main (String args[]){
      Reminder rem = new Reminder();
      rem.sendMessage("questions@nicholaschase.com", "Test event", 
                                "Test description", "8/26/2005");
      rem.sendAllReminders(2,27,2006);
                

There is nothing new here. You're simply creating a connection, creating a query, and looping through the results. In this case, you're sending the data to the sendmessage() method for further processing.

8 of 12 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Java technology
ArticleID=133105
TutorialTitle=Build a Derby calendar, Part 1: Understanding JDBC
publish-date=09132005
author1-email=ibm@nicholaschase.com
author1-email-cc=ruterbo@us.ibm.com