Showing posts with label Net Beans. Show all posts
Showing posts with label Net Beans. Show all posts

Monday, December 9, 2013

JSP - Enhance the first JSP example with Java Beans

In the previous article, we saw a first example of using JSP, where Java was embedded into the HTML to perform the dynamic part.
In this article, we are going to enhance the structure of the previous example, by making use of a Java Bean.
This structure enhances readability, and separates the code of the user interface from the code to perform computations.

In the case of more complex applications, the structure of the first example would lead to a solution difficult to understand and maintain. The structure of the solution presented in this article is more flexible, readable and so more easy to maintain and evolve.

So, we shall create a Net Beans project, just like in the first example, and make the same modifications to index.jsp. Then, we create the new JSP file (newjsp.jsp):


Contrary to what we saw in the First JSP Example, this file has not embedded the code to obtain the parameters or the calculation of the operation result. It has, instead, a JavaBean component that is no more than a class who are passed the parameters and performs the required calculations.

First JSP example

Java Server Pages (JSP) is a Java technology that allows creating web pages with a mix of static HTML and dynamic HTML. The dynamic part is generated by using Java language scripts that are embbeded in the HTML code through special Tags, recognized by the application server.

In this article we will see a small example that will serve to introduce some of the JSP tags and help in understanding how does Java work in the middle of all the rest.

The example is made with NetBeans IDE 7.2.1, and it consists of a small calculator that will allow to perform the four arithmetic operations on two integer operands.

Let's start by creating a new project in NetBeans, of type Web Application:


