Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

JDBC scripting, Part 2

Programming and Java scripting in JudoScript

In Part 1 of this series, I introduced JudoScript, detailed its JDBC (Java Database Connectivity) scripting abilities, and demonstrated the power of its synergy of functional support on top of a general programming language engine. JudoScript is, in a way, a shell for the underlying Java platform, which is, in turn, an interface to today's computing environment that is much richer than 20 or even 10 years ago. JudoScript is designed for today's computing needs, and is a language designed for any computer developer, not just Java developers, to manipulate computers and information.

In Part 2, I concisely present the other face of JudoScript, that is, its programming capability, including Java scripting. I list JudoScript's major features, but do not elaborate on them. The features introduced are all used in a J2EE case study. To conclude, I summarize the differences between conventional and functional scripting languages. I use criteria like signal/noise ratio, directness, coherency, and focus to compare code written in JudoScript with languages like Java and Perl, where Java is arguably one of the best general-purpose programming languages and Perl is the conventional scripting language that no other has surpassed in terms of capability.

Read the whole series on JDBC scripting:



Introduction to programming in JudoScript

The world already has too many programming languages. Inventing a new syntax for programming truly demands reasons. JudoScript, being a general-purpose programming language as well, simply adopts a JavaScript-like programming model and syntax, which is not bad at all. However, JudoScript has many sophisticated datastructures, thread programming, object-oriented programming, and syntactic sugar—it is a powerful programming language. JudoScript is also a top-of-the-line Java scripting language, capable of scripting Java to the fullest allowed by JVMs, including capabilities to extend Java classes and implement Java interfaces using JudoScript classes. I start by introducing the basics of JudoScript programming.

Values in JudoScript all have types. JudoScript has primitive types of integer, double, string, and date and time; all other values are objects, which can be built-in type objects or any Java object. JudoScript is a dynamically typed language, meaning that variables are generic "containers," and the value held in a variable at any time has a definitive type. JudoScript supports object-oriented programming, so you can define your own classes of objects. Each type, whether a primitive value, built-in datastructure, or extraneous object, has numerous properties and a set of predefined methods that are accessed and invoked in the same way regardless of type. Here are some examples:

a = 12345;
println a, ', HEX: ', a.fmtHex(), ', Roman: ', a.fmtRoman();
d = Date(2004, 3, 26);
println 'Today (', d.fmtDate('yyyy-MM-dd'), '), week of the year is ',
d.weekOfYear;
lst = new java::ArrayList;
println "List's length: ", lst.size();


Flow controls include if-elif-else, switch-case-default, while, do-while, and the for-family statements. Unlike Java, the conditional expression for if and while does not have to be quoted in parentheses, but curly braces ({}) must always surround the bodies, even if the body contains just one statement.

Functions and user classes can be defined. I don't discuss class definition in this article. Functions can take parameters; when called, the number of parameters can be less or more than the number of declared parameters. Missing parameters are assumed undefined, and extras are stored in the predefined local array variable, $$args. Functions are always expected to return a value; if no explicit return is used, undefined returns.

Exceptions can be thrown and caught like this:

{ connect to 'fail it', 'any', 'any';
catch:
  println $_; // $_ is the exception object thrown.
finally:
  println 'Finally!';
}


Or, you can use this format:

try {
  connect to 'fail it', 'any', 'any';
} catch ex { // If ex is not specified, $_ is used.
  println ex;
} finally {
  println 'Finally!';
}


Next, let's dig a little deeper by looking at some of JudoScript's built-in data types.

Built-in data types

JudoScript's built-in data types include primitive values; the Array, LinkedList, Set, Object, and TableData objects; and the for..in and printTable statements.

The primitive values
JudoScript's built-in data types include primitive types and datastructures. Primitive types are integer, floating-point number, string, and date and time. All types share many methods; some methods are number-, string-, or date-time-specific. Most number-specific methods are mathematical functions, conversions (e.g., int()), and formats (e.g., fmtHex(), and fmtRoman()). The string value is the most commonly used; it has all the string operations. Moreover, it represents file paths and URLs, so methods exist for those operations, such as fileExists(), fileTime(), isDir(), and parseUrl(). The date/time value has additional methods such as formatDate(). The date/time value also has fields; some are read-only, others are read-write. The following example gets a Date object for tomorrow:

t = Date(); // Now
++t.date;
println t.fmtDate('yyyy-MM-dd');


The fmtDate() (or formatDate()) method takes the same formatting string as the java.text.SimpleDateFormat class. For a variable holding a date or time string, you can call parseDate():

s = '2003-9-18';
t = s.parseDate('yyyy-MM-dd');
println t.fmtDate('MMM d, yy'); // Prints: Sep 18, 03


I discuss some of the detailed usages of primitive types later. Please refer to the language reference for more details.

The Array, LinkedList, and Set objects
Array can be created in two ways, both consistent with JavaScript syntax:

a = [];
a = [ 1, 'xyz', Date() ];
a = new Array;
a = new Array( 1, 'xyz', Date() );


JudoScript arrays are all dynamic, i.e., you can call an array's add() method to add elements. Array elements can be retrieved and set with the [] operator; the index starts at 0.

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. JDBC scripting, Part 2
By JavaWorldAdministrator
0 04/22/08 06:02 AM
by Anonymous


Resources