|
Adding the Query to the Form
You now need to modify MyDVDStoreAdmin.java to add the query strings and create the methods to construct and invoke a query that incorporates the input variables. You also need to modify the button event handler to invoke the correct query and add a method to display the query results in the table. Open MyDVDStoreAdmin.java and click the Source tab.
Add the following query strings (in bold) to the class.
|
The following is the screenshot for the code. It is possible to copy the queries from the HQL Query Editor tabs into the file and then modify the code.
Add the following methods to create the query based on the user input string. The methods call a method called executeHQLQuery() and create the query by combining the query string with the user entered search string.
private void runQueryBasedOnFirstName() {
executeHQLQuery(QUERY_BASED_ON_FIRST_NAME + firstNameTextField.getText() + "%'");
}
private void runQueryBasedOnLastName() {
executeHQLQuery(QUERY_BASED_ON_LAST_NAME + lastNameTextField.getText() + "%'");
}
The following is the screenshot. Just leave the error at this stage we will add the executeHQLQuery() method in the next step.
Add the executeHQLQuery() method. The executeHQLQuery() method calls Hibernate to execute the selected query. This method makes use of the HibernateUtil.java utility class to obtain the Hibernate Session.
private void executeHQLQuery(String hql) {
try {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query q = session.createQuery(hql);
List resultList = q.list();
displayResult(resultList);
session.getTransaction().commit();
} catch (HibernateException he) {
he.printStackTrace();
}
}
The following is the screenshot for the code.
Fix your imports to add import statements for the Hibernate libraries (org.hibernate.Query and org.hibernate.Session) and java.util.List.
--------------------------------------------------------
------------------------------------------------------
Just leave the last error as is. We will add the displayResult() method later.
Create a Query button event handler by switching to the Design view and double-clicking the Query button or select the button > right-click mouse > select Events menu > select Action menu > select actionPerformed menu.
The IDE creates the queryButtonActionPerformed() method and displays the method in the Source view. Modify the queryButtonActionPerformed() method in the Source view by adding the following code so that a query is run when the user clicks the button.
private void queryButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(!firstNameTextField.getText().trim().equals("")) {
runQueryBasedOnFirstName();
} else if(!lastNameTextField.getText().trim().equals("")) {
runQueryBasedOnLastName();
}
}
The following is the screenshot for the code.
Add the following method to display the results in the JTable table.
private void displayResult(List resultList) {
Vector<String> tableHeaders = new Vector<String>();
Vector tableData = new Vector();
tableHeaders.add("ActorId");
tableHeaders.add("FirstName");
tableHeaders.add("LastName");
tableHeaders.add("LastUpdated");
for(Object o : resultList) {
Actor actor = (Actor)o;
Vector<Object> oneRow = new Vector<Object>();
oneRow.add(actor.getActorId());
oneRow.add(actor.getFirstName());
oneRow.add(actor.getLastName());
oneRow.add(actor.getLastUpdate());
tableData.add(oneRow);
}
resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
}
The following is the screenshot for the code.
Fix your imports to add java.util.Vector and save your changes.
After you save the form you can run the project.