ZK with EJB3.1 running on Glassfish (Part 2)

In part 1 we connected a web application to a database using an EJB for reading access, deployed on a Glassfish Server. In part 2 we do some modifications to create a true CRUD application by adding add, delete and update.

Pre-Requirements:

  • Netbeans 6.7 or later
  • Installed ZK plugin (link)
  • Glassfish V3 or later
  • Netbeans Project from Part 1 (link)

Continue reading

ZK with EJB3.1 running on Glassfish

A while back I created a tutorial that covers the direct DB access from a ZK application (link). Today I want to show you the most simple ZK application that reads from a DB using EJB 3.1 and display the data in a listbox.

Prerequisites:

  • Netbeans 6.7 or later
  • Installed ZK plugin (link)
  • Glassfish V3 or later

The tutorial covers 2 steps, the first part to create an EJB and the second part to create a ZK application. It covers all details of the project creation with Netbeans, but it does not elaborate the underlying EJB, JPA technology, feel free to read more about it at the Netbeans doc (eg. link) or in-detail at Oracle (link).

Continue reading

JPA and JDBC calls

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.