About Me

It's me, Arif N. In this blog I'll write about my adventure related to computer, programming, and anything that I found interesting. I wish you a happy reading.. :D

Category: Programming

Howdy.

Not so long ago, I created a GUI for data storage using Java and of course JDBC. I followed a tutorial about how to insert database records to the database table. The tutorial said that before we’re inserting new record to the database, we should generate unique key as record identity. The generation process handled by Java code. The tutorial use System.currentTimeMilis() function to create the key. So, the code will be look like this:

//set new id
Number time = System.currentTimeMillis();
Integer id = (time.intValue()/10000);

The generated key then being inserted to the database.

As you may realized, database systems usually have their own key generation technique. For example AUTO_INCREMENT attribute in MySQL. I followed the tutorial and sure it works. But, I tried database-generation key, and it works too. So, why we should bother generate our own keys?

The O’Reilly Java Author gave an explanation about that. They said:

However, using the supported key generation tools of your database of choice presents several problems:

  • Every database engine handles key generation differently. Thus, it is difficult to build a truly portable JDBC application that uses proprietary key generation schemes.
  • Until JDBC 3.0, a Java application had no clear way of finding out which keys were generated on an insert.
  • Automated key generation wreaks havoc with EJBs.

I got the point that the application should be portable. Who knows that someday we’ll migrating from one database systems to another? So the database generation key will be hard to control. 

Another detailed answer came from Scott Selikoff. He wrote a complete article about database key generation and gave an example:

Now, let’s say a user is in the process of creating a new record in your system. For each user record, you also have a set of postal addresses. For example, Bob may be purchasing items on NewEgg and have a home address and a work address. Furthermore, Bob enters his two addresses at the time he creates his account, so the application server receives the information to create all 3 records at once. In such a situation, you would normally have 3 records: 1 user record for Bob and 2 address records. You could add the address info in the user table, although then you have to restrict the number of addresses Bob can have and/or have a user table with a lot of extra columns.

Inserting Bob into the user table is straight forward enough, but there is a problem when you go to insert users into the address table, namely that you need Bob’s newly generated User Id in order to insert any records into the address table. After all, you can’t insert addresses without being connected to a specific user, lest chaos ensue in your data management system.

The problems will arise in complex database relationship. As we don’t know the generated key, we couldn’t set the foreign key of corresponding tables. I didn’t realize this because the GUI I made was using a simple database design. Maybe with no table relation at all.

So, I got my question answered. Do you have another answer for my question? Feel free to share.

Howdy.

One day, when I surfed the web, I found an interesting site. This site has function like a normal blog, but the post written there was posted from various blog sources. Later, I knew that this blog is called aggregator. How this blog aggregator works? It works by collecting RSS Feeds from various blogs registered. The feeds then posted as normal posts in the aggregator. One of the differences is the article title linked to the “real” blog source.

Ilkomerz 41 Blog Aggregator

Ilkomerz 41 Blog Aggregator

When I knew that some of my friends also have their own blogs, I started to thinking about building this aggregator. The site that I found earlier is powered by Planet, a Phyton-based feed reader. I tried it, and ended up with failure. I just couldn’t configure and tested it properly. I didn’t have Internet connection and didn’t familiar with Phyton at that time. So, I tried to find alternative. I found WP-o-Matic plugins for WordPress. So I installed, asked my friends permission to grab their feeds, and hosted it. I called it Ilkomerz 41 Blog Aggregator.

Another problem arose. This plugin will automatically read and parse feeds from its registered blogs every amount of time. To make this fetching run automatically, it uses cron job. The WP-o-Matic has two options of cron job. The first one is UNIX cron job run by web hosting. Free hosting, like the one I use, doesn’t give cron job feature. So I go to the next option, web cron. It will automatically calling fetching script on the hosting.

I use the web cron option for some months until suddenly the aggregator was down. Well, it worked again when I send a support ticket to the hosting provider. I know that maybe it’s not because of the web cron spending too much resource. I suspected that because of the multi-user nature of the aggregator, the blog automatically sending email to the blog writer. The hosting may called it spam. So I killed the auto-email feature. I also shut down WP-o-Matic and switched it to manual fetching. I know that maybe it’s not because of the WP-o-Matic’s web cron, but I don’t wanna take the risk losing this blog for the second time.

Manual fetching was a pain. I must open my Google Reader, find if my friends have new post, and then login to the aggregator, fetching the post, and the post will showed up. I was thinking, there must be a better way to do this. Accidently, I found out that there are some webs that provide free cron job. One of them is SetCronJob. This site can calls the cron script url of the aggregator. So, I tried it yesterday. I registered the aggregator to the site. And now, my aggregator works well automatically. I don’t know what will come in the future, but I have a high hope for SetCronJob.

Do you have another opinions or experience? Feel free to share it.

Howdy,

People are good at learning by examples. It’s just like when one of my friend asked for my help. Her clients want to make an interface for data entry using Access (you can contact me if you need one ^^). They will use it to store survey data. They gave her an example from the last survey, hoping that she will make the new one based on that example. In fact, the new interface must exactly like the previous (or the example given).

So, we created the visual interface first. After it done, we must add some functions that prevent accidently alter to data entered. I must admit that this is my first experience writing code in VB. Have no choice, I dug deep into the example code. Not so long after I found this code:

Public Sub closeRecord(txt1 As Boolean, txt2 As Boolean, txt3 As Boolean, txt4 As Boolean, txt5 As Boolean, txt6 As Boolean, txt7 As Boolean, txt8 As Boolean, txt9 As Boolean)

idResponden.Locked = txt1
noPlot.Locked = txt2
Luas.Locked = txt3
JenisTanaman.Locked = txt4
Umur.Locked = txt5
Kecamatan.Locked = txt6
Desa.Locked = txt7
Dusun.Locked = txt8
Catatan.Locked = txt9

