Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java, Access, SQL, HTML. Three-tier architecture involves: Client - Browser Server - Tomcat Database - Access - Server-side language - JSP could just.

Similar presentations


Presentation on theme: "Java, Access, SQL, HTML. Three-tier architecture involves: Client - Browser Server - Tomcat Database - Access - Server-side language - JSP could just."— Presentation transcript:

1 Java, Access, SQL, HTML

2 Three-tier architecture involves: Client - Browser Server - Tomcat Database - Access - Server-side language - JSP could just as well be: Server - IIS Database - MySQL - Server-side language - PHP

3 How to connect to a database. How to access Java SQL methods. How to use SQL to Insert a record in data base. How to access HTML Form data. How to retrieve records using SQL Select query. How to process and output Select results. How to register database so Java can locate it. Key Issues

4 How to register database so Java can locate it. Use Control panel > Admin tools > ODBC Data sources etc DSN does not have to be same as file name Data Source Name (DSN) can be whatever you want

5 How to identify required Java SQL methods Place at top of JSP page: Library of java objects, methods that can handle SQL

6 How to Connect to database simplified: <% %> Loads software drivers that can talk to database Connects to Data Source Frank Java try-catch syntax is omitted for simplicity Connection conn=null; Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:Frank", "", ""); out.println ("Database Connected."); Embedded in HTML response. Tags to enclose Java

7 SQL to Insert a record in database conn.close(); out.println ("Database closed."); Statement stm = conn.createStatement(); String s = "INSERT INTO Managers VALUES ('Charlie') "; Make Statement object (once) Make SQL query string s Execute query Close database stm.executeUpdate(s);

8 Loads Driver to Connect to database Executes driver Makes Connection object Makes Statement object String s Execute query - Java SQL Overview - These are mainly repetitious boilerplate. Class.forName DriverManager.getConnection conn= DriverManager.getConnection stm= conn.createStatement() s = "Insert into... " stm.executeUpdate ( s )

9 How to retrieve records using Select query. String s = "SELECT * FROM Managers"; Statement stm = conn.createStatement ( ); Make Statement object - once ! Save results in ResultSet r Make SQL string s ResultSet r = stm.executeQuery(s); Execute s Like a table retrieved by Select query

10 How to process results. while ( r.next( ) ) { } out.print (" Name: " + r.getString ("name") ); out.println (" Age : " + r.getString ("age" ) ); Get next row of results Get attribute values by name ResultSet r = stm.executeQuery(s);...from previous slide

11 Navigation thru results while ( r.next( ) ) { } out.print (" Name: " + r.getString ("name") ); next row of table Gets named column in current row ! ResultSet r = stm.executeQuery(s); nameage p1122 p1337 p1235 Fails at end of table

12 next ( ) is false if no more rows to point to next ( ) is true if it points to a row Navigation – behavior of next ( ) while ( r.next( ) ) { } out.print (" Name: " + r.getString ("name") ); next marches thru table rows nameage p1122 p1337 p1235 gets data from current row

13 How to access HTML Form data String name = request.getParameter ("mName"); name = " ' " + name + " ' " ; String s = "INSERT INTO Managers VALUES (" stm.executeUpdate(s); Get input with HTML field name ! Make SQL query Java variable SQL needs quotes around values +name+")" Variable input data

14 mName HTML form Java Program String name Insert into Managers ( etc )request.getParameter("mName") database Managers' Table name attribute nameage

15 A simple approach is to use hidden html fields Where the server sends you data that you transparently send back when you complete the Form. Remembering who the server is talking too...one approach When you contact a server again It does not remember its previous interaction with you Unless you make that happen. Other approaches involve cookies or session information or background databases.

16 server User-1User-2 Question-for-1Question-for-2

17 HTML-1 – form, field, button, action JSP-1 – gets data, send html with hidden fields & data! & new action HTML-2 – form, hidden field, button, action= JSP-2 JSP-2 – gets data, sends to browser Remembering who the server is talking too...one approach


Download ppt "Java, Access, SQL, HTML. Three-tier architecture involves: Client - Browser Server - Tomcat Database - Access - Server-side language - JSP could just."

Similar presentations


Ads by Google