Java

Java Course: JDBC MySQL Database Connectivity

This free online course will teach you how to interact with a MySQL database programmatically using Java. Almost without exception, every application you build in the real world will store critical information in a database, and knowing how to use Java database connectivity (JDBC) to communicate with a database will be an excellent skill to have on your resume.

If you haven't worked with databases or SQL before, then please first complete the Database Course: MySQL and SQL CRUD Operations before taking this course.

Course Prep:

  1. Read Chapter #14 in the Core Servlets and JavaServer Pages textbook (pages 591 - 606, and pages 616 - 622).
  2. View the JDBC lecture in Powerpoint format (also available here in pdf format), which will provide an overview of JDBC and several useful code samples.
  3. Download and install MySQL Server 5.0 by following the instructions in MySQL5_Install_Configure.doc.

Course Exercises:

  1. Follow course exercises 1 through 4 in the Database Course: MySQL and SQL CRUD Operations to load a pre-built SQL script. This will create an Etraining database containing two tables: users and accounts.
  2. Create the following Java business objects and data access object, and implement the methods defined below:
    • Login (business logic specific to user information)

Eclipse Tips and Tricks

Eclipse is a very powerful IDE with a vast array of features, and it seems like I'm always stumbling on new ones. Here's a list of the Eclipse shortcuts I use the most often:

  • Find any resource or type - Ctl + Alt + R will display a Search for Resource dialog - just start typing the name of the resource, and Eclipse will display a list of matching resources (handy for finding xml deployment descriptors when you're not sure where they're located). Ctl + Alt + T does the same thing, but is specific to Java types.
  • Move a line of code up and down - Alt + UpArrow moves a line of code up. Alt + DownArrow moves it down. It will not overwrite existing lines of code above or below the current line, and you don't have to have the entire line selected. This also retains the code comment formatting/spacing, which is not retained when you manually cut and paste a code comment (thanks Juanita!)
  • Copy a line(s) of code above or below - Ctl + Alt + UpArrow copies the currently selected line(s) above the current selection. Ctl + Alt + DownArrow copies the currently selected line(s) below the current selection.
  • View all classes that implement an interface - select the interface and press Ctl + T to view a list of all classes that implement or extend the selected interface. Very handy when using frameworks like Spring or Tapestry that use interfaces instead of concrete classes.
  • View all references to the selected construct - Ctl + G will display all references to the selected class, interface, method, or variable. Ctl + g will display all occurences of the selected construct. Very useful for finding all the places that a method or variable is being used.
  • Drill down into a method or variable declaration - Click anywhere in the method or variable name, and press F3 to navigate to its definition. Press Alt + Left Arrow to navigate back.
  • Organize import statements and remove unused imports - Press Ctl + Shift + O (works recursively if you select a project or folder).

Java Course - Creating a Java Application with Eclipse

This free online course will walk you through creating a Java project in Eclipse, including compiling, packaging into a jar file, and running the application from the command line. It assumes you have already installed Eclipse on your system and have completed the Eclipse Tutorial - Installing and Running Eclipse.

Creating a Java Project

A Java project in Eclipse will contain all of the .java class files that our application needs, along with any .jar file libraries that our application might depend on.

Start Eclipse and select File - New - Project from the main menu. You'll see the New Project wizard where you'll select Java Project. We'll choose the name assign01, since this is the lesson exercise I give students on the first week of class.

Click "Finish" to accept the remaining default settings for a new Java project.

Creating a Java class

We'll create a math utility application that calculates the average value of a set of numeric values. Nothing complex, but enough to gain a good understanding of how to use Eclipse to create and package a Java application.

Select File - New - Class from the main menu. You'll see a New Java Class wizard which will ask for the name and location of the new source code file. Enter MathUtil as the file name and cis as the package (short for Computer Information Systems). NOTE: Be sure to check the option to create a public static void main method, since this will be the entry point to our application when we run it from the command line.

Eclipse Tutorial - Installing and Running Eclipse

This free online tutorial will walk you through the steps required to install and run Eclipse 3.2.1; the state-of-the-art Integrated Development Environment (IDE) that can be used for multiple programming languages, the most popular being Java.

You'll first need to download the Eclipse zip file from http://download.eclipse.org/eclipse/downloads/ and install it by unzipping it to the root of your hard drive. This should create a C:\eclipse folder on your system.

Eclipse is a memory hog, so you’ll need at least 512 MB of RAM on your system, and a minimum of 1 GB is recommended for a development workstation.

Launching Eclipse

Double-click C:\eclipse\eclipse.exe from Windows Explorer to launch Eclipse for the first time. It’s helpful to create a desktop shortcut to this file to simplify future launches.

Eclipse requires a Java Runtime Environment to be installed on your system, and is not included in the Eclipse download. If you do not have Java installed on your system, you will receive the following error message (Fig 1). To resolve this issue, download and install the Java 1.4.2 SDK from Sun at http://java.sun.com/j2se/1.4.2/download.html (about 55 MB).

Fig 1 - Java Runtime Environment Required to Launch Eclipse

When you run Eclipse for the first time, it will prompt you for a default workspace where it will store your projects (Fig 2). You can accept the default, but I recommend using something simpler like C:\Projects. This will come in handy later on when we try to compile or run our application from the command line.

Syndicate content