Hi All,
What follows is a 3 tier networking example using
JSP and JDBC. FYI!
Regards,
- DL
package lyon;
import java.sql.*;
import java.io.*;
import java.util.*;
public final class DataBaseQuery {
private static DataBaseQuery dbq
= new DataBaseQuery();
String dsn = "addressBook";
String url = "jdbc:odbc:"+dsn;
Connection connection = null;
Statement statement = null;
DatabaseMetaData dmd = null;
PrintStream out = System.out;
String sqlQuery =
"SELECT Addresses.FirstName, Addresses.LastName, "
+"Addresses.Address, "
+"Addresses.EmailAddress FROM Addresses;";
ResultSet rs = null;
ResultSetMetaData rsmd = null;
int colCount = 0;
private DataBaseQuery() {
try {
init();
}
catch(Exception e) {
}
}
public void setOutputStream(PrintStream _out){
out = _out;
}
public static DataBaseQuery getDataBaseQuery() {
return dbq;
}
public void init() throws Exception {
// load driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
dmd = connection.getMetaData();
rs= statement.executeQuery(sqlQuery);
freshenMetaData();
}
public void freshenMetaData() throws Exception {
rsmd = rs.getMetaData();
colCount = rsmd.getColumnCount();
}
public void printMetaData() throws Exception {
out.println(
"Connected to:"+dmd.getURL());
}
public Vector getQuery() throws Exception {
Vector v = new Vector();
while (rs.next()) {
freshenMetaData();
String s[] = new String[colCount];
for (int i=1; i <= colCount; i++) {
s[i-1]=rs.getString(i);
}
v.addElement(s);
}
return v;
}
public String [][] getStringArray(Vector v) {
String sa[][] = new String[v.size()][colCount];
for (int x=0; x < v.size(); x++) {
String s[] = (String []) (v.elementAt(x));
for (int y=0; y < s.length; y++)
sa[x][y] = s[y];
}
return sa;
}
public void print(Vector v) {
for (int i=0; i < v.size(); i++)
print((String[])v.elementAt(i));
}
public void print(String a[]) {
for (int i=0; i < a.length; i++)
out.print(a[i]+ " ");
out.println();
}
public void print(String a[][]) {
for (int x=0; x < a.length;x++) {
for (int y=0; y<a[0].length; y++)
out.print(a[x][y]+" ");
out.println();
}
}
public String[][] get2DArray() {
try {
return getStringArray(
getQuery()
);
}
catch(Exception e) {
}
return null;
}
public void loadDriver() {
try {
//init();
printMetaData();
print(
getStringArray(
getQuery()));
}
catch(Exception e) {
e.printStackTrace();
}
}
public void print() {
print(get2DArray());
}
public static void main(String[] args) {
lyon.DataBaseQuery dbq =
lyon.DataBaseQuery.getDataBaseQuery();
System.out.println("this is a test from main");
dbq.setOutputStream(System.out);
dbq.print();
}
<html>
<!--
Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
This software is the confidential and proprietary information of
Sun
Microsystems, Inc. ("Confidential Information"). You shall not
disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement you entered
into
with Sun.
SUN MAKES NO REPRESENTATIONS OR WARRANTIES
ABOUT THE SUITABILITY OF THE
SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE
LIABLE FOR ANY DAMAGES
SUFFERED BY LICENSEE AS A RESULT OF USING,
MODIFYING OR DISTRIBUTING
THIS SOFTWARE OR ITS DERIVATIVES.
-->
<body bgcolor="white">
<h1>Neal is my hero!</h1>
<%
for (int i=0; i < 100; i++)
out.print(i+" ");
out.println("<p>");
out.println(new java.util.Date());
String ss = request.getParameter("ss");
out.println("Search string="+ss);
String a = request.getParameter("a");
String b = request.getParameter("b");
int ai = Integer.parseInt(a);
int bi = Integer.parseInt(b);
int c = ai+bi;
out.println("a="+a);
out.println("b="+b);
out.println("a + b ="+c);
//lyon.DataBaseQuery.main(null);
//System.out.println("This is a test!!");
lyon.DataBaseQuery dbq =
lyon.DataBaseQuery.getDataBaseQuery();
String sa [][]= dbq.getStashedArray();
dbq.print();
out.println("sa length="+sa.length);
out.println("sa[0].length="+sa[0].length);
for (int x=0; x < sa.length; x++){
for (int y=0; y < sa[0].length; y++)
out.print("array="+sa[x][y]+" ");
out.println("<p>");
}
%>
<jsp:useBean id='clock' scope='page'
class='dates.JspCalendar' type="dates.JspCalendar" />
<font size=4>
<ul>
<li> Day of month: is <jsp:getProperty name="clock"
property="dayOfMonth"/>
<li> Year: is <jsp:getProperty name="clock" property="year"/>
<li> Month: is <jsp:getProperty name="clock" property="month"/>
<li> Time: is <jsp:getProperty name="clock" property="time"/>
<li> Date: is <jsp:getProperty name="clock" property="date"/>
<li> Day: is <jsp:getProperty name="clock" property="day"/>
<li> Day Of Year: is <jsp:getProperty name="clock"
property="dayOfYear"/>
<li> Week Of Year: is <jsp:getProperty name="clock"
property="weekOfYear"/>
<li> era: is <jsp:getProperty name="clock" property="era"/>
<li> DST Offset: is <jsp:getProperty name="clock"
property="dSTOffset"/>
<li> Zone Offset: is <jsp:getProperty name="clock"
property="zoneOffset"/>
</ul>
</font>
</body>
</html>
}
|