Transcript Html form
JSP program that interacts with
HTML form
& Access Data Base
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.
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
…then stop the Java – with <% … %> tags
…and do the HTML
After HTML segment is done:
start the Java again – with <% … %> tags
…and so on
JSP program
Connect to DB
Load IO driver & connect to Access data base
Create Table in data base with suitable attributes
using SQL query – in try/catch
Retrieve & prep [text & integer] data from HTML form
INSERT form data into Access DB
Retrieve DB data using SELECT SQL for ResultSet
Setup 1st row of HTML table
Make DB tables
Retrieve form data
& insert in DB
Retrieve DB data
& display in
HTML table
Enter while loop that iterates over ResultSet rows
inside while loop:
Use next ResultSet row's data to setup next row of table
code follows
<%@ page import="java.sql.*" %>
<%
to import
SQL classes
for Java
tag to start Java
segment
DB named Model1
uses MSAccess
String url = "jdbc:odbc:Model1";
String username="";
String password="";
Connection conn=null;
String classPath = "sun.jdbc.odbc.JdbcOdbcDriver";
out.println(“<font color=red>Before driver loaded</font><br>”);
one way to
output Html
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println ("Driver now loaded. <br>");
conn = DriverManager.getConnection
("jdbc:odbc:Model1", "", "");
System.out.println ("Database now connected. br>");
}
catch (Exception e)
{
out.println(e.toString() + " <br>Error for driver.<br>");
}
out.println("After driver loaded:<br>");
try-catch construct
- useful for preventing program from crashing
when error is detected here during execution.
- also during development
try
{
}
……….
e tells what caused error
catch (Exception e)
{
out.println(e.toString() + " <br>Error for driver.<br>");
}
stm object required
Statement stm = conn.createStatement();
SQL query string defined first
String Query1;
Query1 = "CREATE TABLE Managers (teamID integer, managerName char(15) )";
query string executed
try
{ stm.executeUpdate(Query1) ;
out.println("Managers table was successfully created. <br>");
}
catch (Exception exc)
{ out.println("Managers table already exists. <br>"); }
executed if error in SQL
get Html form data
from mName field
String s = request.getParameter("mName");
s = " ' " + s + " ' ";
String ss = request.getParameter("tId") ;
ss = ss.trim();
get numeric data
from Html tId field
int n = Integer.parseInt(ss);
try
{
stm.executeUpdate("INSERT INTO Managers VALUES (" + n +
}
catch(Exception exc)
{ out.println(exc + " <br> SQL error in INSERT. <br>"); }
", "
+ s +
" )”
);
SQL query retrieval begins this way:
String Query2 = "SELECT teamID, managerName " +
"FROM Managers
"WHERE teamId = 2
" +
" ;
and saves answer in ResultSet – processed & displayed as detailed on next slide:
try
{
ResultSet rst;
rst
%>
= stm.executeQuery (Query2);
String Query2 = "SELECT teamID, managerName " +
"FROM Managers " +
"WHERE teamId = 2 " ;
try
{ResultSet rst;
rst = stm.executeQuery (Query2);
%>
<table border = 5 bgcolor = red>
<tr> <th> teamId </th>
<th> mName </th>
</tr>
gets data
starts
Html table
iteratively outputs
table rows
<%
while(rst.next())
{
%>
<tr>
</td>
<td align=center> <%= rst.getString("teamID")%>
<td align=center> <%= rst.getString("managerName")%> </td>
</tr>
<%
} //end of while
%>
</table>
<table border = 5 bgcolor = red>
<tr> <th> teamId </th>
<th> mName </th>
</tr>
…table starts
<%
while(rst.next())
{
%>
<tr>
<td align=center> <%= rst.getString("teamID")%>
</td>
<td align=center> <%= rst.getString("managerName")%> </td>
</tr>
<%
} //end of while
%>
</table>
Alternating JSP and HTML
– driven by what has to be done to solve problem
String Query2 = "SELECT teamID, managerName FROM Managers WHERE teamId = 2";
try {
ResultSet rst; rst = stm.executeQuery(Query2);
%>
<table border=5 bgcolor = red>
<tr><th>teamId</th> <th>mName</th></tr>
<% while(rst.next()) { %>
<tr>
<td align=center> <%= rst.getString("teamID")%>
</td>
<td align=center> <%= rst.getString("managerName")%> </td>
</tr>
<%
}
%>
</table>
<%
}
catch(Exception exc) { out.println(exc + " <br> Query error in SELECT. <br>"); }
stm.close(); conn.close();
%>
Here java defers
to html
HTML for 1st 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
<%@ page import="java.sql.*" %>
<%
String url = …
try
{ Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception e) { out.println(e.toString() + " <br>Error for driver.<br>"); }
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. <br>"); }
String s = request.getParameter("mName");
String ss = request.getParameter("tId") ;
s = " ' " + s + " ' ";
ss = ss.trim(); int n = Integer.parseInt(ss);
try { stm.executeUpdate("INSERT INTO Managers VALUES (" +n+ ", " + s + " )"); }
catch(Exception exc) { out.println(exc + " <br> SQL error in INSERT. <br>"); }
JSP red
Html blue
String Query2 = "SELECT teamID, managerName FROM Managers WHERE teamId = 2";
try {
ResultSet rst; rst = stm.executeQuery(Query2);
%>
<table border=5 bgcolor = red>
<tr><th>teamId</th> <th>mName</th></tr>
<% while(rst.next()) { %>
<tr>
<td align=center> <%= rst.getString("teamID")%>
</td>
<td align=center> <%= rst.getString("managerName")%> </td>
</tr>
<% } %>
</table>
<%
}
catch(Exception exc) { out.println(exc + " <br> Query error in SELECT. <br>"); }
stm.close(); conn.close();
%>
Notes:
Java program gets data from the Html form with form-field named mName
using following method:
String s = request.getParameter("mName");
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:
1. Select query to select data:
String Query2 = "SELECT teamID, managerName FROM Managers
"WHERE teamId = 2 " ;
" +
2. ResultSet to save results returned:
try
{ ResultSet x;
x = stm.executeQuery (Query2);
…
3. getString [or getInt or getDouble] methods to get attribute values for row in ResultSet x.
<%= x.getInt("teamID")%>
<%= x.getString("managerName")%>