Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 4. Web Apps Frameworks >> 4.4. Spring with Apache Maven and Data Service Frameworks >> 4.4.4.Data Service Framework and Troubleshooting >> 4.4.4.6.Data Service Library
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 records = DataService.getDataWithConnection(sql, runTimeParams, dsName, "system"); // this SQL is usually used by "system"
String name = null;
if(records != null) {
Object[] recordFields = records.get(0); // get first record
name = recordFields[0]; // get first field
}


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 records = DataService.getDataWithConnection(sql, map, dsName, "system"); // this SQL is usually used by "system"
String name = null;
if(records != null) {
Object[] recordFields = records.get(0); // get first record
name = recordFields[0]; // get first field
}


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:

javadoc -d c:\MyWorkspace\MyProject\src\main\webapp\javadoc -sourcepath c:\MyWorkspace\MyProject\src\main\java yourPackage1 yourPackage2

Provide samples of setData and setDataWithConnection and email to dean@ituniversity.us
Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<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?


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:

javadoc -d c:\MyWorkspace\MyProject\src\main\webapp\javadoc -sourcepath c:\MyWorkspace\MyProject\src\main\java yourPackage1 yourPackage2

Provide samples of setData and setDataWithConnection and email to dean@ituniversity.us
Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy