java

String Split() with special character

Posted in java on February 28th, 2011 by Aditya – Be the first to comment

Being an Information Systems Management, I have not been really exposed to much nitty gritty details of programming – even on the language that I am exposed to the most – Java. Yes I have done my fair share of debugging and such, but even so I often stumble in some random stuff that waste quite a number of minutes. For example, with String split() method that I was worked with. read more »

How to Solve GWT IncompatibleRemoteServiceException Error

Posted in gwt, howto, java on February 27th, 2011 by Aditya – 3 Comments

I started GWT development few days ago and encounters these rather unfortunate stack trace error.

com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: This application is out of date, please click the refresh button on your browser. ( Expecting version 6 from server, got 5.)

read more »

Configure Managed Bean with XML

Posted in java, jfs on February 8th, 2011 by Aditya – Be the first to comment

I jot down this note from one of the JFS tutorial by MKYong. Just in Case I forgot about it.

With XML configuration, you can use the old JSF 1.x mechanism to define the managed bean in a normal faces-config.xml file. read more »

Customize Android ListView via ListAdapter

Posted in android on August 29th, 2010 by Aditya – 5 Comments

Often I find situation where typical listview layout provided by Android’s Hello Listview tutorial is barely adequate. For instance, it does not mention anything about ListAdapter and most of the time I want to get a list that goes beyond one line text. Icon and several textfields are the norm. In this tutorial I will discuss how to create Listview that can be customized with one icons and two text fields via a ListAdapter that is customized just for that particular view. read more »

Create Dummy Progress Dialog in Android

Posted in android on August 27th, 2010 by Aditya – 2 Comments

Sometimes you just want to make an android app prototype with minimal logic embedded on it. Or sometimes your UI ‘designer’ just being **** and want some ‘loading’ indicator’ to do whatever he want before creating documents which I will never see anyway. In fact there are many reasons not to develop your fully functional application for user testing. In this tutorial, you will see how to create a dialog progress bar which is literally a fake progress bar.
read more »

How to get current date time in String

Posted in java on August 25th, 2010 by Aditya – Be the first to comment

Stupid
read more »

Customize Android TabHost TabWidget

Posted in android on August 22nd, 2010 by Aditya – 3 Comments

Sometimes I want to simply make a bit of adjustment to default tab in Android UI. Instead of something dreadful, to something more cheerful like red or such. Here is some way to customize it.
read more »

Declare Global Variable In Android via android.app.Application

Posted in android on August 14th, 2010 by Aditya – 2 Comments

I am creating an application which requires login. The problem comes when I want to store the application state (e.g. whether user has login or not). Storing state in activity class is almost it is hard to make it persistent. For example, rotating the screen will literally wipe out the application state. Also when the phone keyboard slides the login form appears again and again.
read more »

Get One Random SQL Row From A Very Huge Table

Posted in java on August 1st, 2010 by Aditya – Be the first to comment

I have a very huge SQL table (5m entries and keep increasing) and want to select one row of entry at random. Typical method of using one SQL statement is apparently so slow. The typical method is by using the below statement by the way.

SELECT <column_name/*> FROM <table_name> ORDER BY RAND() LIMIT 1

while the above statement works fine on smaller table – generally 10000 records and below (depending on your processor speed), when its confronted with huge table, the statement just could not make it. Just to give you some imagination, I start using one of those statement and while waiting for the result, I can go to vending machine, grab a coke and come back, finish half a bottle of coke before stopping the process out of frustration.

After much tweaking and such, I realized that some statement are more size-conscious than the other. The trick is that using several statements which are less size-conscious to gather much needed parameter to assist statement that are more size conscious by increasing its … specifity (TODO: rephrase this paragraph)

I implement these principles using Java and the result can be seen below

public synchronized PhotoBean getRandomPhoto() throws DAOException {
        Connection con = null;
        try {
            con = getConnection();

            PreparedStatement pstmt1 = con
                    .prepareStatement("SELECT max(`id`) AS max_id FROM photos;");

            ResultSet rs1 = pstmt1.executeQuery();

            //this to to null-proof the logic
            int max_id = 22101985;
            int randomId = 22; // put some real ID just to make sure

            while (rs1.next()) {
                max_id = rs1.getInt("max_id");
            }

            Random randomGenerator = new Random();
            randomId = randomGenerator.nextInt(max_id);

            PreparedStatement pstmt2 = con
                    .prepareStatement("SELECT * FROM photos WHERE `id` >= ? ORDER BY `id` ASC LIMIT 1");

            pstmt2.setInt(1, randomId);

            ResultSet rs = pstmt2.executeQuery();
            PhotoBean tmpPhoto = new PhotoBean();
            while (rs.next()) {
                tmpPhoto.setPhotoId(rs.getString("photo_id"));
                //... more
            }

            //ps: you can use just one pstmt and it will be no problem as its executed separately but i am just too lazy
            pstmt1.close();
            pstmt2.close();
            releaseConnection(con);

            return tmpPhoto;

        } catch (Exception e) {
            //... more

    }