A Singleton Java class for MySQL DB connection

I generally use MySQL along with my Java applications, and re-use a Singleton class I created a while back to connect to the database. It contains insert and query methods, but apart from that, it’s pretty basic. Just thought it might be useful for someone if I post it up here.

import com.mysql.jdbc.Connection;
import java.sql.*;
import java.sql.DriverManager;
/**
 * @desc A singleton database access class for MySQL
 * @author Ramindu
 */
public final class MysqlConnect {
    public Connection conn;
    private Statement statement;
    public static MysqlConnect db;
    private MysqlConnect() {
        String url= "jdbc:mysql://localhost:3306/";
        String dbName = "database_name";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "username";
        String password = "password";
        try {
            Class.forName(driver).newInstance();
            this.conn = (Connection)DriverManager.getConnection(url+dbName,userName,password);
        }
        catch (Exception sqle) {
            sqle.printStackTrace();
        }
    }
    /**
     *
     * @return MysqlConnect Database connection object
     */
    public static synchronized MysqlConnect getDbCon() {
        if ( db == null ) {
            db = new MysqlConnect();
        }
        return db;

    }
    /**
     *
     * @param query String The query to be executed
     * @return a ResultSet object containing the results or null if not available
     * @throws SQLException
     */
    public ResultSet query(String query) throws SQLException{
        statement = db.conn.createStatement();
        ResultSet res = statement.executeQuery(query);
        return res;
    }
    /**
     * @desc Method to insert data to a table
     * @param insertQuery String The Insert query
     * @return boolean
     * @throws SQLException
     */
    public int insert(String insertQuery) throws SQLException {
        statement = db.conn.createStatement();
        int result = statement.executeUpdate(insertQuery);
        return result;

    }

}

Cheers!

Updating your HTC Wildfire from Android 2.2 (Froyo) to 2.3 (Gingerbread)

DISCLAIMER: I WILL ACCEPT NO RESPONSIBILITY WHATSOEVER IF YOUR WILDFIRE GETS BRICKED AS A RESULT OF YOU BEING A DAREDEVIL, OR SIMPLY BEING STUPID. ROOTING YOUR PHONE WILL VOID YOUR WARRANTY. I WOULD NOT RECOMMEND THIS PROCEDURE FOR THE FAINT OF HEART.

NOTE: This procedure will cause all your personal data on the phone such as contacts, messages etc to be lost. Data on the SD card will not be lost. I would suggest backing everything up before you start. There are some awesome tools on the Android Market such as Astro File Manager and SMS Backup and Restore which you can use to backup your applications and messages.

Thanks to the AWESOME people at Cyanogenmod, now you can have Android 2.3 Gingerbread on your HTC Wildfire! I also got some help from the Learn How To Hack blog, which was very informative.

Cyanogenmod incorporates a large amount of awesome tools and provides an authentic experience on your phone based on Gingerbread. I would recommend it to anyone who is tired of the dumb old HTC UI.

WARNING: HTC Sense will NOT work on your phone after you have installed Cyanogenmod. There is NO fix for this yet. (but on the other hand, who needs HTC Sense anyway? :P)

One of the sad things that happens to you is that if you upgrade your HTC Wildfire to Froyo (2.2), you can’t immediately install Cyanogenmod on it. So you need to go back to 2.1 first. Most documentation on this topic won’t tell you how to get back to 2.1.

DOWNLOADS SECTION

Before you start, make sure your phone is set to turn on Debugging mode when it’s connected to the computer. If you don’t know how to do that, I wouldn’t recommend continuing with this operation.

The first thing you need to do is to set your phone’s Security Flag to S-OFF. Doing this enables you to do a factory reset on the phone, and allows you to write to any partition. This is basically the first step towards rooting your phone. An easy way to get S-OFF is to use the Revolutionary tool which is built by Unrevoked and AlpaRev. Download the Revolutionary tool (it truly IS revolutionary), connect your phone on ‘Charge Only’ mode (you’ll have to have drivers installed and HTC Sync off if you’re on Windows) and run it. It’ll set your phone to S-OFF.

Download the USB Drivers for Windows. Make sure HTC Sync is disabled, turn off your phone. Boot it into HBOOT by holding down POWER BUTTON + DOWN VOLUME KEY. Your phone will boot in to the HBOOT interface (which is pretty much like a bootloader on Android). Connect your phone to the PC using the USB cable. The computer will search for drivers, at which point you should point out the drivers you downloaded earlier. They will install, and your phone will now be recognized by Windows as an ‘Android Bootloader interface’.

Now it’s time to go back to Android 2.1 Éclair. Download the RUU Android 2.1 Factory ROM for this, and run it with your phone connected. WARNING! At this point you will lose all your data such as contacts, messages and other personal data stored on the phone. Data on the SD card will not be harmed. Progress of installing Éclair on your phone will be shown both on the GUI interface on your PC and your phone itself. Soon it’ll be back to Éclair, but you won’t have to stare at that outdated desktop for long, because we’ll be moving on.

Now you’ll have to get the UnRevoked tool, which will help you to root your phone. PLEASE NOTE THAT THIS STEP WILL VOID YOUR PHONE’S WARRANTY. Simply download the tool, connect your phone and run it. It’ll set your phone to root.

The next step is to download Cyanogenmod 7 ROM for HTC Wildfire and Google Apps, which will get downloaded as ZIP files. Place these ZIP files on your SD card, and reboot the phone into HBOOT by holding down the POWER BUTTON + VOLUME DOWN BUTTON. In the HBOOT menu, use VOLUME DOWN to navigate through the options, and choose the ‘Recovery’ option using the POWER BUTTON. Your phone will go into ClockworkMod Recovery mode. Here, use the Volume buttons to navigate, but the TRACKBALL to select an option.

  1. Select Wipe data/Factory Reset. (you will get an ‘Are You Sure?’ page, just choose Wipe from the options)
  2. Select Wipe Cache Partition.
  3. Select Install ZIP from SD Card
  4. Choose the Cyanogenmod ZIP file you copied to the SD Card earlier and let it install.
  5. Select Install ZIP from SD Card again.
  6. Choose the Google Apps ZIP file you copied to the SD Card earlier and let it install as well.
  7. Reboot your phone. It’ll boot into Cyanogenmod!

Enjoy your new-found OS! 🙂

Sahana Agasti – Vesuvius

I thought of writing up a blog post related to my ongoing Google Summer of Code project, which is to re-implement the translation structure in Sahana Agasti’s Vesuvius release.

Sahana, as you may or may not know, is one of the most prominent HFOSS (Humanitarian FOSS) projects in the world, and is aimed primarily at disaster management. It was developed as a response to the December Tsunami in 2004 which devastated South Asian countries such as Sri Lanka, where Sahana originated. Sahana was brought forward at the time as an effort to co-ordinate the search for missing persons, manage shelters and the distribution of relief efforts.

Due to its robust structure and easy-to-deploy nature, Sahana has been deployed around the world as the primary disaster management system for most situations. The floods in Pakistan and India were mitigated with the aid of Sahana. The City of New York maintains its own installation of Sahana as a measure to manage disasters that could occur. Recently, Sahana was deployed in response to the tsunami in Japan as well.

Sahana is mainly comprised of 2 main projects: Eden and Agasti. Eden is written in Python as a desktop application that can help with co-ordinating the disaster relief effort from a centralized management point. Agasti is written in PHP and focuses on providing a more widespread relief effort through providing interconnection between a large number of disaster relief centers.

Agasti is in turn, made up of 2 parts: Mayon and Vesuvius. While Mayon focuses on statistical analysis and predictions, Vesuvius focuses primarily on finding missing persons. And it is Vesuvius that I’m doing my project on.

Vesuvius has a simple yet efficient structure which helps to set up a disaster event and co-ordinate efforts between a large number of triage centers over a large area very quickly. The most interesting thing about Vesuvius is that it can manage an advanced Access Control List that can include a large number of different types of users from Researchers to Administrators, each with their own set of privileges on-site.

The process of reporting a missing person on Vesuvius is pretty straightforward and self-explanatory. This design would help people with little experience in using Vesuvius.

My project focuses on making Vesuvius more easily translatable, so that it can be adapted for different disaster situations in different locales quickly and effectively. More on that later.