Name | Description |
Sockets | Low-level socket communications |
Remote Method Invocation (RMI) | Java-to-Java object communications (JDK 1.1) |
Data Access Objects/ Remote Data Objects (DAO/RDO) |
Win32 data access libraries |
Distributed COM (DCOM) | COM-based wire protocol |
Hypertext Transfer Protocol (HTTP) | Connectionless protocol of the Web |
JDBC | Java data access API |
Internet Inter-ORB Protocol (IIOP) | Transport protocol for CORBA objects |
Figure 6 Recordset Types
Type | Constant | Note |
Table | RecordsetTypeEnum | Updateable Jet database table |
Dynaset |
RecordsetTypeEnum. dbOpenDynaset |
Dynamic, updateable result set |
Snapshot | RecordsetTypeEnum | Non-updateable record set |
Forward-only | RecordsetTypeEnum | Forward only snapshot |
Figure 9 Bookstor.htm
<HTML>
<HEAD>
<TITLE>
PCSI HTML Demo
</TITLE>
<SCRIPT Language="JavaScript">
function doAdd()
{
window.location = "AddTitle.asp?title_id=" +
window.baseform.title_id.value
}
function doEdit()
{
window.location = "EditTitle.asp?title_id=" +
window.baseform.title_id.value
}
function doDelete()
{
window.location = "DeleteTitle.asp?title_id=" +
window.baseform.title_id.value
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="Silver">
<IMG SRC="Logo.gif" WIDTH=100 HEIGHT=50 BORDER=0>
<H1>Company Bookstore Inventory</H1>
<HR>
<BR>
<FORM Name="baseform">
Title ID: <INPUT NAME="title_id" TYPE="text">
<A HREF="http://localhost/JavaData/JavaJDBC/ListTitles.asp">ViewTitles</A><BR>
<BR>
<INPUT TYPE="button" VALUE="Add" onClick="doAdd()">
<INPUT TYPE="button" VALUE="Edit" onClick="doEdit()">
<INPUT TYPE="button" VALUE="Delete" onClick="doDelete()">
</FORM>
</BODY>
</HTML>
Figure 12 Start Method
public void start()
{
try
{
// add an instance of the Microsoft driver to the driver manager
// by registering the driver dynamically
// Class.forName loads a Java class.
Class.forName("connect.microsoft.MicrosoftDriver");
// open a db connection
// if the DriverManager can not connect to the url, an
// exception is thrown.
// note: some drivers let you pass the user/password with the URL
// (jdbc:odbc:MyData;UID=joe;password=fung
conn = DriverManager.getConnection(url,"sa","");
// create a Statement object so that you can issue SQL commands.
Statement st = conn.createStatement();
// execute the query and get a ResultSet
ResultSet res = st.executeQuery("select * from titles where title_id = '" +
m_TitleID + "'");
// fetch the first record. This returns false if there are no more records.
res.next();
ctrls.IDC_TITLEID.setText(res.getString("Title_ID"));
ctrls.IDC_TITLE.setText(res.getString("Title"));
String price = res.getString("Price");
StringBuffer sb = new StringBuffer(price);
sb.setLength(price.length() - 2);
price = sb.toString();
ctrls.IDC_PRICE.setText(price);
// close the Statement when finished.
st.close();
}
// general exception handler.
// in a real world situation, you should check for specific errors.
catch(Exception ex)
{
System.out.println("Exception during SQL query: " + ex);
ex.printStackTrace(System.out);
}
}
Figure 13 Stop Method
public boolean action(Event evt, Object arg)
{
if (arg.equals("Yes"))
{
try
{
Statement st = conn.createStatement();
// executeUpdate lets you execute SQL that return no results
st.executeUpdate("DELETE titles WHERE title_id = '"
+ ctrls.IDC_TITLEID.getText() + "'");
getAppletContext().showDocument(new URL(getCodeBase(),
"ListTitles.asp"),"_self");
}
catch (SQLException ex)
{
System.out.println("Exception during SQL query: " + ex);
ex.printStackTrace(System.out);
}
catch (MalformedURLException ex)
{
System.out.println("Exception in URL: " + ex);
ex.printStackTrace(System.out);
}
return true;
}
if (arg.equals("No"))
{
try
{
getAppletContext().showDocument(new URL(getCodeBase(),"BookStor.htm"));
}
catch (MalformedURLException ex)
{
System.out.println("Exception in URL: " + ex);
ex.printStackTrace(System.out);
}
return true;
}
return false; // not handled
}
Figure 16 FillGrid Method
public void fillGrid()
{
try
{
// create a new URL to point to the ASP query
URL url = new URL("http://localhost/JavaData/javaasp/ShowTitles.asp");
// open the ASP, run the query.
URLConnection connection = url.openConnection();
// create a stream to read the results from the connection.
DataInputStream s = new DataInputStream(connection.getInputStream());
// increase performance by telling grid not to recalc. layouts
// while we're adding rows.
titlesGrid.setValue("layoutFrozen", false);
// read the column headings from the ASP result
res.add("simpleHeadings", s.readLine());
// set the column headings for the grid
titlesGrid.setValues(res);
int i = 0;
String inputLine;
// loop and read each HTML line
while ((inputLine = s.readLine()) != null)
{
// display the line into the console for debugging
// a great console for IE is at http://TrevorHarmon.com/visualj
System.out.println(inputLine);
// set each row with the fields
// the Microline grid access pipe characters as separators
// otherwise, we'd need to
titlesGrid.setStrings(i++, inputLine);
}
// close the DataInputStream
s.close();
titlesGrid.setValue("layoutFrozen", false);
Dimension prefSize = titlesGrid.preferredSize();
titlesGrid.reshape(0, 0, prefSize.width, prefSize.height);
// add the grid to the applet
add(titlesGrid);
}
catch (MalformedURLException e)
{
System.err.println("MalformedURLException " + e);
}
catch (IOException e)
{
System.err.println("IOException " + e);
}
}
Figure 17 ShowTitles.asp
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "pubs","sa",""
Set RS = Conn.Execute("SELECT title_id 'Title ID', title 'Title',
price 'Price' FROM Titles")
%>
<% For i = 0 to RS.Fields.Count - 1 %>
<% = RS(i).Name %>
<% If i < RS.Fields.Count - 1 Then
Response.Write("|")
End If %>
<% Next %>
<% Response.Write(vbCr) %>
<% Do While Not RS.EOF
For i = 0 to RS.Fields.Count - 1 %>
<% = RS(i) %>
<% If i < RS.Fields.Count - 1 Then
Response.Write("|")
End If
Next
Response.Write(vbCr)
RS.MoveNext
Loop
RS.Close
Conn.Close
%>