Sunday, September 28, 2008

Getting a single DB connection in Java

Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@[ip_address]:[port]:[SID]", "[username]", "[password]");

Tuesday, September 23, 2008

Changing the Date/Time on Linux

Changing the timezone to Johannesburg:
$ ln -s /usr/shar/zoneinfo/Africa/Johannesburg /etc/localtime

Changing the date/time:
To change the date/time use the following syntax:
$ date MMDDhhmmYYYY
where MM is the month, DD is the day, hh is the hour, mm is the minute and YYYY is the year.

Updating the hw clock:
$ hwclock --systohc

Checking the hw clock:
$ hwclock --show

To maintain the timezone through a restart you may need to change the /etc/sysconfig/clock file.

Binary Tree Traversals

One of the important features of Genetic Programming is the tree structure which is the primary method of representing an individual. Understanding binary tree traversals is therefore required. The 3 methods of traversing a binary tree are Preorder, Postorder and Inorder traversal.

Preorder Traversal:
begin
   if tree=null return;
   print(tree.root)
   preorder(tree.leftsubtree)
   preorder(tree.rightsubtree)
end

Postorder Traversal
begin
   if tree=null return
   postorder(tree.leftsubtree)
   postorder(tree.rightsubtree)
   print tree.root
end

Inorder Traversal
begin
   if tree=null return
   inorder(tree.leftsubtree)
   print(tree.root)
   inorder(tree.rightsubtree)
end

JSESSIONID on URL causes an issue for the SE P1 handset

A JsessionID on the end of a download URL to a piece of content seems to cause a problem for the Sony Ericsson P1 handset e.g. file.3gp;jsessionid=xxxxx. The handset doesn't seem to recognise the format of the file and gives an error "There is a problem opening the file. Try Again?". The installation message sent back to the handset though is a 900 Successful Installation. Oddly if DRM is applied to the content (which will then be a .dm or .dcf file), the JsessionID is not really a problem. Removing the jsessionid from the URL to the content fixes the problem described above.

952 device aborted error

SE K750i gives a 952 device aborted error when downloading content if there is not enough space on the memory card. The handset shows the message "Operation Failed". This error is not very intuitave as, if it is a space issue the handset is supposed to send a 901 error. The handset had enough space on the handset itself so this is probably why a 901 error isn't the installation message. So identifying the actual problem was quite difficult. Deleting some of the items on the memory card solved the issue.

Monday, September 22, 2008

Installing Imagemagick on Red Hat

Download the latest ImageMagick. If you need to manipulate jpegs the jpeg libraries may not be installed on your OS by default so you will need to download the jpeg source.

Uninstall the current ImageMagick (if installed):
$ make uninstall
$ make clean

To install the jpeg source:
$ tar -xzvf jpegsrc.vb6.tar.gz
$ cd jpeg-6b
$ ./configure --enable-shared=yes
$ make
$ make install

To install ImageMagick (in this case v 6.4.1 is being used):
$ tar -xzvf ImageMagick-6.4.1-0.tar.gz
$ cd ImageMagic-6.4.1
$ ./configure --enable-shared=yes --disable-static --without-perl
$ make
$ make install

Testing the installation:
$ convert test.jpg -resize 50% half_sized_image.jpg

Going through a file "line by line" with a shell script

cat $FILENAME | while read i
do
#do something here with each line e.g.
echo $i
done

Running Unix Commands from java

Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec(new String[] {"/bin/sh","-c","sh myScript.sh"});
child.waitFor();
int return_code = child.exitValue();

Using a script to get files via FTP

(echo "user username password
binary
prompt
cd /where_the_file_is
mget $FILENAME
bye" | ftp -n $systemID)


where $systemID is the IP address of the system.

Oracle SQL - String replace

Updating String fields using the replace function:

update [table_name] set [field_being_updated]=replace([field_being_updated], '[string_to_be_replaced]', '[replacement_string]')