Interacting with Databases through the Java Persistence API

25 04 2009

Featured article about working with JPA in Netbeans. Reviewed and reproduced following an article by PACKT Publishing (link). Please hava a look at the end of the article with the book recommendation.

We will look into:

Java EE 5 Development with NetBeans 6
  • Creating our first JPA entity
  • Interacting with JPA entities with entity manager
  • Generating forms in JSF pages from JPA entities
  • Generating JPA entities from an existing database schema
  • JPA named queries and JPQL
  • Entity relationships
  • Generating complete JSF applications from JPA entities

Read the rest of this entry »





Tutorial: Reading from the DB with Netbeans and ZK (Part 2)

24 03 2009

In this part 2 we modify the ZK project from part 1 and add data binding and a detail view.

Before we start adding the new functions, you might want to look in some of the project settings that are default when you create a new ZK360 type web project. Both context path (under Project properties|Run), as well the Display Name in the web.xml (under project explorer|configuration files) . You better change the default to avoid confusion when starting to create the second project.

I recommend to have a look at these 2 documents: ZK Developers Guide and the ZK Reference Doc (link), giving you more insights how ZK works and how feature rich it is. I noticed the available Netbeans palette does not show all available features, so you are better of browsing the docs and look at the sourcecode of the ZK Explorer application (Live Demo).

Back to part 2.

Requirements:

  • Netbeans 6.5 with a running derby DB and the sample database (customer table)
  • Installed ZK 3.6.0 plugin
  • The project from part 1 (link)

Content

1. Open the ZKReadDB project from part 1
2. Change the controller class
3. Activate the Data Binding Manager
4. Make the listbox work witha model and refer the listitems to the fields
5. Implement the detail view
6. Add the detail view with textboxes
7. The complete index.zul and controller.java

Read the rest of this entry »





Tutorial: Reading from the DB with Netbeans and ZK

21 03 2009

After the first very basic tutorial that gets you started with the required plugins and settings (link), I will summarize in this tutorial how display data with zul using the JPA.

Requirements:

  • Netbeans 6.5 with a running derby DB and the sample database (customer table)
  • Installed ZK 3.6.0 plugin

Tutorial:

Content:
1. Start a new project
2. Create a new Entity Class from Database
3. Create a Controller
4. Create the table

Read the rest of this entry »





JPA and JDBC calls

9 11 2008

JPA – Java Persistence API
This JAVA programming framework is part of EJB3.0 in JEE5. It is very powerful and removes a truckload of work for todays enterprise developer. You manage the relation between classes and relational DB’s via ORM. Once you digest the concept, it become part of your toolset.

I come from a old-fashioned background and always interest what runs under the hood and like to know about the manual way as well before indulging the “can-do-everything-easily” way.

So here my hommage to the “old” of directly accessing a DB. I am still using this approach for simple J2SE tools though ! I will talk about JPA more in detail soon.

Open and access a MySQL DB from Java

Establish a connection

String userName = “user”;
String password = “********”;
String url = “jdbc:mysql://localhost/db_sample”;
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
con = (Connection) DriverManager.getConnection(url, userName, password);

Read from a table

Statement dbStatement = (Statement) con.createStatement();
ResultSet dbResultSet = dbStatement.executeQuery(“Select * from table_name”);
while (dbResultSet.next()) {
String testField = dbResultSet.getString(“fieldname”);
}

(Dont forget to cover both with try-catch’es and Netbeans will remind you of all the missing imports)

You see a number of potential flaws which gonna create trouble once you need to change it:
- hardcoded user, password, DB
- hardcoded table- and fieldnames
Imagine the project time 100 with dozens of tables and 100’s of fields, thats where the headache starts… You know what I am talking about.