Here's how I did it:
Any comments?
Josh
Here's how I did it:
Any comments?
Josh
3 files, dbtemplate.jsp, dbAccess.java, FinalResultSet.java
source:dbtemplate.jsp
<%@ page import="db.*" %>
<!-- pass a variable to included page -->
<% request.setAttribute("color", "white"); %>
<% request.setAttribute("title", "Database Access Template"); %>
<%@ include file="header.jsp" %>
<BODY>
<jsp:useBean id="dbAccessBean" class="db.dbAccess" scope="page"/>
<jsp:setProperty name="dbAccessBean" property="query" value="select email
from emailtable"/>
Query is:<b><%=dbAccessBean.getQuery() %></b><br>
Getting a String with getProperty:<b></b><Br>
Getting a string by calling a bean method:<% FinalResultSet rs =
dbAccessBean.getResult();%>
<% for(int j=0;rs.next();j++) { %>
<TR>
<TD><%=rs.getString("email") %></TD>
</TR>
<%
}
%>
<jsp:include page="footer.html"/>
</BODY>
</HTML>
source:dbAccess.java
package db;
import java.io.*;
import java.sql.*;
import db.FinalResultSet;
public class dbAccess
{
public String query = "";
public String result = "";
public dbAccess()
{
}
public void setQuery(String sqlString)
{
query = sqlString;
}
public String getQuery()
{
return query;
}
public FinalResultSet getResult()
{
FinalResultSet rset=null;
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection
("jdbc:oracle:thin:TEST/LETMEIN@TEST:1521:PCGDEV");
Statement stmt = conn.createStatement ();
rset = new FinalResultSet(stmt.executeQuery (query));
}
catch (Exception E)
{
}
return rset;
}
}
source:FinalResultSet.java
package db;
import java.sql.*;
import java.io.*;
import java.math.*;
import java.sql.ResultSet;
public final class FinalResultSet
{
private ResultSet rs;
public FinalizingResultSet(ResultSet resultfilling)
{
rs = resultfilling;
}
protected final void finalize() throws Throwable // The reason we exist...
{
rs.close();
rs = null;
}
// The rest of the methods just call their corresponding
// ResultSet method...
public final boolean next()
{
try
{
return(rs.next());
}
catch (Exception E)
{displayException(E);} return(false);
}
public final void close()
{
try
{
rs.close();
}
catch (Exception E)
{
displayException(E);
}
}
public final boolean wasNull()
{
try
{
return(rs.wasNull());
}
catch (Exception E)
{displayException(E);} return(false);
}
public final String getString(int columnIndex)
{
try
{
return(rs.getString(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final boolean getBoolean(int columnIndex)
{
try
{
return(rs.getBoolean(columnIndex));
}
catch (Exception E)
{displayException(E);} return(false);
}
public final byte getByte(int columnIndex)
{
try
{
return(rs.getByte(columnIndex));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final short getShort(int columnIndex)
{
try
{
return(rs.getShort(columnIndex));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final int getInt(int columnIndex)
{
try
{
return(rs.getInt(columnIndex));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final long getLong(int columnIndex)
{
try
{
return(rs.getLong(columnIndex));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final float getFloat(int columnIndex)
{
try
{
return(rs.getFloat(columnIndex));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final double getDouble(int columnIndex)
{
try
{
return(rs.getDouble(columnIndex));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final BigDecimal getBigDecimal(int columnIndex, int scale)
{
try
{
return(rs.getBigDecimal(columnIndex, scale));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final byte[] getBytes(int columnIndex)
{
try
{
return(rs.getBytes(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final Date getDate(int columnIndex)
{
try
{
return(rs.getDate(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final Time getTime(int columnIndex)
{
try
{
return(rs.getTime(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final java.sql.Timestamp getTimestamp(int columnIndex)
{
try
{
return(rs.getTimestamp(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final InputStream getAsciiStream(int columnIndex)
{
try
{
return(rs.getAsciiStream(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final InputStream getUnicodeStream(int columnIndex)
{
try
{
return(rs.getUnicodeStream(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final InputStream getBinaryStream(int columnIndex)
{
try
{
return(rs.getBinaryStream(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final String getString(String columnName)
{
try
{
return(rs.getString(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final boolean getBoolean(String columnName)
{
try
{
return(rs.getBoolean(columnName));
}
catch (Exception E)
{displayException(E);} return(false);
}
public final byte getByte(String columnName)
{
try
{
return(rs.getByte(columnName));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final short getShort(String columnName)
{
try
{
return(rs.getShort(columnName));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final int getInt(String columnName)
{
try
{
return(rs.getInt(columnName));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final long getLong(String columnName)
{
try
{
return(rs.getLong(columnName));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final float getFloat(String columnName)
{
try
{
return(rs.getFloat(columnName));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final double getDouble(String columnName)
{
try
{
return(rs.getDouble(columnName));
}
catch (Exception E)
{displayException(E);} return(0);
}
public final BigDecimal getBigDecimal(String columnName, int scale)
{
try
{
return(rs.getBigDecimal(columnName, scale));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final byte[] getBytes(String columnName)
{
try
{
return(rs.getBytes(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final Date getDate(String columnName)
{
try
{
return(rs.getDate(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final Time getTime(String columnName)
{
try
{
return(rs.getTime(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final java.sql.Timestamp getTimestamp(String columnName)
{
try
{
return(rs.getTimestamp(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final InputStream getAsciiStream(String columnName)
{
try
{
return(rs.getAsciiStream(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final InputStream getUnicodeStream(String columnName)
{
try
{
return(rs.getUnicodeStream(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final InputStream getBinaryStream(String columnName)
{
try
{
return(rs.getBinaryStream(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final SQLWarning getWarnings()
{
try
{
return(rs.getWarnings());
}
catch (Exception E)
{displayException(E);} return(null);
}
public final void clearWarnings()
{
try
{
rs.clearWarnings();
}
catch (Exception E)
{
displayException(E);
}
}
public final String getCursorName()
{
try
{
return(rs.getCursorName());
}
catch (Exception E)
{displayException(E);} return(null);
}
public final ResultSetMetaData getMetaData()
{
try
{
return(rs.getMetaData());
}
catch (Exception E)
{displayException(E);} return(null);
}
public final Object getObject(int columnIndex)
{
try
{
return(rs.getObject(columnIndex));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final Object getObject(String columnName)
{
try
{
return(rs.getObject(columnName));
}
catch (Exception E)
{displayException(E);} return(null);
}
public final int findColumn(String columnName)
{
try
{
return(rs.findColumn(columnName));
}
catch (Exception E)
{displayException(E);} return(0);
}
public void displayException (Exception E)
{
System.out.println(E);
}
}
-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Carlos Orrego
Sent: Wednesday, November 03, 1999 7:13 AM
To: [EMAIL PROTECTED]
Subject: ResultSet pass as an attributte?
Can i pass a ResultSet from a servlet as an object in the request attribute
to a jsp page?
are ResultSet actually normal objects?
or do i have to copy the whole content of my ResultSet to a Vector and then
send it in the atribute to the jsp?
thanks
Carlos Orrego-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Carlos Orrego
Sent: Wednesday, November 03, 1999 7:13 AM
To: [EMAIL PROTECTED]
Subject: ResultSet pass as an attributte?
Can i pass a ResultSet from a servlet as an object in the request attribute
to a jsp page?
are ResultSet actually normal objects?
or do i have to copy the whole content of my ResultSet to a Vector and then
send it in the atribute to the jsp?
thanks
Carlos Orrego
-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Carlos Orrego
Sent: Wednesday, November 03, 1999 7:13 AM
To: [EMAIL PROTECTED]
Subject: ResultSet pass as an attributte?
Can i pass a ResultSet from a servlet as an object in the request attribute
to a jsp page?
are ResultSet actually normal objects?
or do i have to copy the whole content of my ResultSet to a Vector and then
send it in the atribute to the jsp?
thanks
Carlos Orrego
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html