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:

Tuesday, January 3, 2012

Start using .Net Entity Framework - 3


This article illustrates the usage of ADO.Net Entity Framework in creating, updating and deleting data records.

This article comes after:
    - Start using .Net Entity Framework - 1 , and
    - Start using .Net Entity Framework - 2

where we saw how to create a data model (ADO.Net Entity Data Model) from a pre-existing database, and how to use that model to query the database and fill a comboBox, by using a small example. The example database has only one table (Pais) with countries.

Taking the same example, we added a label to Form1, which will enable the visualization of each record's ID, and a text box that enables the user to modify the name of a country or create a new country in the database.



Buttons have also been added to Create New, Update and Delete a country in the table, and a Cancel button that closes the application.

To start, we add, in the method that handles the selection of a new line in the comboBox, the code to  fill the label (label1) with the country ID, and the text box (paisTextBox) with the name (nome) of the country. This allows the user to edit the current name of a country, in order to modify it, or to insert a new country's name, in order to create it in the database.

        private void paisesBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = paisesBox1.SelectedValue.ToString();
           
            paisAtual = (Pais)paisesBox1.SelectedItem;
            paisTextBox.Text = paisAtual.nome;
        }