Current Topic: 4.4.4.6.3.Get and Set Data with Data Service
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Get and Set Data with Data Service Framework
Data Service framework supports data handling with:
- getData - methods - for select operations
- setData - methods - for insert, update and delete operations.
There are two ways of handling data in Data Service framework:
- with Hibernate and Java Persistance API (JPA) - the prefered way
- with just JDBC connection pool - no Hibernate/JPA
In the latest case the methods are usually have distinguished names, like getDataWithConnection() and setDataWithConnection().
The next section describes how Data Service works with Hibernate and Java Persistance API (JPA) frameworks.
Here is the sample of a select statement with question mark:
select name from accounts where userID = ? and password = ?
This statement can be stored with the name, like "getNameFromAccounts.sql" and will be handled with this code sample:
// assuming there are values for userID and password
String[] runTimeParams = new String[] {userID, password};
String sqlName = "getNameFromAccounts";
String dsName = "MyProject/sampleDB"; // dsName usually consists of appName/originDsName; appName is a projectName
String sql = DataService.getSql(sqlName, dsName);
List
The select statement can look a bit different, although it is also a very common way for building SQL:
select name from accounts where userID = :userID and password = :password
Was it clear so far?
This statement can be stored with the same name "getNameFromAccounts.sql", so we replace old SQL with the new one.
In this case instead of using sequence of run-time parameters, we use the map and this code sample:
// assuming there are values for userID and password
HashMap map = HashMap();
map.put("userID", userID);
map.put("password", password);
String sqlName = "getNameFromAccounts";
String dsName = "MyProject/sampleDB"; // dsName usually consists of appName/originDsName; appName is a projectName
String sql = DataService.getSql(sqlName, dsName);
List
Both ways are equally good for handling data operations.
A very similar approach is used for handling insert, update and delete operations with setData() and setDataWithConnection() methods.
These methods usually return the number of affected records.
Assignments:
Get the latest library:
http://ITUnivrsity.us/downloads/com.its.util.zip
Look for the updated Javadoc:
http://ITUnivrsity.us/javadoc/index.html
Create the Javadoc documentation for your own project:
<br/>// assuming there are values for userID and password
<br/>String[] runTimeParams = new String[] {userID, password};
<br/>String sqlName = "getNameFromAccounts";
<br/>String dsName = "MyProject/sampleDB"; // dsName usually consists of appName/originDsName; appName is a projectName
<br/>String sql = DataService.getSql(sqlName, dsName);
<br/>List<Object[]> records = DataService.getDataWithConnection(sql, runTimeParams, dsName, "system"); // this SQL is usually used by "system"
<br/>String name = null;
<br/>if(records != null) {
<br/> Object[] recordFields = records.get(0); // get first record
<br/> name = recordFields[0]; // get first field
<br/>}
<br/>
The select statement can look a bit different, although it is also a very common way for building SQL:
select name from accounts where userID = :userID and password = :password
Was it clear so far?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=478&intro=general&group=aitu&ur=f'">
This statement can be stored with the same name "getNameFromAccounts.sql", so we replace old SQL with the new one.
In this case instead of using sequence of run-time parameters, we use the map and this code sample:
<br/>// assuming there are values for userID and password
<br/>HashMap<String, Object> map = HashMap<String, Object>();
<br/>map.put("userID", userID);
<br/>map.put("password", password);
<br/>String sqlName = "getNameFromAccounts";
<br/>String dsName = "MyProject/sampleDB"; // dsName usually consists of appName/originDsName; appName is a projectName
<br/>String sql = DataService.getSql(sqlName, dsName);
<br/>List<Object[]> records = DataService.getDataWithConnection(sql, map, dsName, "system"); // this SQL is usually used by "system"
<br/>String name = null;
<br/>if(records != null) {
<br/> Object[] recordFields = records.get(0); // get first record
<br/> name = recordFields[0]; // get first field
<br/>}
<br/>
Both ways are equally good for handling data operations.
A very similar approach is used for handling insert, update and delete operations with setData() and setDataWithConnection() methods.
These methods usually return the number of affected records.
Assignments:
Get the latest library:
http://ITUnivrsity.us/downloads/com.its.util.zip
Look for the updated Javadoc:
http://ITUnivrsity.us/javadoc/index.html
Create the Javadoc documentation for your own project: