Getting a simple JDBC connection with Java
At this point I needed my IDE and J2EE server.
For the SCJP certification I used Tomcat. It was really easy to use. A fully integrated environment was not recommended because the point was to learn how to do everything by hand. Ok, been there, done that. Instead, for this, Ill be using Glassfish as my server and Eclipse.
Eclipse Ganymede is here.
Again, Im not really doing this to explain to everybody step for step what I did. Its more for my own notes.
First I installed the latest Ganymede (the J2ee server installation comes in the next step)
I created a new project.
I created a new java project and added the mysql jar to the build path.
(I keep my jars in a common /lib directory….however it is usually better to have a separate lib directory for each project. This comes in handy if you are working on two separate versions of the same code.)
package com.workout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import java.util.TreeSet;
/*********************************
* Simple test to connect to mySQL and get some data.
* @author Robert Stave
* see www.robthefiddler.com for the rest of this code
*********************************/
public class DoQuickJdbcStuff {
static String theQuery = "SELECT e.name, mg.muscle_name, emg.primary_muscle "+
" FROM exercise e INNER JOIN exercise_muscle_group emg " +
" ON e.exercise_id = emg.exercise_id"+
" INNER JOIN muscle_group mg"+
" ON mg.group_id = emg.group_id" +
" ORDER BY e.name,emg.primary_muscle;";
public static void main(String [] args) throws SQLException{
String url = "jdbc:mysql://localhost:3306/";
String dbName = "workout";
String driver = "com.mysql.jdbc.Driver";
String userName = "user";
String password = "user";
Connection connection = null;
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
//Really not a lot to it. Get the connection, execute the query.
Statement select = connection.createStatement();
ResultSet result = select.executeQuery(theQuery);
//For some formatting
Set<String> h = new TreeSet<String>();
while (result.next()) {
String ex = result.getString("name");
String mn = result.getString("muscle_name");
String pri = result.getString("primary_muscle");
if (!h.contains(ex)){
System.out.println("Exercise:" + ex);
h.add(ex);
}
//only print the exercise once
if ("1".equals(pri)){
System.out.print(" Primary " );
} else {
System.out.print(" Secondary " );
}
System.out.print("muscle:" + mn + "n" );
}
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
} finally {
connection.close();
}
}
}
To run the program just right click in the code and select the “run” option.
If you cant connect to the database, make sure the driver is in the path
The results are:
Connected to the database Exercise:Barbell Bench Press Secondary muscle:Shoulders Secondary muscle:Back Secondary muscle:Triceps Primary muscle:Chest Exercise:Barbell Squat Secondary muscle:Glutes Secondary muscle:Lower Back Secondary muscle:Hamstrings Primary muscle:Quadriceps Exercise:EZ Bar Bicep Curls Primary muscle:Biceps Exercise:Leg Extensions Primary muscle:Quadriceps Exercise:Lying Leg Curls Primary muscle:Hamstrings Exercise:Seated Barbell Military Press Secondary muscle:Triceps Secondary muscle:traps Primary muscle:Shoulders Exercise:Seated Dumbbell Press Secondary muscle:Triceps Secondary muscle:traps Primary muscle:Shoulders Exercise:Tricep Cable Pushdown Secondary muscle:Chest Secondary muscle:Shoulders Secondary muscle:Back Primary muscle:Triceps Exercise:Tricep Dumbbell Kickbacks Secondary muscle:Shoulders Primary muscle:Triceps