Showing posts with label Apache Derby. Show all posts
Showing posts with label Apache Derby. Show all posts

Monday, July 02, 2012

JavaFX 2.2 Pie Chart with JPA 2.0

Shipping Costs By Purchase Order
I saw an example of a Pie Chart using JavaFX. I thought that it looked really nice and I wondered how easy it would be to do using JPA 2.0, an example Apache Derby DB included in NetBeans, and NetBeans 7.2.

Well the first iteration took about 10 minutes, and the tweaking took another 15 minutes. Basically, if you asked me to do it again, I bet I could do it in under 5 minutes. My output looks really cool too.

I saw an old post using JavaFXScript to create a similar output which was used in JSF. I am wondering if I can do the same thing using version 2.2 of JavaFX, or if I should wait until the HTML libraries are complete.

The code for the project can be downloaded here: PieChartExample.zip

PieChartExample.java


Wednesday, February 08, 2012

Book Review: Murach's Java Programming 4th Ed.

I recently had the pleasure to read and examine Murach's Java Programming 4th Ed. Joel Murach does a good job of producing training and reference manuals for a variety of Java technologies. This is no exception.

The book focuses on Java 7 Standard Edition, but does include a number of other features which were added in Java 5 and Java 6 for completeness.

One of the things I really like is that it uses NetBeans as the IDE to teach Java to the next generation of programmers. A lot of books on Java focus on the language itself. This would have the appearance that an IDE plays no role in learning Java. Any professional programmer will tell you that an IDE is absolutely essential in making you more productive. Joel's choice of NetBeans as a tool to teach Java with demonstrates its ease of use with beginners. Chapter 1 is dedicated to getting the new programmer up to speed with NetBeans.

The book offers educators and trainers optional support materials for teaching. This thoughtful approach to education is vital for anyone teaching Java.

The layout and examples are designed to get the programmer up to speed on the topic being covered.  A typical example of the layout is to have textual material on the left-side page with examples on the right. This allows you to read the material, and refer to the actual code examples. The book layout makes it easy to teach and learn. You are not left reading a volume of material without any code examples to make the essential points. This is a real benefit to the reader.

In addition, the book is developed with exercises to get the beginner involved. The exercises are based on the material learned in the chapter. The example exercises follow a common theme throughout the book allowing you to build on top of the information learned in the previous chapters. This common thread keeps you engaged through the book to see how the different topics tie the applications together.

Overall I would highly recommend the book to anyone looking to learn Java 7, or update their skills. I would give it (4/5) stars.

Chapters

Chapter 3 has a great section on using BigDecimal and arbitrary precision arithmetic. I really liked the explanation and examples. Although BigDecimal was added in 1.4.2 it is not used as much as it should, and this gives it a little press.

Chapter 5 mentions a best practice which is often overlooked. The practice is to add any custom exceptions to the package in which classes are likely to throw them. Often I find that the exceptions are in a package.exception package. This may be a good design if there is a general hierarchy for using the exceptions in multiple sub-packages, but generally is not the case.

Chapter 7 describes a relationship between a class and its instance. The description is eloquent and worth repeating.
"Once an instance of a class is created, it has an identity (a unique address) and a state (the values that it holds). Although an object's state may change throughout a program, its identity never does."
This is a beautiful explanation of OO programming relationships between a class and instance.

Chapter 8 has a discussion on the use of final. The point to be made is that if you declare a class final, all of its methods automatically become final implicitly.

Chapter 10 on nested classes really does a good job of explaining the requirements and limitations of nested classes. There are a couple of exam type gems like an inner class can not contain any static methods, or variables, and a static class can only reside inside another class.

In chapter 11, there was a remark about using Arrays.sort(Object[]) to sort an array. It mentioned that the objects must implement the Comparable interface. I thought that it would "naturally order" without it. I was wrong. I validated that specific case. Even an old dog can learn new tricks.

Chapter 19 is the best tutorial on StAX I have seen. It is simple and easy to follow. After reading the chapter, I will go back and look at StAX when I need this kind of low level XML handling.

Chapter 20 covers Apache Derby (JavaDB). It does a good job of explaining the basics, and gives some implementation recommendations. My only gripe is that the author fell into the Oracle trap of claiming that it is not an enterprise capable database. This is simply not true as borne out from my own experience.

If there is a weakness in the book, it has to do with Chapter 22 on Threads. The chapter does not cover this important topic in enough details. It also does not mention the Java Concurrency Library which has been standard since JDK 6, and handles a lot of the issues with direct thread programming. This chapter should be updated.

Errata

Figure 2-5 Statements that mix int and double variables. The first example requires a cast to int.
int result9 = (int) invoiceTotal / invoiceCount;
Figure 9-1 Example 1 should reflect the current Java design principles. All methods of a public interface are public and abstract.
public interface Printable {
void print();
}
Figure 9-1 Description bullet #3 should read
A classs that implements an interface must provide an implementation for each method defined by the interface, or the class must be declared abstract.
Figure 10-8 Bullet #2 is incorrect. A nested class does not need to be enclosed in a public class of the same file name as the public class. For example X.java could contain the following code:
class P {

    Q q;

    P() {
        q = new Q();
    }

    public Q getQ() {
        return q;
    }

    class Q {

        void print() {
            System.out.println("I am inside P");
        }
    }
}

This will work without being in a public class.

Figure 12-2 Common Collection Classes.
The statement that a HashSet requires that classes to implement a hashcode is incorrect.

