Outline of JSP and Access
Download
Report
Transcript Outline of JSP and Access
Java, Access, SQL, HTML
Three-tier architecture involves:
Client - Browser
Server - Tomcat
- Server-side language - JSP
Database - Access
could just as well be:
Server - IIS
Database - MySQL
- Server-side language - PHP
Key Issues
How to register database so Java can locate it.
How to access Java SQL methods.
How to connect to a database.
How to use SQL to Insert a record in data base.
How to retrieve records using SQL Select query.
How to process and output Select results.
How to access HTML Form data.
How to register database so Java can locate it.
Use Control panel > Admin tools > ODBC Data sources etc
Data Source Name (DSN) can be whatever you want
DSN does not have to be same as file name
How to identify required Java SQL methods
Place at top of JSP page:
Library of java objects,
methods that can
handle SQL
<%@ page import = "java.sql.*" %>
How to Connect to database
simplified:
Tags to enclose Java
Loads software drivers
that can talk to database
<%
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connects to
Data Source Frank
Connection conn=null;
conn = DriverManager.getConnection("jdbc:odbc:Frank", "", "");
out.println ("Database Connected.");
%>
Embedded in HTML
response.
Java try-catch syntax is omitted for simplicity
SQL to Insert a record in database
Make Statement
object (once)
Make SQL
query string s
Statement stm = conn.createStatement();
String
s = "INSERT INTO Managers VALUES ('Charlie') ";
Execute query
stm.executeUpdate(s);
Close database
conn.close();
out.println ("Database closed.");
- Java SQL Overview -
These are mainly
repetitious boilerplate.
Loads Driver to Connect to database
Class.forName
Executes driver
DriverManager.getConnection
Makes Connection object
conn= DriverManager.getConnection
Makes Statement object
String s
Execute query
stm= conn.createStatement()
s = "Insert into ...
"
stm.executeUpdate ( s )
How to retrieve records using Select query.
Make Statement
object - once !
Statement stm = conn.createStatement ( );
Make SQL string s
String
s = "SELECT * FROM Managers";
Execute s
ResultSet
r = stm.executeQuery(s);
Save results in ResultSet r
Like a table retrieved
by Select query
How to process results.
ResultSet
...from previous slide
r = stm.executeQuery(s);
Get next row
of results
while ( r.next( ) )
{
out.print
("<br>Name: " + r.getString ("name") );
out.println ("
Age : "
}
Get attribute
values by name
+ r.getString ("age" ) );
Navigation thru results
name age
next row
of table
ResultSet
r = stm.executeQuery(s);
while ( r.next( ) )
{
out.print
p11
22
p13
37
p12
35
Fails at end of table
("<br>Name: " + r.getString ("name") );
}
Gets named column
in current row !
Navigation – behavior of next ( )
name age
next marches
thru table rows
p11
22
p13
37
p12
35
while ( r.next( ) )
{
out.print
("<br>Name: " + r.getString ("name") );
}
next ( ) is true if it
points to a row
next ( ) is false if no
more rows to point to
gets data from
current row
How to access HTML Form data
Get input with
HTML field name !
Java variable
String
name
= request.getParameter ("mName");
name
= "'" + name + "'" ;
SQL needs quotes
around values
String
s = "INSERT INTO Managers VALUES ("
+name+ ")"
stm.executeUpdate(s);
Make SQL query
Variable input data
mName
request.getParameter("mName")
HTML form
String
Insert into Managers ( etc )
name
Java Program
name age
Managers'
Table
name attribute
database
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.
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.
Other approaches involve cookies or session information or background databases.
User-1
User-2
server
Question-for-1
Question-for-2
Remembering who the server is talking too...one approach
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