//DBExample1.java
// Written by Chuck Cusack, Feb 2002.
// A simple applet that connects to a MySQL database.
//
// As with much code submitted by my students, this code
// is "self-documenting"
//

import java.sql.*;
import java.awt.*;
import javax.swing.*;

public class DBExample1 extends JApplet {
    public void init() {
        String sqlDriver="org.gjt.mm.mysql.Driver";
        String url = "jdbc:mysql://tacse/Sales";
        String userName="jde156";
        String password="student";
        String query="select  EmpFirstName, EmpLastName," +
                     "EmpStreetAddress, EmpCity, EmpState,"+
                     "EmpZipCode from Employees";
        String theAddresses="";

        Connection con;
        Statement stmt;

        try {
            Class.forName(sqlDriver);

        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }

        try {
            con = DriverManager.getConnection(url,userName,password);
           //con = DriverManager.getConnection(
            //        url+"?user=" + userName +"&password=" + password);
            stmt = con.createStatement();
	    ResultSet rs = stmt.executeQuery(query);
	    while (rs.next()) {
                  //--------------------------------------------------
                  // Get the information about the current student, 
                  // based on the column names.
                  //
                  theAddresses+=rs.getString("EmpLastName")+" "
                         +rs.getString("EmpFirstName")+"\n"
                         +rs.getString("EmpStreetAddress")+"\n"
                         +rs.getString("EmpCity")+", "
                         +rs.getString("EmpState")+"  "
                         +rs.getString("EmpZipCode")+"\n\n";
                  //--------------------------------------------------
                  // Alternatively, use the column numbers:
                  //
                  //theAddresses=rs.getString(2)+" "
                  //       +rs.getString(1)+"\n"
                  //       +rs.getString(3)+"\n"
                  //       +rs.getString(4)+", "
                  //       +rs.getString(5)+"  "
                  //       +rs.getString(6)+"\n\n";
            }
            stmt.close();
            con.close();
        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
            theAddresses="This applet needs to be on the same machine\n"
                        +" as the MySQL database is on, and apparently\n"
                        +" it is not.  If it was, you would have seen\n"
                        +" a list of addresses here.";
        }

    // Draw the stuff on the applet.
    JLabel title=new JLabel("Employees Adresses");
    JTextArea addresses=new JTextArea(theAddresses);

    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(title,BorderLayout.NORTH);
    this.getContentPane().add(addresses,BorderLayout.CENTER);
    this.validate(); 
    }
}


