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,"root", "ibmis4me"); 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); } }