Wednesday, May 23, 2007

Netbeans 6.0 Visual Web Pack (Woodstock) - Common Tasks Component

There are some really cool new woodstock components available in the new VWP and from the java.net site. Woodstock is the original code name for the components which are used in VWP. These new components provide a new level of reusable web components which were not available before. One of the new components which is used extensively in project glassfish on the administrative console is the common tasks component.

This component provides a number of visual queues to what a user can do with the component. The component looks like this currently in Netbeans 6.0 M8. The component is not on the visual palette in 6.0 M9 Preview. The component does not necessarily look appealing in the current form on the palette, but renders perfectly.





As you can see from the images this is a really cool idea. The info button "i" allows you to provide additional information about what the task does. You can add a URL for additional information and a separate title for the info that is displayed. Here is an example from project glassfish where you can see another example of its use.



This shows you some of the capabilities.

The trick is how do you use it?

On project glassfish, when you click on the task it directs you to the appropriate category you selected. The details are scarce. So let me give you one solution. Use the actionExpression tag to set an action for the component. Here is mine for the example I showed above

<webuijsf:commontask
actionExpression="#{CommonTasksExample.masterDetailTaskAction}"
binding="#{CommonTasksExample.commonTask1}"
id="commonTask1" infoLinkUrl="/faces/Page3.jsp"
infoText="This example shows how to use a DropDown component to set values in a table" infoTitle="Master-Detail Example"
style="height: 48px; width: 250px" target="_self" text="Master Detail Example"/>

The actionExpression is bound to a method which uses a navigation handler to navigate to the appropriate page.

public String masterDetailTaskAction() {
getApplication().getNavigationHandler().
handleNavigation(FacesContext.getCurrentInstance(), null, "MasterDetail");
return null;
}

This allows me to navigate as required. I hope this example will inspire you to try out the new components. They will add a level of professionalism to your web pages and make your users have a better experience on your site. Implemented fully, it may even avoid a call to the help desk, or you to figure out what a task is supposed to do.

Thursday, March 29, 2007

HSQLDB Database Locking and Shutdown

I have had a number of questions on how to shutdown multiple HSQLDB databases running on one JVM. I have written an example application which will shutdown all of the databases attached to a single database engine in a JVM. The application uses only one test database, but the principal applies to multiple databases.

The software is released under the Apache 2.0 license. The project was developed in Netbeans 5.5

Here is the software: HSQLDBLocking.zip

Here is the source code if you prefer to see it instead of downloading the project:

/*
* TestHSQLDB.java
*
* Created on February 9, 2007, 10:04 PM
*
* $Id: TestHSQLDB.java 12 2007-03-29 21:19:48Z jyeary $
*
*/
/*
* Copyright (C) 2007 Blue Lotus Holdings, LLC. All Rights Reserved.
* Copyright (C) 2007 Blue Lotus Software. All Rights Reserved.
* Copyright (C) 2007 White Lotus Software. All Rights Reserved.
* Copyright (C) 2007 John Yeary. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF ANY KIND.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. BLUE LOTUS HOLDINGS, LLC
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
* SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
* OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
* BLUE LOTUS HOLDINGS, LLC OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
* PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
* THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
* EVEN IF BLUE LOTUS HOLDINGS, LLC HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/

/*
* Copyright 2007 John Yeary
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bluelotussoftware.db.hsqldb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Vector;
import org.hsqldb.DatabaseManager;

/**
*
* @author John Yeary
* @version $Revision: 12 $
*/
public class TestHSQLDB {

/** Creates a new instance of TestHSQLDB */
public TestHSQLDB() {
}

/**
* @param args the command line arguments
*/

public static void main(String args[]) throws Exception {

// Use the class loader to load the JDBC driver
Class.forName("org.hsqldb.jdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:hsqldb:file:test", "sa", "");

// Fetch all of the known database URIs and display them
Vector v = DatabaseManager.getDatabaseURIs();

for (Object o : v) {
System.out.println(((String) o));
}

ResultSet r = connection.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYSTEM_USERS");
r.next();
System.out.println(r.getString(1));

// Shutdown the database normally (0), or shutdown and script out using (2)
DatabaseManager.closeDatabases(0);

connection.close();
}
}

Wednesday, March 28, 2007

JDBC Tips and Tricks

Tip #1

A ResultSet is ALWAYS returned from an executeQuery() method. This often causes issues since developers often try to check to see if the result was null, and it will always return false.

For example:
...
Statement stmt = connection.createStatement();

ResultSet rs = stmt.exeuteQuery("SELECT * FROM X.Y");

// Check for null will be false
If(rs == null) {
//Do something
}
...

To check for results use next(). This will allow you to determine if there are results.

if(rs.next()) {
//Do something
}

Tip #2

Please note that generally a cursor returned by a query only moves forward through the ResultSet unless it is set explicitly. Some databases do not support anything other than forward only cursors. You need to set the cursor explicitly before executing a query.

To set the cursor to a read-only concurrency and scroll insensitive use the following:
...
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
...

Tip #3

To see native database errors try using Connection.nativeSQL("your query here") in the error messages. This will show you what the database thinks you are asking for.

try {
...
} catch (SQLException e ) {
info("An SQLException occurred. This is the native query sent to the database\n: "
+ con.nativeSQL("SELECT TRUNC(SYSDATE) DATE FROM DUAL"));
}
...

Tip #4

If you use a counter inside your next() method loop you can see how many results were processed, or report if nothing was processed.

...
int counter = 0;
while(rs.next()) {
//Do something
counter++;
}

if (counter == 0) {
System.out.println("No records processed.");
} else {
System.out.println(counter + " records processed.");
}
...

Popular Posts