How to Handle Exceptions in EJB

August 28, 2003 on 8:29 pm | In Java | 1 comment

From the article on DeveloperWorks.

Principles of exception handling

The following are some of the generally accepted principles of exception handling:

1. If you can’t handle an exception, don’t catch it.

2. If you catch an exception, don’t swallow it.

3. Catch an exception as close as possible to its source.

4. Log an exception where you catch it, unless you plan to rethrow it.

5. Structure your methods according to how fine-grained your exception handling must be.

6. Use as many typed exceptions as you need, particularly for application exceptions.

Point 1 is obviously in conflict with Point 3. The practical solution is a trade-off between how close to the source you catch an exception and how far you let it fall before you’ve completely lost the intent or content of the original exception.

Note: These principles are not particular to EJB exception handling, although they are applied throughout the EJB exception-handling mechanisms.

Technorati Tags: ,

Related Posts:

How to Format Dates for SQL in Java

August 23, 2003 on 9:20 pm | In General, Java | 17 comments

Say, you have a date that you need to store in a database record, and the date is entered as a string.

If you are like most beginners in Java, your first attempt will be to parse the string for a Date object by using the static method java.sql.Date.valueOf(String s). However, this will most likely not work as the input date is not in the format that is expected by the this method (i.e. yyyy-MM-dd).

The solution is to use a java.text.SimpleDateFormat object configured with the correct input pattern (e.g. dd/MM/yyyy) to parse the string for a java.util.Date object. The resulting date object can then be represented as a string in the yyyy-MM-dd format, which can be parsed by java.sql.Date.valueOf(String s).

This example demonstrates this.

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Date;
...
String dateString = "23/08/2003";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d;
try {
    d = dateFormat.parse(dateString);
    dateFormat.applyPattern("yyyy-MM-dd");
    dateString = dateFormat.format(d);
} catch (Exception e) {
    e.printStackTrace();
}

java.sql.Date date = java.sql.Date.valueOf(dateString);

System.out.println(date);
...

This code creates a java.sql.Date object from a date value entered as a string by first formatting it to the ISO date standard format.

UPDATE: Of course, there is no need to strictly pass a java.sql.Date object to the java.sql.PreparedStatement.setDate() method as it happily accepts a java.util.Date.

Related Posts:

php|architect features PHP ANN

August 20, 2003 on 8:40 pm | In PHP | 1 comment

Via Dan:

Congratulations! Your ANN is featured on php | architect.

I’m flattered. To experience my 15 minutes of fame, read the article.

Related Posts:

Java Artificial Neural Network

August 16, 2003 on 2:25 pm | In Java | 3 comments

My artificial neural network written in PHP prompted a few requests for a Java implementation. Well, I could only oblige.

Porting the code from PHP was easy; I only had to find and use the Java equivalent of the functions in my original implementation. However, since object serialisation in Java is more complex than in PHP, saving and loading functions were left out.

To understand the concepts and the alorithm, the reader is encouraged to refer to the above-mentioned article. The source code for the ANN class and the example usage can be downloaded.

Technorati Tags: , ,

Related Posts:

IM Online Status Indicators

August 10, 2003 on 10:12 am | In General | 2 comments

The Yahoo! Messenger status indicator on this page was put together based on information gleaned from a bugtraq advisory and Yahoo! Messenger help page.

It was easier to find information to build the AIM status indicator, perhaps owing to a larger user-base. All that was needed was a search on Google for “aim commands“.

There is no straightforward way to get the status for MSN Messenger, so OnlineStatus.org had to be used. Unfortunately, whenever this page is refreshed, I would be disconnected from MSN.

Technorati Tags: , , ,

Related Posts:

PHP collection class

August 9, 2003 on 2:05 pm | In PHP | Add a comment

Below is a simple collection class that works approximately like Java collection classes.

The usage should be fairly obvious, but you may also refer to the sample code at the end.

Collection.php

 1 <?php
 2 class Collection {
 3     var $elements = array();
 4     var $counter = 0;
 5     var $pointer = 0;
 6
 7     function Collection() {
 8
 9     }
10
11     function add($element) {
12         $this->elements[$this->counter] = $element;
13         $this->counter++;
14         $this->pointer++;
15     }
16
17     function remove($element) {
18         $found = null;
19         for ($i = 0; $i < count($this->elements); $i++) {
20             if ($this->elements[$i] == $element) {
21                 $found = $i;
22             }
23         }
24         if ($found != null) {
25             array_splice($this->elements, $found, 1);
26             $this->counter--;
27             $this->pointer--;
28         }
29     }
30
31     function contains($element) {
32         for ($i = 0; $i < count($this->elements); $i++) {
33             if ($this->elements[$i] == $element) {
34                 return true;
35             }
36         }
37         return false;
38     }
39
40     function hasNext() {
41         return $this->pointer < $this->counter;
42     }
43
44     function hasPrevious() {
45         return $this->pointer > 0;
46     }
47
48     function next() {
49         return $this->elements[$this->pointer++];
50     }
51
52     function first() {
53         $this->pointer = 0;
54         return $this->elements[$this->pointer];
55     }
56
57     function last() {
58         $this->pointer = $this->counter;
59         return $this->elements[$this->pointer];
60     }
61
62     function previous() {
63         return $this->elements[--$this->pointer];
64     }
65
66     function count() {
67         return count($this->elements);
68     }
69 }
70
71 ?>

Sample

 1 <?php
 2
 3 require_once "Collection.php";
 4
 5 $collection = new Collection();
 6
 7 for ($i = 0; $i < 10; $i++) {
 8     $element = "Element $i";
 9     $collection->add($element);
10 }
11
12 $collection->first();
13 while ($collection->hasNext() ) {
14     $element = $collection->next();
15     echo $element . "n";
16 }
17
18 echo "n";
19
20 $collection->previous();
21 $collection->previous();
22 $collection->remove("Element 7");
23
24 $collection->last();
25 while ($collection->hasPrevious() ) {
26     $element = $collection->previous();
27     echo $element . "n";
28 }
29
30 ?>

Technorati Tags:

Related Posts:

Welcome

August 8, 2003 on 11:30 pm | In General | Add a comment

Welcome for the n-th time.

Do not despair because I think I have managed to bend CSS, Movable Type and Textile to my will. But, of course, I may have left some errors here and there. I will keep working on the web site in days to come, and I encourage anyone to report errors to me.

Technorati Tags: , , ,

Related Posts:




Powered by blog.mu with Pool theme design by Borja Fernandez.