End Sub

This code used to set the appropriate text field’s status so that the entered data can’t be altered. Not until the user click an edit button. If the user click the edit button, the locked status will be set to False and the data can be altered.

This closeRecord function has nine parameters (really long for a function right?). Every single parameter set status for one text field. So if the interface has twelve text fields, the function will catch twelve parameters. See another function that called the function from before:

Public Sub RecordOff()

closeRecord False, True, True, True, True, True, True, True, True

End Sub

How many True parameters are there? It’s not my place to judge this is a bad code or not. I was just thinking about another variations from the function.

The first variation that came to my head was setter-like function in OOP. Assuming that a text field is an object, we can split the function into nine function. Every function sets their own text field. It will looks like this:

Public Sub closeIdResponden(status as Boolean)

idResponden.Locked = status

End Sub

And all the other eight function for all of the text fields. It offers more flexibility. Just in case if we want to set just one of the text fields and not all of them. But, it’ll be less effective if we don’t want to set just one text field in the whole program. For this case, the function above will be called like this:

Public Sub RecordOff()

closeIdResponden False
closeNoPlot True
'and all the other seven

End Sub

Then, I came up with another idea. According to the function uses toward the whole program, all of the text field except the first was set to the same status. So, why we don’t just set the other eight text fields. Here is the code looks like:

Public Sub closeRecord(status As Boolean)

idResponden.Locked = False
noPlot.Locked = status
Luas.Locked = status
JenisTanaman.Locked =status
Umur.Locked = status
Kecamatan.Locked = status
Desa.Locked = status
Dusun.Locked = status
Catatan.Locked = status

End Sub

So it will just set the other text field well. The flexibility of the code will decreased, but it is okay for the whole program.

The point here is if you could decreased the flexibility as long as it will make the code more effective and didn’t violate the whole code. Just like the old saying, the right man for the right job. It’s just another function to me, what about you?

Days ago, I accompanied my sister to rent some DVDs at one place. We didn’t have any account yet, so we decided to register as a new member. Because of my sister working outside town, she asked me to use my id as the registration id. So then, the clerk gave me a form and asked me to write down my identity there. After finished, I must sign the form.

My sister did the writing work while I was wandering watched the movies. After she finished, I got back to her and read the form. Below the entry, I found some terms of services and agreements. I decided to read it. The clerk was still waiting for my sign. Impatient, she told me to sign it right away. She even asked me why I don’t mind spending time reading the TOS and agreement. I replied to her that I need to know the agreement before I signed it. What if there were some points in the agreement that I didn’t agree? From the why she acted, I knew that no one bother to read the agreement first. And I’m pretty sure of it.

License agreement

License agreement

The situation made me think. Did everyone really care about agreements, terms of service, or maybe instruction manuals? We met them at various places and occations. Like when we’re installing software(s), do we read the licence agreement? When we’re using social networking services, do we read the terms of service and the privacy policy? When we’re using new hardwares or electronic stuffs, do we read the instruction and safety manuals?

If you’re dealing with software developments or services, thing like this become very important. You can’t just ignore this. If you done wrong with this kind of things, you may get yourself sued, wether by the service providers or by your own clients (Remember the Facebook case?).

So, from now on, I encourage you, and myself, to becareful about something that we didn’t read yet. Go and start read the unread.

Hi there..

Long time ago, I got some challenge to create some MDI (Multiple Document Interface) application. Why I called it challenge? Because I must create it with Java and I didn’t have experience on using Swing. But thanks to Netbeans, it makes Swing gui development far easier.

Netbeans provided some tools for gui building. You just need to drag-and-drop, edit some properties, and adding some event-handling. Voila, your desktop application will be up and running. But there is a but. In MDI application, we’ll need a scrollable desktop pane. For your information, desktop pane is a home for all of child windows. If I dragged the child window outside the desktop pane, the desktop pane should automatically give scroll to facilitate it. Java have library to implement dekstop pane. It’s called JDesktopPane. But the usual JDesktopPane didn’t have support for auto-scrolling.

Gerald Nunn in Java World site write a way to conquer this deficiencies. He create some libraries that we can use to enhance the usual JDesktopPane. He called the object MDIDesktopPane. All we have to do are just download it, link it to our codes, and use it.

JFrame with MDIDesktopPane preview

JFrame with MDIDesktopPane preview

In Netbeans, these were I did:

  1. I extracted the sample code (library) into the same package with my gui package.
  2. Using gui builder tools provided by Netbeans, I created JFrame and inside it, I added JMenuBar, JItem, and JMenuItem as necessary.
  3. Using gui builder, Netbeans will generate function called initComponents. This function lists all of the variable declarations and gui setups for components added before.
  4. In the JFrame constructor, the initComponents function will be called. In this area, we will added the MDIDesktopPane init.
  5. Before that, I created MDIDesktopPane declaration. It will not far from this:
    private MDIDesktopPane desktop = new MDIDesktopPane();
    private JScrollPane scrollPane = new JScrollPane();

    We need JScrollPane because it will support the scrolling functions.

  6. Then, after the initComponents function, I added the MDIDesktopPane and the JScrollPane to the frame.
    jMenuBar1.add(new WindowMenu(desktop));
    scrollPane.getViewport().add(desktop);
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(scrollPane,BorderLayout.CENTER);

    FYI, jMenuBar1 was the variable name of my JMenuBar. It means I added WindowMenu of the MDIDesktop to the menu bar. WindowMenu was a class to control the display of all of the child menu like cascaded or tiled windows.

  7. It’s done. It’s simple, right.

With this MDIDesktopPane and WindowMenu class, I finally conquer the scrollable desktop pane. Thanks to Gerald Nunn for his great class.