Apr 23 2006

Hello World Cocoa

Want to be a Mac Developer? Well, OS X and Xcode makes it easy to start hacking your ideas into Apple applications. I started to ‘write’ a Hello World program using Xcode. In less than five minutes and no coding at all I was able to create what I had in mind:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   // insert code here...
   NSLog(@"Hello, World!");
   [pool release];
   return 0;
}

The above code was auto-generated for me by Xcode. Now only if Xcode could auto-generate the code for a new killer app that I been dreaming of.

I also want to note that the above code is a command line, Foundation Tool, application.

The @”Hello, World!” piece of code is a NSString literal. You night also have noticed that there are a lot of types that start with NS. The NS prefix stands for Next Step which is where Cocoa first started out.

Technorati Tags: , , ,


Apr 20 2006

Gnarly Custom Groovy Closures

In a previous post I went over Groovy closures. In this post I will show how you can extend a Java class so as to use it to construct a closure in a Groovy script. I’ll start by defining a plain old Java class with an array of data.

public class Sentence {
    public String[] getWords() {
        return new String[]{"Hello", "World"};
    }
}

You will need to write a Sentence helper class that will iterate over the words and call the Groovy closure. Here is a sample implementation:

public class SentenceClosure {
    public static void each(Sentence s, Closure c) {
        String words = s.getWords();
        for(int i=0; i < words.length; i++) {
            c.call(words[i]);
        }
    }
}

Once you compile the above classes and add them to your classpath you can write a Groovy script such as:

def sentence = new Sentence();
use(SentenceClosure.class) {
    sentence.each {
        println " -> ${it}";
    }
}

At this point you will notice that in the Groovy script it looks like the class Sentence defines an each method but that method is actually implemented in the SentenceClosure class. The SentenceClosure helper class allows you to separate closure specific code from you business class.

Technorati Tags: , , ,


Apr 20 2006

MySQL Import Export

I recently migrated one of our MySQL 3 databases to MySQL 5. I needed to export data from MySQL 3 and import it to MySQL 5. To export the data I had to do the following:

mysqldump -u username -ppassword database_name > FILE.sql

FILE.sql is just a ascii file with create and insert SQL statements. You can import this file like this:

mysql -u username -ppassword database_name < FILE.sql

I read about MySQL Migration Toolkit 1.0 but I had trouble using it. When I used the Migration Toolkit the JVM would crash and produce a hs err log file.

Technorati Tags: , ,


Apr 20 2006

Show SQL Tables

When working with a database sometimes you will want to look up all the available database tables. To show the available tables in SQL Server you can do the following:

select * from information_schema.tables;

In Oracle you can use any of the following statements:

select * from user_tables;
select * from all_tables;

In MySQL I have been using the following since MySQL 3:

show tables;

But in MySQL 5 you can use the following:

select * from information_schema.tables;

Apr 11 2006

Class HighLite File

Many people might be familiar with the File class. You need a File object for many of Java’s IO operations. In this Class High Lite article I cover some of the File’s least known methods and fields.

Please note that in all cases be sure to read the Java documentation, as it is the best available source of information regarding the Java API. You can also always take a pick into the source code provided with the Sun JDK.

I often see “\\” in strings to separate directory and a file names. It is strongly suggested to not use operating dependent strings to separate directories and files. The backward character is a Windows specific way to separate a directory hierarchy, while Linux and Apple use a different character. To avoid using hard coded strings to concatenate a file path use the File’s separator field. The value of separator contains the correct file separator character for your operating system.

The listRoots method is a convenient way to to access all the available drives available in your system. This method might be helpful to enhance the default JFileChooser and have a drives drop down list. This method returns an array of File objects contain the root letter drives, your file system roots.

Some people procrastinate, but does their code procrastinate also? The deleteOnExit method waits until the JVM terminates to delete a file. Be sure to read the JavaDoc. According to the JavaDoc 1.4.1, “Deletion will be attempted only for normal termination of the virtual machine.” When your program exits, the file marked for deletion is removed, even if the program exits with an exception. As the JavaDoc states, the file will be deleted when the JVM exits normally, and an exception is normal termination of the JVM although not for your program.

Technorati Tags: , ,


Apr 10 2006

Sybase Transaction Log

One of the database vendors that my company supports is Sybase. We have a Sybase database which we hit from JUnit tests but every once in a while a Sybase specific JUnit test will just freeze. I found out that the JUnit test would freeze because the Sybase transaction log would fill up and need to be cleared.

To clear the transaction log open up Sybase SQL Advantage and log in. Once log in execute the following statement:

dump transaction <DATABASE NAME> with no_log

Once I run this command my JUnit tests continue without a hitch.

Technorati Tags: , ,