|
March 2007
March 20, 2007
Using Java DB
Java DB (derived from Apache Derby) is included with the latest Java EE and Java SE platforms. Let's take a look at how to use it.
I've used Java DB quite a bit since it was released with Java EE 5, and I like it. It's well integrated with NetBeans, and writing code to use it is easy. For instance, the figure below shows NetBeans' Runtime View, which can display servers to deploy to, running application processes (related to projects you are developing), a built-in HTTP server, and preconfigured Java DB databases. I purposely blotted out some production databases I'm currently working on.
Figure 1: NetBeans' Runtime View.
When you expand the Databases leaf, you will have access to configured databases. You can right-mouse-click on the lead, and choose "New Connection" from the pop-up menu, to add a new database configuration. The "New Database Connection" dialog box will appear, asking you to enter information about the database you're connecting to, or creating. You can choose the embedded driver (JavaDB runs in the context of your application), or the networked driver (the JavaDB process runs external from your application). The remaining information includes a username, password, and the full URL for your database.
From the figure above, you can see that I'm connected to a sample travel agency database. All of the tables within this database are visible, and you can create new tables through this interface also, along with stored procedures, and specialized database views.
Executing direct queries outside of your application (to test your queries, stored procedures, or just to pre-populate your database) is very easy with NetBeans. Simply right-mouse-click on a database entry, or on a table within a database, and choose "Execute Command" from the pop-up menu. You can write your query in the SQL Editor that opens as a result (see the figure below).
Figure 2: SQL Editor.
Writing JBDC code for JavaDB is similar to other databases. Java EE 5 supports dynamic resource injection, where the application server takes care of linking Java attributes in your code to a physical database. However, there are still cases where I need to write a standalone Java application, or a simple Servlet, where I need to hand craft the JDBC code. The addPerson method below is a JDBC example that connects to a Java DB database and inserts a row into a table:
public void addPerson(Person person)
{
try {
Class.forName(
"org.apache.derby.jdbc.ClientDataSource").newInstance();
Connection conn = DriverManager.getConnection(
"jdbc:derby://localhost:1527/Travel",
"TRAVEL",
"TRAVEL");
String query =
"INSERT INTO PERSON values(?,?,?,?,?)";
PreparedStatement ps = conn.prepareCall(query);
ps.setInt(1, this.genID() );
ps.setString(2, person.name);
ps.setString(3, person.title);
ps.setInt(4, person.freqFlier);
ps.setDate(5, this.genDate() );
ps.execute();
ps.close();
conn.close();
}
catch ( Exception e ) {
e.printStackTrace();
}
}
After being skeptical as to the need for Sun to include a database with both Java EE and Java SE, I'm now sold on Java DB. I've found that it’s very robust, it performs well, and it scales well also. I've used in some demanding production web applications, and it has worked flawlessly, although Sun does mention that it’s better suited to internal corporate applications.
For more information on Java DB, and Apache Derby, click on the following:
- Java DB: http://developers.sun.com/javadb/
- Apache Derby: http://db.apache.org/derby/
Happy coding,
EJB
Posted by Eric Bruno at 10:21 PM Permalink
|
March 05, 2007
Parallel Processing for the Masses
It wasn’t too long ago that multi-CPU computers were called servers and resided in data centers.
With the advent of multi-core computing, however, a growing number of everyday people are now paying bills, blogging, surfing the web, managing their digital pictures, and more, with what is essentially a multi-CPU desktop computer. This has experts asking the question, “Do people really need the power of multiple CPUs in their home computer?”
The answer is yes, for the same reason people have needed more than 640K in their home computers for the past 15 years. The real question we should be asking ourselves is how are we going to best take advantage of this power? This presents a real challenge as writing code to truly take advantage of more than one CPU is far more complex than one might think. There is an entire branch of computer science that studies this problem, called parallel computing.
Parallel computing can be more involved than simply creating java.lang.Threads in your Java application; that only helps with applications that perform multiple, individual, tasks simultaneously, such as web and application servers. For other applications, you need to figure out ways to break up the execution of a single task into pieces that can be executed in parallel, and then combined, where the result is an overall speedup in task processing. Debugging these applications can be a chore, also.
I’m sure, as you’re reading this, you can think of ways to accomplish this for the current application you’re developing. But doing this for every task in every piece of code you develop from this point onward can be very time-consuming. Never mind the issues around tuning based upon the actual number of cores that exist in the computers you deploy to. As with any programming problem, you need to spend less time on environment issues, and more time solving business problems.
There are companies with toolkits and frameworks to help you solve parallel computing problems. Pervasive Software’s soon to be released product, DataRush, is an example of a new product that aims to help developers tackle parallel programming challenges. DataRush goes through the trouble of breaking programming tasks into pieces that can be executed in parallel and then combined, so that you don’t have to. It then dynamically adjusts the application’s behavior according to the CPU cores and resources available on the target machine.
Sun’s CEO, Jonathon Schwartz, recently wrote an excellent blog entry about Sun’s efforts with parallel computing. He also does a good job of explaining how revolutionary multi-core computing is in general (it’s probably more revolutionary than many experts even realize right now). Instead of looking at it as a multi-threaded application, parallel processing problem, he looks at ways it can save space, power, and resources in the data center. With more computing power (namely, more CPU cores) in a single server, you can add the power of virtualization to run more than one OS, almost natively, on a single server. In fact, Sun has launched Project Neptune, which marries the parallelism of multi-core, and that of the OS, with the network.
What happens when the combined concepts of parallel processing and virtualization move out of the server, and into network devices, storage devices, and other areas of computing? Will your parallel programming skills and toolset be up to the challenge? Learn about the evolving new technologies in this area today, such as Project Neptune, and Pervasive DataRush, and be ready for the continued explosion in computing power in the years to come.
-EJB
Posted by Eric Bruno at 09:15 AM Permalink
|
March 01, 2007
Java Mobile Application Video Contest
Sun, in an effort to help mobile Java developers get more recognition, has launched a fun contest with cool prizes.
Java is embedded and running in literally billions of devices around the world. Java ME developers work hard to get these applications running, and Sun wants to reward them. Whether or not you’re a Java ME developer, this contest may interest you.
After reading the rules, you’ll need to create a short video (under 3 minutes) to explain and showcase the Java ME application or service that you think is worthy. When you’ve completed your video, you’ll need to post it to YouTube.com (the viral effect at its best).
You can win prizes such as a state-of-the-art Ericsson K800 phone, a Sony PS3, a Blu-Ray DVD player, and an Amazon.com gift card. Remember that this a Sun Microsystems contest for Java ME applications and services. Check Sun's site for all the rules and details.
Posted by Eric Bruno at 10:26 AM Permalink
|
|