Getting MySql Working with JDBC in my Windows 7 Box

   I have been studying for the OCP Exam.  I passed the Programmer 1 exam and that one wasn't too difficult to prepare for.  Preparing for the Programmer 2 exam has proven to be a much bigger challenge.  It covers 12 areas with one of them being Building Applications with JDBC.   In my work experience, I have been exposed to a small subset of JDBC. That's mostly because we don't use the raw implementations and instead use a combination of other persistence frameworks.  I tried studying JDBC straight from the book to prepare for the exam and it's proven ineffective.  Studying theory with no hands-on JDBC is difficult.  There is simply too many unanswered questions. It also proves to be difficult because it is vastly different than anything I've done with ADO.net.

    So earlier this week I decided that in order to truly grasp JDBC, I was going to get some hands on practice. With this goal in mind, I figured the best option for a sandbox database system was MySQL.  For my IDE I decided to use Eclipse since that's what I've been using for quite some time already.  After a few days (a few hours here and there spread out over a few days) I have finally got MySQL installed on my Windows 7 box and have a small program running and performing some CRUD(create, read, update, delete) on the database that I created.  I ran into a few gotchas that I'd like to share, so that hopefully you won't run into these when you decide to try out JDBC with MySQL.

MySQL Is not Free Anymore?

   MySql is an open source Relation Database Management System.  It is still currently free.  It has been for a long time and as a matter of fact it has been one of the key elements for developing on the LAMP(Linux Apache MySql and PHP) stack for instance. However, it was a little confusing downloading it from the website.  Oracle owns MySql and on the MySql website you can actually purchase a license from Oracle.  This made me think that MySql was no longer available free of cost.  Fortunately I read this article and it led me to discovering that the community edition of MySQL is free.  It's a little tricky to find the right version, so you have to poke around the site a little bit to download the MSI installer.  Don't make the mistake that I made and download the zipped files because those don't include other applications such as the MySQL Workbench, which is quite handy by the way.

A Great Tutorial on Using MySQL with Java

   You can install MySQL rather easily by following the prompts you get along the way with the MSI installer.  Make sure you save your root password. You can follow the steps in the tutorial: http://www.vogella.com/tutorials/MySQLJava/article.html to get up and running.  The tutorial does a great job of getting you setup and running so I recommend it.  There is also a link within that article that has an introduction to using MySQL.  If you have and understanding of databases you should be able to do everything the tutorial requires you to do with the MySQL Workbench.

Where is that MySQL Connector JAR?

   Another thing that was a little confusing was that the MySql connector JAR is not installed where you would think it should be.  The MySQL install was a 64 bit install so MySQL installed in C:\Program Files and the Connector actually installed in C:\Program Files\x86.  Once I looked in the C:\Program Files (x86\MySql folder I was able to copy over the connector JAR file (mysql-connector-java-51.1.29-bin.jar).  The MSI provides the option to install the connector but after the install I couldn't find it.  So I then proceeded to download just the connector and installed it but then I couldn't find that one either.  Once I looked in the correct folder I found both connectors there.

The Tutorial Fails to Connect to my Database

4. One correction to the tutorial is that in order to connect to the database you must include the port so instead of
      connect = DriverManager
          .getConnection("jdbc:mysql://localhost/feedback?"
              + "user=sqluser&password=sqluserpw");

Add the port. You can verify the port from the MySQL Workbench (it may be different than mine, 3307)

      connect = DriverManager
          .getConnection("jdbc:mysql://localhost:3307/feedback?"
              + "user=sqluser&password=sqluserpw");


Well... hopefully this eases your pain in installing MySQL for playing with it and Java.  With this out of the way I can now begin playing around with all the possible concepts of Creating Applications with JDBC.  Please drop me a comment. I'm always interested in hearing your feedback. Kudos are also a good incentive to keep writing.

Comments

Popular posts from this blog

Simple Example of Using Pipes with C#

Putting Files on the Rackspace File Cloud

Why I Hate Regular Expressions