Chapter 12 (Page 384) 2nd Para. incorrectly states the two queue methods as push and pull. The pull method does not exist, and was likely supposed to be pop. There are actually three from the Queue<E> interface of note: offer(E e), peek(), and poll.

Tuesday, February 09, 2010

NetBeans 6.8 JSR-296 Database Application Example

Here is an example application development video.

Wednesday, June 18, 2008

Apache Derby (JavaDB) INSERT Statement with IDENTITY

If you are trying to insert values into a table which has an identity column, the documentation is not very clear. If I create a table in an Apache Derby (JavaDB) database which has a primary key which is generated always, the syntax for doing a query based insert is a little different.

Here is the table:
CREATE TABLE FAMILY (
ID INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
DESCRIPTION VARCHAR(50) NOT NULL
)
Now if I want to insert values from another table here is the query I used:
INSERT INTO FAMILY (DESCRIPTION) ( SELECT DISTINCT FAMILY FROM EQUIPMENT_GROUP WHERE FAMILY IS NOT NULL)
Please note that I did not put anything into the ID column. If I try to specify a value, it will result in an exception
Exception: Attempt to modify an identity column 'ID'.
Since the column has been marked as GENERATED ALWAYS, it handles incrementing the key and assigning it.

Sunday, October 21, 2007

Murach's Java SE 6

There have been a number of books and tutorials written about Java. The gold standard for me has always been "Learning Java" by Patrick Niemeyer and Johnathan Knudsen (O'Reilly & Associates). I have had a number of new programmers, enthusiasts, and members of the JUG tell me it is information overload. The book does contain a lot of information in its 828 page heft. As a result, I have been looking for a book just for beginners. I have found it.

I just completed reviewing Murach's Java SE 6. It is a very good resource for learning Java. This book does an excellent job of providing a firm basis for understanding the technology. The book is clearly and concisely written. The book is divided into 5 major sections which cover the essential Java skills to advanced topics on data access programming using XML and JDBC.

The teaching style is very clever. It typically takes the form of a page of information with facing page with examples. I found this to be very important in getting sometimes difficult points across. I typically take the "Show me the code" philosophy, and this style works for me.

The other technique that used is to convey a purpose for learning Java. This is done by using the various topics as building blocks to create an application. The final result is a completed application at the end of the book. It encompasses the lessons learned, and gives the new programmer a sense of accomplishment with a completed functional application at the end of the book. I love it.

I have found that people learn better with functional code examples. This book is replete with them. One of my greatest annoyances is to have code samples which do not work. This clouds the ability to learn because it forces the beginning programmer to question their abilities. The shroud of uncertainty should not be because the gold standard code is incorrect. I am pleased to note that I tried a number of code examples and they all worked.

My favorite section is Data access programming with Java. Chapter 20 covers working with XML. This is a must for any programmer. XML is the new black, and anything that can help you learn this important technology is a must. In chapter 20, the topic of StAX is covered. This is the best simplified example of using StAX I have seen. After reading the information, and performing the examples, I felt I had a better understanding of this technology.

I only have a few minor negative points to mention: the title is a little misleading. The majority of the information in the book really details Java SE 5 enhancements with two notable exceptions: StAX and an introduction to Derby (Java DB). The water is chummed in Chapter 1 where he mentions that C# runs faster than Java. This is a point that needs to be proven. Java 6 SE is almost as fast as C/C++ on bare metal. I have not heard anything like this for C#. I will forgive this remark though since the rest of the book is so well written.

My overall impression is that the book is an outstanding resource for new and seasoned programmers. This is a great book to add to the reference shelf.

The publisher has a number of code samples, student workbooks, and tutorials:

Student and Trainer Resources (Publisher)

Sunday, January 28, 2007

LDAP Authentication with Apache Derby (Java DB)

It has come to my attention that there is not a really good tutorial on how to use Apache Derby and LDAP without combining a lot of different sources. Hopefully this will simplify the work for everyone wanting to use LDAP authentication with Apache Derby.

Assumptions:


Procedures:

Global:

If you want to have the configuration set on a global level add the following to a file called derby.properties in the installation directory. You will need to modify it to match your environment.

derby.connection.requireAuthentication=true
derby.authentication.provider=LDAP
derby.authentication.server=ldap://localhost:389
derby.authentication.ldap.searchBase=ou=people,dc=bluelotusholdings,dc=com
derby.database.defaultAccessMode=fullAccess

Database:

You can set the database properties on a database basis. This is accomplished by setting the parameters (database properties) in the database using SQL. The script is located below. This must be modified to accommodate your environment. Place the jndi and ldap files in the directory containing the database (see below). The database in my example is called ldaptest.




/* Apache Derby 10.x */


/*
* This file is used to set the database-wide LDAP configuration. Rebooting the service is
* required for the changes to take effect.
*/

/* Set the authentication provider to LDAP */


CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(
'derby.authentication.provider',
'LDAP')

/* Set the LDAP server */

CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(
'derby.authentication.server',
'banyan:389')

/* Set the BaseDN to search */

CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(
'derby.authentication.ldap.searchBase',
'ou=people,dc=bluelotusholdings,dc=com')

/* Create a cached entry for a user */

CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(
'derby.user.jyeary',
'uid=jyeary,ou=people,dc=bluelotusholdings,dc=com')
Once you have executed the script above, you will need to restart the database. I used my Aqua Studio application to connect to the database.


All Done!

Notes:

  • If LDAP is not available you can not connect to the database.

Popular Posts