Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP program that interacts with HTML form & Access Data Base.

Similar presentations


Presentation on theme: "JSP program that interacts with HTML form & Access Data Base."— Presentation transcript:

1 JSP program that interacts with HTML form & Access Data Base

2 Warning: This JSP program interacts with an HTML form The form sends data to the server and requests the server to compile/execute the JSP program The html form is not described here! – but don’t execute the program without the form or it will crash.

3 Java is for processing data & accessing data base HTML is for displaying results nicely When programming JSP page: write JAVA until need to output some HTML After HTML segment is done: start the Java again – with tags …and so on …then stop the Java – with tags …and do the HTML

4 JSP program Load IO driver & connect to Access data base Create Table in data base with suitable attributes using SQL query – in try/catch Retrieve DB data using SELECT SQL for ResultSet Enter while loop that iterates over ResultSet rows inside while loop: Use next ResultSet row's data to setup next row of table Setup 1 st row of HTML table INSERT form data into Access DB Retrieve & prep [text & integer] data from HTML form Retrieve DB data & display in HTML table Retrieve form data & insert in DB Make DB tables code follows Connect to DB

5 String url = "jdbc:odbc:Model1"; String username=""; String password=""; Connection conn=null; String classPath = "sun.jdbc.odbc.JdbcOdbcDriver"; <% out.println(“ Before driver loaded ”); to import SQL classes for Java tag to start Java segment one way to output Html DB named Model1 uses MSAccess

6 conn = DriverManager.getConnection ("jdbc:odbc:Model1", "", ""); try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); catch (Exception e) { out.println(e.toString() + " Error for driver. "); } out.println("After driver loaded: "); System.out.println ("Driver now loaded. "); System.out.println ("Database now connected. br>"); }

7 try { ………. } catch (Exception e) { out.println(e.toString() + " Error for driver. "); } try-catch construct - useful for preventing program from crashing when error is detected here during execution. e tells what caused error - also during development

8 Statement stm = conn.createStatement(); String Query1; Query1 = "CREATE TABLE Managers (teamID integer, managerName char(15) )"; try { stm.executeUpdate(Query1) ; out.println("Managers table was successfully created. "); } catch (Exception exc) { out.println("Managers table already exists. "); } stm object required SQL query string defined first query string executed executed if error in SQL

9 String s = request.getParameter("mName"); s = " ' " + s + " ' "; try { stm.executeUpdate( " INSERT INTO Managers VALUES ( " + n + ", " + s + " ) ” ); } String ss = request.getParameter("tId") ; ss = ss.trim(); int n = Integer.parseInt(ss); catch(Exception exc) { out.println(exc + " SQL error in INSERT. "); } get Html form data from mName field get numeric data from Html tId field

10 String Query2 = "SELECT teamID, managerName " + "FROM Managers " + "WHERE teamId = 2 " ; try { ResultSet rst; rst = stm.executeQuery (Query2); %> SQL query retrieval begins this way: and saves answer in ResultSet – processed & displayed as detailed on next slide:

11 String Query2 = "SELECT teamID, managerName " + "FROM Managers " + "WHERE teamId = 2 " ; try {ResultSet rst; rst = stm.executeQuery (Query2); %> <% } //end of while %> <% while(rst.next()) { %> teamId mName gets data starts Html table iteratively outputs table rows

12 <% } //end of while %> <% while(rst.next()) { %> teamId mName …table starts

13 String Query2 = "SELECT teamID, managerName FROM Managers WHERE teamId = 2"; try { ResultSet rst; rst = stm.executeQuery(Query2); %> <% } catch(Exception exc) { out.println(exc + " Query error in SELECT. "); } stm.close(); conn.close(); %> teamId mName Here java defers to html HTML for 1 st row of table is hardwired JSP loop starts …then defers to html HTML – alternates with JSP expressions JSP loop finally ends HTML table tag closes JSP – try finally closes & rest of JSP program Alternating JSP and HTML – driven by what has to be done to solve problem

14 <% String url = … try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { out.println(e.toString() + " Error for driver. "); } Statement stm = conn.createStatement(); String Query1; Query1 = "CREATE TABLE Managers (teamID integer, managerName char(15) "; try { stm.executeUpdate(Query1);} catch(Exception exc) { out.println("Managers table already exists. "); } String s = request.getParameter("mName"); s = " ' " + s + " ' "; String ss = request.getParameter("tId") ; ss = ss.trim(); int n = Integer.parseInt(ss); try { stm.executeUpdate("INSERT INTO Managers VALUES (" +n+ ", " + s + " )"); } catch(Exception exc) { out.println(exc + " SQL error in INSERT. "); } String Query2 = "SELECT teamID, managerName FROM Managers WHERE teamId = 2"; try { ResultSet rst; rst = stm.executeQuery(Query2); %> teamId mName <% } catch(Exception exc) { out.println(exc + " Query error in SELECT. "); } stm.close(); conn.close(); %> JSP red Html blue

15 Notes: String s = request.getParameter("mName"); Java program gets data from the Html form with form-field named mName using following method: Java program gets data from Access Data Base using combination of three techniques: a Select query, a ResultSet,and a getString [or getInt or getDouble] method to retrieve ResultSet values: String Query2 = "SELECT teamID, managerName FROM Managers " + "WHERE teamId = 2 " ; try { ResultSet x; x = stm.executeQuery (Query2); … 1. Select query to select data: 2. ResultSet to save results returned: 3. getString [or getInt or getDouble] methods to get attribute values for row in ResultSet x.


Download ppt "JSP program that interacts with HTML form & Access Data Base."

Similar presentations


Ads by Google