16.10.2015 Views

Getting Startedwith pureQuery

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10 – Inline Programming Style 197<br />

}<br />

+ "MGRNO = ?1.employeeNumber AND DEPTNO = ?1.departmentNumber)) "<br />

+ "AND e.WORKDEPT = (SELECT ADMRDEPT FROM DEPARTMENT "<br />

+ "WHERE DEPTNO = ?1.departmentNumber) AND e.EMPNO = d.MGRNO))",<br />

EmployeeBean.class, employee);<br />

Listing 10.13 - Add code to the selectEmployeeManager method to determine the<br />

manager of an employee<br />

10.4.2.2 Test the "Company Employees" screen<br />

Save and test your application. Supply 000030 when the Human Resources Manager<br />

asks for your employee number. On the main menu, select option 1 to display information<br />

about Sally Kwan.<br />

10.5 Executing SQL statements that modify information in a database<br />

The <strong>pureQuery</strong> inline-method style provides methods named update and updateMany<br />

for executing SQL statements that modify the information stored in database tables. These<br />

methods can execute SQL statements that do not return query results, such as INSERT,<br />

UPDATE, and DELETE.<br />

The Data interface contains two update methods for executing a single SQL statement<br />

one time, as well as three methods for updating an SQL statement multiple times.<br />

10.5.1 Executing an SQL statement one time by using the method: int<br />

update (String sql, Object... parameters)<br />

The method int update (String sql, Object... parameters) executes the<br />

specified SQL statement and returns an int indicating how many rows were updated.<br />

Listing 10.14 shows an example of using this method.<br />

EmployeeBean newEmployee = new EmployeeBean ();<br />

newEmployee.setEmployeeNumber ("010010");<br />

newEmployee.setFirstName ("FirstName");<br />

newEmployee.setLastName ("LastName");<br />

newEmployee.setEducationLevel (18);<br />

int updateCount = data.update (<br />

"INSERT INTO EMPLOYEE (EMPNO, FIRSTNME, LASTNAME, EDLEVEL)<br />

VALUES (:employeeNumber, :firstName, :lastName, :educationLevel)",<br />

newEmployee);<br />

System.out.println ("Update count: " + updateCount);<br />

Listing 10.14 - An example of executing the update method

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!