Guide to Object-oriented Programming With Java (Buffalo State College Version)


Chapter 16: Using WindowBuilder to Create GUI Applications

Java provides a number or robust libraries for creating Graphical User Interfaces (GUIs). The include the java.awt.* and javax.swing.* libraries.

Adding WindowBuilder to Eclipse

Before you can work with a MySQL database using Java you need to create a connection to your database. First, you need to import java.sql.* the Java SQL utilities. Next you need to use try/catch to setup the SQl driver. Then you need to setup the variables that store the server name, database name, your username for that database, your password for that database, and the SQL Query string:

package edu.buffalostate.cis425.sp18.guide.your-last-name;

import java.sql.*;

public class ConnectToDatabase {

  public static void main(String[] args) {
    try {
      System.out.println("Loading JDBC driver...");
      Class.forName("com.mysql.jdbc.Driver");
      System.out.println("JDBC driver successfully loaded!");
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    String url = "jdbc:mysql://bscacad3.buffalostate.edu:3306/your-bsc-username";
    String username = "your-bsc-username";
    String password = "your-Banner-ID";
    String query = "SELECT firstName, lastName, DeptName, locationName ";
    query = query + "FROM employees AS e ";
    query = query + "JOIN departments AS d ON e.dept = d.departmentID ";
    query = query + "JOIN locations AS l ON d.location = l.locationID ";
    query = query + "ORDER BY e.lastName";

Figure 16-1: Java ConnectToDatabase Code (Part 1)

Once you have your driver and variables setup you can attempt to make a connection, execute your query, and print out the results:

    Connection connection = null;
    Statement stmt = null;

    try {
      System.out.println("Connecting to the MySQL database..." + url + "\n");
      connection = DriverManager.getConnection(url, username, password);
      System.out.println("\nMySQL Database connected!\n");
      stmt = connection.createStatement();
      ResultSet rs =stmt.executeQuery(query);
      while (rs.next()) {
        System.out.format("%s %s works in %s located in %s\n",
            rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
      }
      stmt.close();
    } catch (SQLException e) {
      System.out.println(e.toString());
    } finally {
      System.out.println("\nClosing the connection.");
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException ignore) {
        }
      }
      System.exit(0);
    }
  }
}

Figure 16-2: Java ConnectToDatabase Code (Part 2)
Loading JDBC driver...
JDBC driver successfully loaded!
Connecting to the MySQL database...jdbc:mysql://bscacad3.buffalostate.edu:3306/your-bsc-username

Sun Oct 29 16:51:38 EDT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

MySQL Database connected!

Joe Cocker works in HR located in Paris, FR
Bob Dylan works in HR located in New York, NY
John Prine works in HR located in London, UK

Closing the connection.

Figure 16-3: Java ConnectToDatabase Output

You can safely ignore that warning about the bscacad3.buffalostate.edu server not having a secure certificate installed.

Task - Create a Java Table Page From a MySQL Table

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2018 Jim Gerland