The following Java program was written as a test for a job I was applying for. I did not get the job, but it is an example of my Java programming.
// Program Java Job
// 9/27/12
// Tim Noerr
// This is the main class; this program is for Phil at Two Point Conversions. It reads
// a MYSQL data base of employees, checks if an expense report was sent
// by 1:00 pm and sends an email reminder if no expense report was sent, it will continue
// to send reminders every hour that a report is not sent until 8:00 pm.
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.TimeZone;
import javax.swing.*;
public class SendEmail {
boolean didOne = false;
boolean didTwo = false;
boolean didThree = false;
boolean didFour = false;
boolean didFive = false;
boolean didSix = false;
boolean didSeven = false;
boolean didEight = false;
boolean didNine = false;
public SendEmail() {
Timer timer = new Timer(120000, new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// this section of code will check what time of day it is
// and on the hours between 1 and 8 pm will call the checkReports method to
// check when the data base was updated and send an email if it wasn't
Calendar calendar = new GregorianCalendar();
int i = Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
if (i == 13 && didOne == false) {
didOne = true;
didNine = false;
checkReports();
}
if (i == 14 && didTwo == false) {
didTwo = true;
checkReports();
}
if (i == 15 && didThree == false) {
didThree = true;
checkReports();
}
if (i == 16 && didFour == false) {
didFour = true;
checkReports();
}
if (i == 17 && didFive == false) {
didFive = true;
checkReports();
}
if (i == 18 && didSix == false) {
didSix = true;
checkReports();
}
if (i == 19 && didSeven == false) {
didSeven = true;
checkReports();
}
if (i == 20 && didEight == false) {
didEight = true;
checkReports();
}
if (i == 21 && didNine == false) {
didOne = false;
didTwo = false;
didThree = false;
didFour = false;
didFive = false;
didSix = false;
didSeven = false;
didEight = false;
didNine = true;
}
}
public void checkReports() {
Connection con;
Statement stmt;
ResultSet rs;
// the UpdateDatabase class does the query of the database
rs = UpdateDatabase.doQuery("SELECT * FROM Emp");
try {
while (rs.next()) {
String name = rs.getString(2);
String email = rs.getString(4);
// get information when the record was last updated from database
Date whenUpdated1 = rs.getDate(5); // date in milliseconds
Date whenUpdated2 = rs.getTime(5); // time in milliseconds
// get the timezone offset this is the difference in time
// between local time and Greenwich mean time,
Calendar cal3 = new GregorianCalendar();
long offset = 0;
TimeZone tz = cal3.getTimeZone();
long off = tz.getOffset(offset);
// add date and time and offset, the offset brings the time
// from Greenwich mean time to local time, the database is
// using local time
long secData = (whenUpdated1.getTime() + whenUpdated2.getTime() + off);
// put the database information back into a date object
Date dateDatabase = new Date(secData);
// put the database last updated information into a calendar object
Calendar calDatabase = new GregorianCalendar();
calDatabase.setTime(dateDatabase);
// create a calendar object with the present time
Calendar calNow = new GregorianCalendar();
// minus 15 hours from the present time to determine
// if the record was changed in the last 15 hours
calNow.add(Calendar.HOUR_OF_DAY, -15);
// if the database hasn't been updated within the last
// 15 hours this will call class to send an email
if (calNow.compareTo(calDatabase) > 0) {
System.out.println(" send email to " + name);
SendIt.SendIt1(name, email);
}
}
UpdateDatabase.closeAll();
} catch (SQLException ex) {
ex.toString();
}
}
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
JFrame frame = new JFrame();
SendEmail sendEmail = new SendEmail();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(0, 0);
}
}
// Program Java Job
// 9/27/12
// Tim Noerr
// This class will send an email to an employee
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendIt {
public static void SendIt1 (String EmpName, String Email){
String host = "smtp.gmail.com";
String from = "tn101660@gmail.com";
String to = Email.trim();
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "jo1anny!");
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
try {
Authenticator auth = new SMTPAuthenticator("tn101660@gmail.com", "jo1anny!");
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("Expense Report");
// Now set the actual message
message.setText(EmpName + "\n\nPlease remember to submit your expense report.");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
/** * This class provides authentication information. * @author Ha Minh Nam * */
private static class SMTPAuthenticator extends javax.mail.Authenticator {
private String userName;
private String password;
public SMTPAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
}