|
|
A Second SQLProcessor Example
We'll look at the power of the SQLProcessor by first looking at the /** * This is our first use of the SQLProcessor "set" methods. * This is much better than using JDBC statements, as the underlying * PreparedStatements keep you from having to worry about SQL Injection hacks. * And the syntax is much more obvious, and less prone to errors, than * the JDBC PreparedStatement syntax. */ private void doInsertTest() { System.out.print("\n[Performing INSERT] ... "); SQLProcessor sqlProcessor = new SQLProcessor("The Coffee Break INSERT Test", "INSERT INTO COFFEES VALUES (|coffeeName|,|supplierID|,|price|,|sales|,|total|)"); sqlProcessor.set("coffeeName","BREAKFAST BLEND"); sqlProcessor.set("supplierID",200); sqlProcessor.set("price",7.99); sqlProcessor.set("sales",0); sqlProcessor.set("total",0); sqlProcessor.execute(conn); }
I've changed the As you can see with this approach, adding variables to your SQL commands is a simple two-step process. First, create the named parameters in your INSERT statement, as shown. Then, set the value of each named parameter in a SQLProcessor set method, such as: sqlProcessor.set("coffeeName","BREAKFAST BLEND"); This approach is simple, readable (hence maintainable), and powerful, putting the field name right next to the desired value. And a great thing about this approach is that the SQLProcessor can help you debug problems much more powerfully than PreparedStatements. How the SQLProcessor Helps You Debug ProblemsTo show how the SQLProcessor helps you debug problems, assume that you made a mistake and "accidentally" commented-out one of your set calls, like this: //sqlProcessor.set("coffeeName","BREAKFAST BLEND"); sqlProcessor.set("supplierID",200); sqlProcessor.set("price",7.99); sqlProcessor.set("sales",0); sqlProcessor.set("total",0); If you try to run the program now you'll obviously get an error, but let's look at the error you get from the SQLProcessor: java.lang.IllegalArgumentException: The parameter |coffeeName| was not set SQL Description: The Coffee Break INSERT Test SQL: INSERT INTO COFFEES VALUES (|coffeeName|,|supplierID|,|price|,|sales|,|total|) at com.missiondata.oss.sqlprocessor.AbstractSQLProcessorBase.prepareStatement(Unknown Source) at com.missiondata.oss.sqlprocessor.AbstractSQLProcessorBase.execute(Unknown Source) Now that's what I call an error message. It's hard to be more descriptive than "The parameter |coffeeName| was not set" in the SQL code you created with the description "The Coffee Break INSERT Test". That's an error message that will help you solve a problem, and as you can see from the code, it's very easy to set up and use. The doUpdateTest and doDeleteTest methods
Once you understand the private void doUpdateTest() { System.out.print("\n[Performing UPDATE] ... "); SQLProcessor sqlProcessor = new SQLProcessor("The Coffee Break UPDATE Test", "UPDATE COFFEES SET PRICE=|price| WHERE COF_NAME=|coffeeName|"); sqlProcessor.set("price", 4.99); sqlProcessor.set("coffeeName", "BREAKFAST BLEND"); sqlProcessor.execute(conn); }
and the following code shows how the private void doDeleteTest() { System.out.print("\n[Performing DELETE] ... "); SQLProcessor sqlProcessor = new SQLProcessor("The Coffee Break DELETE Test", "DELETE FROM COFFEES WHERE COF_NAME=|coffeeName|"); sqlProcessor.set("coffeeName", "BREAKFAST BLEND"); sqlProcessor.execute(conn); }
The The doSelectTest method
You may have noticed that I skipped the |