|
|
The same functionality using the SQLProcessorI'll demonstrate the SQLProcessor by making incremental changes to the Java code so you can see the benefits of each step.
package com.devdaily.sqlprocessortests; import java.sql.*; import com.missiondata.oss.sqlprocessor.SQLProcessor; /** * This class shows a simple (but not good) way of using the SQLProcessor. * This class just helps you get warmed up with the syntax. */ public class SQLProcessorDemo1 { Connection conn; public static void main(String[] args) { new SQLProcessorDemo1(); } public SQLProcessorDemo1() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost/coffeebreak"; conn = DriverManager.getConnection(url, "username", "password"); doTests(); conn.close(); } catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());} catch (IllegalAccessException ex) {System.err.println(ex.getMessage());} catch (InstantiationException ex) {System.err.println(ex.getMessage());} catch (SQLException ex) {System.err.println(ex.getMessage());} } private void doTests() { doSelectTest(); doInsertTest(); doSelectTest(); doUpdateTest(); doSelectTest(); doDeleteTest(); doSelectTest(); } /** * note the automatic looping through the ResultSet; * no "while" loop is necessary, it is implied. */ private void doSelectTest() { System.out.println("[OUTPUT FROM SELECT]"); String query = "SELECT COF_NAME, PRICE FROM COFFEES"; SQLProcessor sqlProcessor = new SQLProcessor("The Coffee Break SELECT Test", query) { protected void process(ResultSet rs) throws SQLException { String s = rs.getString("COF_NAME"); float n = rs.getFloat("PRICE"); System.out.println(s + " " + n); } }; sqlProcessor.execute(conn); } private void doInsertTest() { System.out.print("\n[Performing INSERT] ... "); SQLProcessor sqlProcessor = new SQLProcessor("The Coffee Break INSERT Test", "INSERT INTO COFFEES VALUES ('BREAKFAST BLEND', 200, 7.99, 0, 0)"); sqlProcessor.execute(conn); } private void doUpdateTest() { System.out.print("\n[Performing UPDATE] ... "); SQLProcessor sqlProcessor = new SQLProcessor("The Coffee Break UPDATE Test", "UPDATE COFFEES SET PRICE=4.99 WHERE COF_NAME='BREAKFAST BLEND'"); sqlProcessor.execute(conn); } private void doDeleteTest() { System.out.print("\n[Performing DELETE] ... "); SQLProcessor sqlProcessor = new SQLProcessor("The Coffee Break DELETE Test", "DELETE FROM COFFEES WHERE COF_NAME='BREAKFAST BLEND'"); sqlProcessor.execute(conn); } }
Discussion
The As you can see I've kept the methods in the class the same as the JDBC example, but I've changed the contents of each method. Let's dig right in and start looking at each method.
The doSelectTest method
The
The doInsertTest , doUpdateTest, and doDeleteTest methods
The changes to the
With these basic changes in place, let's kick it up a little by taking advantage of what the SQLProcessor can really do for you.
|