Presentation is loading. Please wait.

Presentation is loading. Please wait.

DataBases and SQL INFSY 547 Spring 2007. Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:

Similar presentations


Presentation on theme: "DataBases and SQL INFSY 547 Spring 2007. Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:"— Presentation transcript:

1 DataBases and SQL INFSY 547 Spring 2007

2 Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19: Review Form ideas, Team Meetings and presentation guidelines April 26: Project Due Date Develop presentations May 3: Final Presentation and Party

3 Servlet Processing  Process a database (similar to an.xml file)  Output an.html file  Interact with Oracle via a servlet

4 In SQL Plus

5 Lab 10: Start with Eclipse  Create a project in Eclipse – (Infsy547OracleCustomerLab.java)  External.jar files classes12.jar servlet-api.jar

6 Lab 10: Start with Eclipse  Create a package – infsy547oraclecustomerlab  Create a class – by the same name as the project  Add import statements import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*;

7 Servlet Assuming Names Table public class Infsy547OracleCustomer extends HttpServlet { private Connection connection; private PreparedStatement insertRecord; private int acctNumber; private PrintWriter out; Names acctNumber* nameaddresscitystate

8 public void init( ServletConfig config ) throws ServletException { }

9 private void getConnected () { try { Class.forName( "oracle.jdbc.driver.OracleDriver" ); connection = DriverManager.getConnection ("jdbc:oracle:thin: @146.186.84.66:1521:CLDB", “ ",“ "); // PreparedStatement to add a customer record to names table insertRecord = connection.prepareStatement ( "INSERT INTO names (name, address, city, state, accountnumber) " + "VALUES(?, ?, ?, ?, ?)"); } catch(ClassNotFoundException cnf) { System.err.println(cnf.getMessage()); cnf.printStackTrace(); } catch (SQLException sqlex) { System.err.println(sqlex.getMessage()); sqlex.printStackTrace(); } } Loads the driver for Oracle

10 protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // set up response to client response.setContentType( "text/html" ); out = response.getWriter(); // start XHTML document out.println( " " ); out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); out.println(" " ); // head section of document out.println( " " ); acctNumber = Integer.parseInt(request.getParameter("accountNumber")); name = request.getParameter("Name"); out.println( " Thank you! " ); out.println( " " ); out.println( " Thank you for adding your record." ); out.println( " " ); out.close(); }

11 public void insertRecord () { try { // update total for current survey response insertRecord.setString( 1, name ); insertRecord.setString( 2, address ); insertRecord.setInt( 5, acctNumber); insertRecord.executeUpdate(); } catch ( SQLException sqlException ) { sqlException.printStackTrace(); out.println( " Error " ); out.println( " " ); out.println( " Database error occurred. " ); out.println( "Try again later. " ); out.close(); } Note: Will not complete the html on previous page if this occurs.

12 @Override public void destroy() { // attempt to close statements and database connection try { insertRecord.close(); connection.close(); } catch( SQLException sqlException ) { sqlException.printStackTrace(); } } // end of destroy method

13 ● Exceptions represent error conditions ● Exceptions isolate the code that deals with error situations ● Throwing: the act of detecting an abnormal condition  Searches for a handler  catches the exception ● Try Block: Encloses one or more java statements Exception Terminology

14 HTML Insert Customer Account Number

15 Complete Place files appropriately in TomCat structure Add to web.xml Start TomCat Start you servlet

16 .java file from this lab –uploaded to the DataBase Connectivity Drop Box –In ANGEL under the Lessons Tab.html file –Upload into HTML Drop Box


Download ppt "DataBases and SQL INFSY 547 Spring 2007. Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:"

Similar presentations


Ads by Google