Pages

Showing posts with label JDBC. Show all posts
Showing posts with label JDBC. Show all posts

November 9, 2014

Implementing Derby in Greenfoot’s Jar Deployment: Tetris Game

Hello, this November should be called database month in my blog! I’ll post databases related technology such as MySQL, Derby, etc (from {Java} developer’s point of view) to familiarize myself with such technology so I can learn more advanced technology, such as spatial database technology.

Simple greenfoot tetris game
While there are so many libraries out there on the internet that you can use within greenfoot, this post will be the implementation of embedded derby in your greenfoot scenario deployment and a little of its problem. A little knowledge about greenfoot (2.4.0), jar packaging, and embedded derby will be helpful when reading this article. I also post my tetris game distribution which you can download below this post.



Imagine a situation where you have developed from scratch, a tetris game in greenfoot, and then you said to yourself,

“Hmm, I’m too lazy to write down the score saving code manually, I should use derby instead.“

If you want to utilize derby or any other libraries in greenfoot IDE, the IDE has to know where the derby library is and load the derby library’s driver from there. For that, you need to do:

Set the classpath in greenfoot environment by:
  1. Clicking edit -> preferences.
  2. In libraries tab, in user libraries, click add and enter the location to your derby.jar files .
  3. Finally, restart greenfoot.
Then, you can connect to the database via JDBC through derby’s driver. The path to the database will begin from your greenfoot scenario’s root folder.

Problem with Deployment

While it is relatively easy to use derby to create and access database right away in your greenfoot scenario, it’s not the same if you deploy it into a jar file.

You see me losing the game..
If you deploy (or ‘export’) your scenario and run it as is, all of your database related code will be useless. I don’t know the exact problem, but what I do know is that I’ve never saw any ‘jar in a jar’ files in any Java application’s jar. Maybe, your game can’t access the jar files when it is a jar file by itself and thus, the driver will never loaded.

But, worry not--remembering the Java packaging/deployment tutorials, I’ve decided to put derby library (jar) in the same directory as the game’s jar file (not inside the jar) and set the classpath in the game’s jar manifest file to include the derby library.

Derby’s database system and your database folder, will belong in the directory (root directory) that contain your game and derby library’s jar files.

These are the highscore which is saved using derby library
Finally, besides your game’s jar file and the derby’s library, you should put the (pre-generated) database in your game distribution.

The final file structure of your distribution more or less should look like this:

[your game directories] /
   |--- [your game].jar
   |--- derby.jar
   |---derby.log
   |---database /
     |---[your database]

To summarize things up, you can use external libraries in greenfoot by addressing the libraries in the preferences. After you deploy your greenfoot scenario into jar file, you should put the library besides your scenario jar’s file (or database folder) and tell your game’s jar file manifest where the library is.

Tetris game: https://drive.google.com/file/d/0B27ziWaCDimLenNKNkZVYWNwdms/view?usp=sharing


November 2, 2014

MathQuiz: A little experiment using embedded Derby database.

Ok, so I've decided to write down my blog in English language to make my writings more available to the wide audiences of the Internet. This post will be my first attempt to write a post in English and also my first attempt to utilize (embedded) Apache Derby technology in my Java code. All of the resoure(s) I've put on this post are available for download below this post.

Introducing MathQuiz
MathQuiz is a very thin, Java (using JDBC) game that utilize embedded Derby database to save the player's score. It consist of only 1 source (java) file, and can only be played in command prompt interface.

The game (or should I say, quizzes?) is very straight forward: you run it, answer ten (10) mathematical problems: each will give you 1 point if answered correctly, and then save the score with your name in the database. You can download the game and the sourcecode using the link provided below this post.

The database and its table are pre-created using the ij tools from the Apache Derby distribution. The one and only table 'scores' consist of two columns, labelled username and score.
Below, using the ij tools, you can see me connecting to the database and retrieve all of the rows from the table 'scores'.


This game is part of my learning process: Learning JDBC, Apache Derby, Ant, SQL, and other usefull (database) technology, I plan to integrate it into the eBookManager project I've reviewed in this blog. As student in Department of Geography who learn GIS and stuff's, its even become more important.

Sourcecode: https://drive.google.com/file/d/0B27ziWaCDimLdnN1eXFlbVlCMEU/view?usp=sharing