Java Revisited: A Tutorial for My Students Vol.1 Part 1

I have promised my former students that I would give a tutorial on Java because they wanted to learn more about it. I know that this has already been delayed but I hope this little tutorial series on Java/MySQL would still be helpful. =) If you find some errors/glitches in the code, please feel free to post a comment. Or if you have any suggestions on how to improve this, please let me know. Thanks!

First off, I’d like to let you know the tools and platforms that I am using.

– JDK 1.6
– NetBeans IDE 6.1
– MySQL (bundled with WAMP)
– HeidiSQL (a lightweight, fast, and flexible interface to MySQL)
– Windows XP

For this tutorial series, I would expect that you already have a working knowledge on Java and databases (specifically MySQL), and an understanding of object-oriented programming. We will start with a mini project entitled “Time Management System”. I have chosen this mini project for simplicity.

We have 4 tables in the system. Accounts, Acct_type, Logs, and Team. Using HeidiSQL, create a database named tms with this schema (will be posted on the flash widget: right side of My Lane page).

First, we will create a class for database connection and we will name it DBConnector.

/**
*
* @author Mylene E. Sereño
*/
import java.sql.*;
public class DBConnector {
private String url;
private String user;
private String password;
private String driver = “com.mysql.jdbc.Driver”;
private Connection conn = null;
public DBConnector(String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
}
public Connection connect() throws Exception {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
return conn;
}
}

We will create a second class (not included in the system) named DBConnectorTest to test if DBConnector works.

import java.sql.Connection;
public class DBConnectorTest {
private static DBConnector dbConn = new DBConnector(“jdbc:mysql://localhost:3306/tms”,”root”,””);
public static void main(String args[]) throws Exception {
Connection conn = dbConn.connect();
if(conn != null) {
System.out.println(“Connection to database established!”);
}
}
}

The url is jdbc:mysql://localhost:3306/tms. It indicates the protocol (in this case, mysql), hostname (localhost or IP address), port (3306 for mysql), and the database name, which in this case is tms.

If this code doesn’t work, please make sure that you have installed Java support for MySQL correctly. If you are not sure about it, please refer to this tutorial. I was able to get things right when I found Java’s lib/ext directory. Where? It really depends on your system. But in my case, the path looks like this: C:\Program Files\Java\jdk1.6.0\jre\lib\ext.

Download the current version of MySQL Connector/J from mysql.com’s Drivers and Connectors. If you are a Windows user, then you’d want to choose the .zip file. Expand the file and move the driver(mysql-connector-java-X.X.X-bin.jar) to the lib/ext directory from the previous step.

The values of the instance variables user, password and url of  DBConnector would depend on your system. It is of course, always advisable to make your systems secure so please avoid using root and do create a strong password for your MySQL instance. Or else, you’re in BIG trouble! Just for simplicity, I used WAMP’s default values.

Leave a comment... :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s