Date post: | 13-Apr-2015 |
Category: |
Documents |
Upload: | ajmal-muhammad-p |
View: | 140 times |
Download: | 2 times |
Java Database Classes
E-Software Presents
Muhammad Ajmal P
1
Java Database Classes By Muhammad Ajmal P
Introduction:
This is a java package for 2 tier and 3 tier database application development. RMI
[Remote Method Invocation] is used for 3 tier [Tested with JDK 1.6]. I used eclipse indigo
edition and windows builder eclipse plugin for drag drop designing. You can also use Netbeans.
I’m a professional Delphi software developer. It’s very easy to develop database
applications in Delphi. There are so many ready controls in Delphi for database application
development. I’ve done my best to port the same method in java. I’ve developed few packages,
they are
Controls
DataControls
DataSnap
DBExpress
In this the DBExpress is the main database package. This includes,
ESQLConnection
ESQLQuery
EClientDataSet
EDataSetProvider
EDataSource
The next is the DataControl package which contains,
EDBCheckBox
EDBGrid
EDBImage
EDBLookupComboBox
EDBRadioButton
EDBTextField
We can discuss each later.
Java Database Classes
E-Software Presents
Muhammad Ajmal P
2
Screenshot of designing using windows builder in Eclipse editor
Packages:
1. Controls:
Currently I’ve added only 2 controls. They are EFrame and EImage.
EFrame:
EFrame is a class which inherits JFrame. In this I have added a window positioning
option. If you call the setSize(int x, int y) of this class, then this will draw a window in the center
of the screen.
EImage:
EImage is a class which inherits JLabel. This can be used as image control. This is later
modified as EDBImage.
Java Database Classes
E-Software Presents
Muhammad Ajmal P
3
2. DBExpress:
This package includes all the database invisible classes. They are,
ESQLConnection:
As name suggests, this is used to connect application with the database. This can be
used to connect to 4 types of databases [Firebird, MySQL, SQLite, and MSAccess]. For this there
is a function “public void setDatabaseType(final EDatabaseType databaseType)”. For setting
username and password there is 2 methods, “public void setUserName(final String UserName)”
and “public void setPassword(final String Password)”. You can use “public void
setDatabasePath(final String DBPath)” for setting the path of database. Also you have to use
the database driver package. This can be downloaded free from corresponding sites.
E.g.:-
final ESQLConnection connection = new ESQLConnection(); connection.setDatabasePath("localhost/ESoftECA"); connection.setDatabaseType(EDatabaseType.FireBird); connection.setPassword("masterkey"); connection.setUserName("sysdba"); try {
connection.open(); // Not necessary, this ‘ll be called from EDataSet
} catch(final AuthenticationException e1) { } catch(final SQLException e1) { } catch(final ClassNotFoundException e1) {}
EDataSet:
With ESQLConnection we connected our application to database. Next we want to fetch
data from the database. This class is for fetching data from database and stores the fetched data
safely for future use. This class is designed as a base class for 2 Tier and 3 Tier classes. The sql
query is set as CommandText of the EDataSet [dataset.setCommandText(“SELECT * FROM
USERMASTER UM WHERE UM.USERAGE = ?USERAGE”);]. In this you can use named parameters
as shown. Here before setting the parameters you have to call the “public void fetchParams()”.
Then call the “public void open()”. This will be discussed I detail in ESQLQuery because we don’t
use the EDataSet directly but use the ESQLQuery which inherits the class EDataSet. Also we use
the EClientDataSet for 3 Tier whereas ESQLQuery is used for 2 Tier. There are many other useful
methods. The “public boolean addParamByName(final String ParamName, final Object
obj_Value)” method is used to set the params of sql query one by one. For the above sql query
use, dataset.addParamByName(“USERAGE”, 24);. There are some events they are,
a. public void onAfterOpen(final EDataSet dataSet);
b. public void onAfterScroll(final EDataSet dataSet);
Java Database Classes
E-Software Presents
Muhammad Ajmal P
4
c. public void onBeforeOpen(final EDataSet dataSet);
d. public void onBeforeScroll(final EDataSet dataSet);
ESQLQuery:
This class is derived from the EDataSet class. Actually this class will be used for 2 Tier
data access. For 3 Tier, we have to use ECllientDataSet. The EDataSet needs the ESQLConnection
for connection to database. For this a function “protected void setSQLConnection(final
ESQLConnection connection)” is there. This function is made public in the ESQLQuery class, so
that developer can access it from object itself. It’s very important to call the “public void
fetchParams()” which will read the named params from the commandText. After this you can
set the params one by one using addParams(ParamName, Value) function. Now the input are
all set, and you can call the open() function. This will connect to the corresponding
ESQLConnection and fetch data from the databas. After this we can use access the data with the
help of function fieldByName(FieldName) which returns EField class. The EField has many
function to fetch data as String, Object, Blob, Integer etc. Also this class will give basic
information about the field such as field type [String, Integer, Currency etc.], auto incremental
etc. In ESQLQuery this will be fetching each time from db when you call the functions.
E.G.:-
final ESQLQuery query = new ESQLQuery(); query.setSQLConnection(connection); query.setCommandText("SELECT UM.USERID, UM.USERNAME, UM.USERFULLNAME, UM.USEREMAIL, BM.BRANCHNAME FROM USERMASTER UM LEFT JOIN BRANCHMASTER BM ON UM.BRANCHID = BM.BRANCHID WHERE (UM.USERNAME = ?USERNAME OR ?USERNAME = '*****ALL')"); query.fetchParams(); //query.addParamByName("USERNAME", "*****ALL"); query.addParamByName("USERNAME", "r"); try { query.open();
System.out.println(query.getFieldByName("USERFULLNAME").asString());
} catch(final AuthenticationException e) { } catch(final SQLException e) { } catch(final ParserConfigurationException e) { } catch(final EDataControlException e) { } catch(final ClassNotFoundException e) {}
EClientDataSet:
The EClientDataSet is also derived from the EDataSet class. So the basic function of
EDataSet such as DataSetListener, fetchParams, getFieldByName etc. will be included in this
class too. Apart from this, in class EClientDataSet the data is stored in the local memory after
Java Database Classes
E-Software Presents
Muhammad Ajmal P
5
open () function is called. This is how this class is used in 3 Tier instead of the ESQLQuery. This
class has additional methods. They are,
public void setRemoteServer(final ESoftServer_Intf server_Intf)
public void setDataSetProviderName(final String DataSetProviderName)
For connecting to database in 2 Tier [ESQLQuery] we used setSQLConnection function. In 3
Tier the client app will not directly connect to the database but connect to another application
say Server application. The Server application will fetch data from the database and send it to
the client. Normally, this process is carried out by writing function in the server application and
access or calls these functions at the client side. In this case we will have to write so many
functions at the server side and have to update both the server application and client
application for a minute change. More over this writing the server function is more complicated.
And such complicated works should be handled by most experienced developer to avoid
hacking. So in Delphi [Software Language] we have Client database controls which will fetch
data from the server database controls via proper channel [such as XmlRpc, JSON-Rpc, SOAP
etc.] and store in the local memory. The sql query will be set at the client side itself. So we can
change this at any time without updating server application. As a Delphi Software developer
I’m trying my best to port this to java. A Delphi software developer will understand this concept
very easily.
I’m using the RMI [Remote Method Invocation] for 3 Tier. The most important feature of
java is anything in java can be converted to Object and an object can be cast back to its
corresponding class. Along with this java can save the object to a file, if that object can be
serialized. Refer Java Serialization for details. The next is, a serialized object can not only be
saved to a file but also we can send it via RMI to another java application [Some android
developers ported the RMI package to android too.].
I’m utilizing this feature for 3 Tier. First I checked the database driver classes. Some of
them where not serialized. Since the java ResultSet is an interface which will connect to db
driver only, also it was not possible to send this via RMI. So I planned to create some classes
which will store the metadata and data. Then I created 3 classes EResultSet,
EResultSetMetaData, ERowMetaData and ECField. Here the EResultSet and EResultSetMetaData
implement “Serializable” the ResultSet and ResultSetMetaData correspondingly and the ECFiled
and ERowMetaData store the data and metadata correspondingly.
After setting the RemoteServer and Provider [explain later], you can call open function.
This function will fetch data from the server application for further usage. At server, when a call
event is happened, the server application should fetch data from the database and store it
inside the above classes. After that these classes should send back to the client application. This
will be documented in “Portable Application Server” documentation [Java server application
Java Database Classes
E-Software Presents
Muhammad Ajmal P
6
which can connect to 3 databases]. We should add a EDataSetProvider in the server application
for each ESQLQuery there. And the name of these providers should be set in the EClientDataSet
so that the EClientDataSet will fetch data from the corresponding database.
E.G.:-
final EClientDataSet query = new EClientDataSet(); query.setRemoteServer(serverIntf); query.setDataSetProviderName("PROVFIRST"); query.setCommandText("SELECT UM.USERID, UM.USERNAME, UM.USERFULLNAME, UM.USEREMAIL, BM.BRANCHNAME, UM.USERIMAGE FROM USERMASTER UM LEFT JOIN BRANCHMASTER BM ON UM.BRANCHID = BM.BRANCHID WHERE (UM.USERNAME = ?USERNAME OR ?USERNAME = '*****ALL')"); query.fetchParams(); query.addParamByName("USERNAME", "*****ALL"); query.open();
Java Database Classes
E-Software Presents
Muhammad Ajmal P
7
EDataSource:
The EDataSource is used to connect the visible database control and the DataSet. After
fetching data from the database, it’s very difficult to set the data in the TextFields, CheckBoxes,
Comboboxes etc. each time when the data changesand when row changes. So I used a
EDataSource class. This will be used with the DataControl classes to link the DataControl classes
with the DataSet.
E.G.:-
final EDataSource dataSource = new EDataSource(); dataSource.setDataSet(query);
EDataSetProvider:
The EDataSetProvider is used in the server side for connecting the EClientDataSet with
the proper database via proper dataset. This will be well explained in my “Portable Server
Application” project. In the server application, all the EDataSetProviders to be exported should
be added to the DataSnap class [Using addDataSetProvider(EDataSetProvider, ProviderName)
method]. In this, there is an option to allow the EClientDataSet to change the commandText
[SQL Query]. This is set true default. If this is set false then the query in the corrensponding
ESQLQuery will be accepted. This is the format in the Delphi programming language.
Java Database Classes
E-Software Presents
Muhammad Ajmal P
8
3. DataControls:
This package includes all the database visible classes. All these class Implements
EDBControl. This Interface has methods “SetDataField and SetDataSource” with which we can
connect these classes to database very easily and fetch data automatically. Earlier we were
discussing about connecting to database and storing fetched data in local memory. Now let us
try to push this data to the visual controls automatically, so that there is no need of code for
changing data while the selected row to the database changes. I.e. usually we fetch data from
database and push it to visual classes such as JTextField, JCheckBox etc. Later if user selected
next record from the database then we fetch that record and push it to visual classes. This will
be more complicated in the case of large database applications.
By using the DataControls, this will happen automatically in these classes. For this we
connect the EdataSource to these classes and the database table field as the dataField. After
this, when the record number of the EDataSet is changed then the corresponding EDataSource
will be alerted to update the data in all the visual classes.
The classes are below,
EDBCheckBox:
This class is derived from the JCheckBox and EDBControl. This has two properties
ValuChecked and ValueUnChecked. In some cases we store ‘Y’ for true and ‘N’ for false or 0 and
1. In this case, ‘Y’ should be assigned to ValueChecked via “setValueChecked” method and ‘N’ to
“ValueUnChecked” method via “setValueUnChecked” method.
EDBGrid:
This class is derived from the JTable and EDBControl.
EDBImage:
This class is derived from the EImage and EDBControl.
EDBLookupComboBox:
This class is derived from the JComboBox and EDBControl. This can store one complete
field of the database in it as list and connect to database as data. I.e. in this we can connect two
EDataSource one to connect to the current record and the other to show a list of data which
should be displayed. This can be well explained using an example.
Take the case of displaying the country for some purchase. The list of country is stored
in “COUNRTYMASTER” table with its primary key as “COUNRTYCODE” and country as
“COUNTRYNAME”. Now fetch this data using “SQLQueryCountryList” and connect it to
LookupComboBox using “DataSourceCountryList” which is connected to SQLQueryCountryList.
Java Database Classes
E-Software Presents
Muhammad Ajmal P
9
Now set the “ListSource” of LookupComboBox as DataSourceCountryList, “ListField” as
COUNTRYNAME and “KeyField” as COUNTRYCODE. This is explained in TestClientApplication.
There is a “DisplayEmpty” and “EmptyValue” properties. The EmptyValue will be
returned from the “getValue()” function if DisplayEmpty is selected.
E.G.:-
final EDBLookupComboBox lookupComboBox = new EDBLookupComboBox(); lookupComboBox.setDisplayEmpty("<Select All>"); lookupComboBox.setEmptyValue("**ALL"); lookupComboBox.setListSource(dataSource); lookupComboBox.setListField("USERFULLNAME"); lookupComboBox.setKeyField("USERID");
EDBRadioButton:
This class is derived from the JRadioButton and EDBControl.
EDBTextField:
This class is derived from the JTextField and EDBControl.
Java Database Classes
E-Software Presents
Muhammad Ajmal P
10
4. DataSnap:
This includes one interface file and one class file for 3 tier access. They are,
ESoftServer_Intf:
This is an Interface. This is used for 3 tier RMI database data transfer. Almost all the
Web-Service follows the method of Interface and Implementation. All the declarations will be
done in the Interface file and the Definition in the Implementation file. So the Interface file can
be easily transferred and converted to other language. Most of the languages support the WSDL
format. The development tool converts the Interface to WSLD and vice versa.
In our Interface, we have only four methods. The “getTestCode” method, which returns
“Test Ok” string for checking the connectivity at the development time. The method
“Authenticate(AuthCode)” is for client server handshaking. The method
“getDataSetProviderByName(ProviderName)” for accessing the corresponding
EDataSetProvider at the server. The provider name in the EClientDataSet will be send to this
method to get the corresponding EDataSetProvider. The last is the “getEFields(EClientDataSet)”
method. We use this method to send the EClientdataSet to the server. From there we fetch the
data and this method returns the ResultSet.
ESoftServer_Impl:
This is the implementation file for the ESoftServer_Intf Interface. This is also used for
the 3 tier RMI database data transfer. Almost everything is explained above. This class
implements the ESoftServer_Intf interface.
E.G.:-
final ESoftServer_Intf server_Intf = new ESoftServer_Impl(); ((ESoftServer_Impl) server_Intf).addDataSetProvider(providerFirst, "ProvFirst ");
Registry registry = LocateRegistry.createRegistry(8902); registry.bind("ESoftService.Mobile", server_Intf); registry.unbind("ESoftService.Mobile”); // On Application Exit