As applicational server we will use GlassFish:

 (we don't select any framework, in step 4 of creating a new project)

NetBeans creates a project structure with a file index.jsp, which will generate the initial page of the project:



Initial contents of file index.jsp:



Tuesday, April 17, 2012

Using Hibernate for Java - 2

In the previous article we saw how to use Hibernate as a middleware layer between the class structure of our Java program and the representation of instances of these classes in a database.


The previous article illustrated the mapping between a Java class and a database table, namelly the class Book and the table with the same name.

Let us now see how to map the class relationships, of type 1-to-N and N-to-N, to the database.

Let's start by adding to our model, which only had one class, two new classes:
     - BookCopy (A Book contains the information of a book, and is related to several BookCopy, which correspond to the book copies a library has of that book) --> 1-N relationship.
     - Author (A Book has several Author and vice-versa) --> relação N-N relationship.


In the Java project, developed in the previous post, let's start by adding class BookCopy and the respective Hibernate mapping file BookCopy.hbm.xml.

Sunday, April 15, 2012

Using Hibernate for Java - 1

Hibernate is a framework for object-relational mapping with Java language. It facilitates mapping classes, attributes and class relationships between an application class model and its implementation in a relational database. For defining the mappings, XML files or Java anotations can be used.
Hibernate works as a middleware layer between the Java objects and the relational database.

In this article, and in the next one, we will use Hibernate version 3 to ilustrate, through a small example, the utilization of Hibernate in a Java program.

As database system we will be using Oracle 10g.

To start with, we need to crate the database tables. In the article Example of Java Swing Application with Oracle Database - 1 this process is explained.
In this article, we will only be mapping class BOOK.


 Using Netbeans, or other Java IDE, we create a new Java application:


We create a package for the classes corresponding to database tables, and create class Book:





Saturday, March 3, 2012

Example of Java Swing desktop application without the JSR - 2

In this article we are going to create a Java Swing-based GUI project to complement the ones we previously made, and whose developments are addressed in:
     - Example of Java Swing Application with Oracle Database – 1
     - Example of Java Swing Application with Oracle Database – 2
     - Example of Java Swing Application with Oracle Database – 3
     - Example of Java Swing desktop application without the JSR - 1

In NetBeans 7.1, we need to create a Java Application Project:




A Java Application Project automatically creates a class with method main().
We really don't need that class, because we are going to create a JFrame, which automatically creates a main() method that launches it and makes the frame visible.

Anyway, and before creating the JFrame, let's add the needed references to our new project. We need to add, in the project libraries, references to projects HelperDB and librarySystemBLL. References to "ojdbc" jars that come with Oracle XE also need to be added, just like we did before in the other two projects.


Now, let's create the JFrame, that will be the application's main window:

Friday, February 24, 2012

Virtual Developer Day - Java (free event)

Example of Java Swing desktop application without the JSR - 1


From NetBeans 7.1, Swing application framework (JSR) left being supported by NetBeans, and is no longer part of the official JDK (Java Development Kit).

So, in this article, and the next one, we'll make a small application with Swing classes without using the Swing application framework.

We will make use of the application layers previously developed in:

     - Example of Java Swing Application with Oracle Database – 1 (database creation)
     - Example of Java Swing Application with Oracle Database – 2 (HelperDB project for accessing the database)
     - Example of Java Swing Application with Oracle Database – 3 (librarySystemBLL project / BLL classes)

So, after creating the database, and having present projects HelperDB and librarySystemBLL, we will create a new project (librarySystemGUI) for the user interface.
Firstly, we will see how to import a project to NetBeans, having only the java sources developed in any IDE.

Thursday, February 23, 2012

Swing-based desktop applications after NetBeans 7.1

In the articles
     - Example of Java Swing Application with Oracle Database – 1
     - Example of Java Swing Application with Oracle Database – 2
     - Example of Java Swing Application with Oracle Database – 3
     - Example of Java Swing Application with Oracle Database – 4

we developed a small example application, for desktop, using Java/JavaSwing.
For that we used NetBeans IDE, and created a project of type Java Desktop Application for the user interface.

Since NetBeans 7.1, however, Swing application framework is no longer supported by NetBeans, and stopped being part of the official JDK (Java Development Kit). In fact, in NetBeans 7.0.1, the following message already appeared every time one created a Java Desktop Application project:


In NetBeans 7.1 we have, at least, two alternatives:
  • To create an application from scratch, or
  • To create a project of type Netbeans Platform Application
In the next article we will address the first option, lefting the second alternative to a future article.


Tuesday, February 7, 2012

Example of Java Swing Application with Oracle Database – 4

In this article we're going to develop the presentation layer of our software application. This layer is also know as User Interface or Graphical User Interface Layer.

In blog entry Example of Java Swing Application with Oracle Database - 1, the first one of this series, we've handled the Oracle database creation. In the 2nd article a database access package has been created, HelperDB. The 3rd article addressed the creation of a class library at the level of business logic layer and data access layer.

This article addresses the construction of the user interface project (GUI - Graphical User Interface) using Java Swing classes. To do that, we need to create a new Net Beans project, which we will name LibrarySystem:


This project must be a Java Desktop Application. This kind of project supplies a template with the basic infrastructure of a desktop application, such as a main window with a menu bar and a status bar.

Then, we select Basic Application (the other type, Database Application, will be treated in another series of articles).

This kind of project creates beforehand 3 classes:
- LibrarySystemApp.java - this is the application's main class. The main method will launch the main window. From there, the whole application will be event-driven.


Thursday, January 26, 2012

Example of Java Swing Application with Oracle Database - 3

Today, we will develop a class library that will consist in a software layer that maps to concepts of the problem domain, and bridges the gap between the user interface, whose development will be addressed in the next article, and data persistence layer, treated in the first article of this series.

This layer, with concepts and operations of the problem domain, is commonly called the middle-tier or business logic layer.
 

We may consider, then, that our small application system will have a 3-layered (or 3-tier) software architecture (we'll discuss the topic of software architecture in a future article).


There are several ways to develop this middle tier, which may itself comprise one or more layers. We will develop a class for each domain concept, which, within this small example, corresponds more or less to develop a class for each table in the database.
 

There are various ways to do this. In particular, we will look at two possible ways, each with its own group of supporters, and with its own advantages and disadvantages.


1 – Some authors advocate the development of only one layer of classes with attributes and getters and setters (eg class Book below) separated from a layer with classes containing operations with meaning for the business (eg class BookBLL below), being this latter layer the one with knowledge for interfacing with the database.

1.1 classe Book:
public class Book {
    private int idBook;
    private String title;
    private String editor;
    private java.sql.Date date;
    private String ISBN;
    private String edition;
    //
    private List<Author> authors = new ArrayList<Author>();

Sunday, January 22, 2012

Example of Java Swing Application with Oracle Database - 2

In the previous blog entry we initiated the construction of a series of articles for constructing a small example application for managing a library, for desktop environment and using Java Swing classes, and accessing an Oracle database.

The previous article addressed the Oracle database creation. This article will handle the construction of a Java class for accessing the database.

We will be using NetBeans IDE 7.0.1, which may be downloaded from http://netbeans.org/community/releases/70/.

After installing NetBeans IDE 7.0.1, we must create a new "Java Class Library" project, which we will name HelperDB, because it will consist only of a single class for helping us access the database.

 
We also create a Java class, that we named DBAccessObj, in file DBAccessObj.java.

It is needed to include two or three (depending on the ORACLE version) JAR library classes to access the Oracle database by using a JDBC driver:


These jar files may be found in directory

                              oraclexe\app\oracle\product\10.2.0\server\jdbc\lib

being oraclexe the directory where Oracle XE has been installed.

Let's now create a package, which we name HelperDB, that will contain our class, and let's import package java.sql, which contains classes for connecting and sending SQL statements to the database:



package HelperDB;

import java.sql.*;

public class DBAccessObj{
       private Connection conn;

       public DBAccessObj()
       {
       }
}


As the only instance variable in our class, we shall create conn of "type" java.sql.Connection.
Actually, Connection is a Java interface, and so conn may contain an object of any type (class) that implements that interface. So, conn may contain an instance of any type of connection. The type will be given by the DriverManager according to the driver that is being used.

The class constructor will then be modified to create a database connection:

Thursday, January 19, 2012

Example of Java Swing Application with Oracle Database - 1

Today we're beginning a series of blog entries that will culminate in a small example Java application for a library management, for desktop environments and using Java Swing classes, and accessing an Oracle database (DB).

This first article addresses the creation of a small Oracle database. It was used Oracle Express 10g, which can be freely downloaded from http://www.oracle.com/technetwork/database/express-edition/downloads/102xewinsoft-090667.html. There is a more recent version of Oracle Express, the 11g release 2, which may be downloaded from http://www.oracle.com/technetwork/database/express-edition/downloads/index.html and with which the example shall also execute correctly.

After installing Oracle, we may access the database administration tool by going to button Start --> All Programs --> Oracle Database 10g Express Edition --> Go to the Database Home Page  or, in a browser, go to http://localhost:8080/apex.

Then, it shall appear a page for us to login in the administration tool:





We shall login with user SYSTEM and the password that we defined for that user during the instalation process.
After logging in the work space appears: