Way back in Unit 4 of M257, we looked at basic input/output stuff involving keyboard input and text files. It feels like months ago, and in my last Java course input/output was one of the topics I found hardest to get to grips with, so this week I’ve been concentrating on shoring up my java.io skills.

In particular, I’ve been experimenting with handling input/output via a GUI, since that wasn’t covered in Unit 4. It took me a little while to get the hang of dealing with the possible exceptions that are involved in working with files, but eventually I was able to get my GUI to display the contents of a text file in a JTextArea in response to a button-click.

Finishing that program seemed to really kick-start my memory of the IO material from M255, and I realised that I’d easily be able to adapt my program to read values from a CSV file rather than lines from a text file, by adding a useDelimiter(",") and changing the occurrences of nextLine() to next(). Incidentally, I still cringe every time I type in useDelimiter(","), since I completely forgot to use that particular method in the CSV-reading code we had to write in the M255 exam – I realised I’d omitted it about 5 mins after leaving the exam hall, and spent the journey home feeling extremely stupid.

Anyway, getting the CSV-reading program to work made me curious about how to read values from a database using Java, so I decided to adapt my code to read from an MS Access database. It took me quite a while to figure out that it was the JDBC API that I need to learn how to use, and to be honest when I first looked at the Sun tutorial on the topic, I felt quite overwhelmed – I had one of those “Am I smart enough to understand this?” moments.

So I took a break, and when I came back to it, I decided to look at some other tutorials on the subject; last time I got really stuck with a Java problem, I ended up finding a forum thread about the subject more helpful than the official tutorial, so I thought it was worth a try (although I was half-resigned to just accepting that JDBC was simply beyond me!). The main problem I had was figuring out what the url argument of the DriverManager.getConnection(url, user, password) method should be in order to connect to the Access database I was using – it seemed like I could find example of connecting to a dozen other kinds of databases, but not an Access example. Perseverance won out, though, and eventually I found a guide on Planet Source Code that showed me what the url needed to be:

public void createConnection()
        String filename = "C:\\Users\\Rob\\Documents\\Training.mdb";

        String db = "jdbc:odbc:Driver="
                  + "{Microsoft Access Driver (*.mdb)};DBQ=";
        db += filename.trim() + ";DriverID=22;READONLY=true}";
        Connection con = DriverManager.getConnection(db,"","");
    catch (SQLException x)
        System.out.println("Couldn't get connection!");

So I can finally display values from the tables in an Access database in my GUIs, making use of all that lovely SQL I’ve been learning at work recently! I’ve only used simple SQL statements in my Java programs so far, so the next thing I want to do is to experiment with executing more complex queries, and particularly to learn how to get the parameters for the queries from the GUI input fields. Then once I’ve done that, I think it’ll be time to get back to Unit 8, and hopefully learn some useful threads-related stuff that I can incorporate in my database-GUI experiments…