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.
We will create a second class (not included in the system) named DBConnectorTest to test if DBConnector works.
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.