Thanks, Rich.
Upon your hint, I've found the problem: the "test_it" class
was not in the CLASSPATH; I was assuming that the server
would know, since the class "NumberGuessBean" is in the same
directory but the server could load this in !!!! [Would anyone have
an explanation for this inconsistency ? Thanks.]
Anyway the server console also complained that it couldn't
find "test_it" class, but I overlooked it.
---Nam
Friedman, Richard wrote:
> Hey Nam,
>
> 1. Have you checked the log of your server to check for an exception
> being thrown?
> 2. Is there anything in the constructor of test_it()?
> 3. Is your database driver located in the classpath of your jsp-server?
>
> If you could supply some more of your code it would probably help!
>
> -Rich.
>
> -----Original Message-----
> From: Nam Nguyen [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, October 12, 1999 12:16 PM
> To: [EMAIL PROTECTED]
> Subject: A question on secondary class used by a JSP page.
>
> Hello all,
>
> I've been trying to do a db connection thru JSP-bean(or servlet ?) but ran
> into a problem and would appreciate if you could help to identify
> the meaning of the error code.
>
> There are 2 files that used to work:
>
> numguess.jsp
> NumberGuessBean.java.sav
>
> Now I have a new code:
>
> test_it.java
>
> which connect to a database and do a very simple SQL:
>
> String query = "select count(*) from TbAddress where City like '%Calgary%'";
>
> I tested this code independently from another Java main program and it
> works.
>
> But when I modified:
>
> NumberGuessBean.java
>
> to use an instance of "test_it" and run the numguess.jsp, I got an error
> code:
>
> Error: 500
> Unknown exception:
>
> for which I don't know what causes it. Although it seems to occur in this
> very
> line:
>
> a_DSN = new test_it(); [in the reset() method, of
> NumberGuessBean.java]
>
> when I commented this line out, it works.
>
> Thanks in advance for any help on this.
>
> ---Nam
>
> numguess.jsp
> =========
>
> <!--
> Number Guess Game
> Written by Jason Hunter <[EMAIL PROTECTED]>, CTO, K&A Software
> Copyright 1999, K&A Software, distributed by Sun with permission
> -->
>
> <%@ page import = "num.NumberGuessBean" %>
>
> <jsp:useBean id="numguess" class="num.NumberGuessBean" scope="session"/>
> <jsp:setProperty name="numguess" property="*"/>
>
> <html>
> <head><title>Number Guess</title></head>
> <body bgcolor="white">
> <font size=4>
>
> <% if (numguess.getSuccess()) { %>
>
> Congratulations! You got it.
> And after just <%= numguess.getNumGuesses() %> tries.<p>
>
> <% numguess.reset(); %>
>
> Care to <a href="numguess.jsp">try again</a>?
>
> <% } else if (numguess.getNumGuesses() == 0) { %>
>
> Welcome to the Number Guess game.<p>
>
> I'm thinking of a number between 1 and 100.<p>
>
> <form method=get>
> What's your guess? <input type=text name=guess>
> <input type=submit value="Submit">
> </form>
>
> <% } else { %>
>
> Good guess, but nope. Try <b><%= numguess.getHint() %></b>.
>
> You have made <%= numguess.getNumGuesses() %> guesses.<p>
>
> I'm thinking of a number between 1 and 100.<p>
>
> <form method=get>
> What's your guess? <input type=text name=guess>
> <input type=submit value="Submit">
> </form>
>
> <% } %>
>
> </font>
> </body>
> </html>
>
> NumberGuessBean.java.sav
> ===================
>
> // Number Guess Game
> // Written by Jason Hunter <[EMAIL PROTECTED]>, CTO, K&A Software
> // Copyright 1999, K&A Software, distributed by Sun with permission
>
> package num;
>
> import java.util.*;
>
> public class NumberGuessBean {
>
> int answer;
> boolean success;
> String hint;
> int numGuesses;
>
> public NumberGuessBean() {
> reset();
> }
>
> public void setGuess(String guess) {
> numGuesses++;
>
> int g;
> try {
> g = Integer.parseInt(guess);
> }
> catch (NumberFormatException e) {
> g = -1;
> }
>
> if (g == answer) {
> success = true;
> }
> else if (g == -1) {
> hint = "a number next time";
> }
> else if (g < answer) {
> hint = "higher";
> }
> else if (g > answer) {
> hint = "lower";
> }
> }
>
> public boolean getSuccess() {
> return success;
> }
>
> public String getHint() {
> return "" + hint;
> }
>
> public int getNumGuesses() {
> return numGuesses;
> }
>
> public void reset() {
> answer = Math.abs(new Random().nextInt() % 100) + 1;
> success = false;
> numGuesses = 0;
> }
> }
>
> NumberGuessBean.java
> ================
>
> // Number Guess Game
> // Written by Jason Hunter <[EMAIL PROTECTED]>, CTO, K&A Software
> // Copyright 1999, K&A Software, distributed by Sun with permission
>
> package num;
>
> import java.util.*;
> import test_it;
>
> public class NumberGuessBean {
>
> int answer;
> boolean success;
> String hint;
> String sanswer;
> int numGuesses;
> test_it a_DSN;
>
> public NumberGuessBean() {
> reset();
> }
>
> public void setGuess(String guess) {
> numGuesses++;
>
> int g;
> try {
> g = Integer.parseInt(guess);
> }
> catch (NumberFormatException e) {
> g = -1;
> }
>
> if (g == answer) {
> success = true;
> }
> else if (g == -1) {
> hint = "a number next time";
> }
> else if (g < answer) {
> hint = "higher";
> }
> else if (g > answer) {
> hint = "lower";
> }
> }
>
> public boolean getSuccess() {
> return success;
> }
>
> public String getHint() {
> return "" + hint;
> }
>
> public int getNumGuesses() {
> return numGuesses;
> }
>
> public void reset() {
> a_DSN = new test_it();
>
> answer = Math.abs(new Random().nextInt() % 100) + 1; // Commneted out
> for
> now.
> //sanswer = a_DSN.just_a_test();
> //answer = Integer.parseInt(sanswer);
> success = false;
> numGuesses = 0;
> }
> }
>
> test_it.java:
> ========
>
> //--------------------------------------------------------------------------
> --
> //
> // Module: test_it.java
> //
> // Description: Test class for ODBC API interface. This java application
> // will connect to a JDBC driver, issue a select statement
> // and display all result columns and rows
> //
> // USING: JDBC to ODBC Bridge
> //
> //--------------------------------------------------------------------------
> --
>
> import java.net.URL;
> import java.sql.*;
>
> public class test_it {
>
> public String just_a_test () {
> String url = "jdbc:odbc:wildrose";
> String query = "select count(*) from TbAddress where City like
> '%Calgary%'";
> String ret = "-1";
>
> try {
>
> // Load the jdbc-odbc bridge driver
>
> Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
>
> //
> // This prints a lot of messages
> //
> // DriverManager.setLogStream(System.out);
>
> // Attempt to connect to a driver. Each one
> // of the registered drivers will be loaded until
> // one is found that can process this URL
>
> Connection con = DriverManager.getConnection (
> url, "", "");
>
> // If we were unable to connect, an exception
> // would have been thrown. So, if we get here,
> // we are successfully connected to the URL
>
> // Check for, and display and warnings generated
> // by the connect.
>
> checkForWarning (con.getWarnings ());
>
> // Get the DatabaseMetaData object and display
> // some information about the connection
>
> DatabaseMetaData dma = con.getMetaData ();
> //System.out.println("\nConnected to " + dma.getURL());
> dma.getURL();
> //System.out.println("Driver " +
> //dma.getDriverName());
> dma.getDriverName();
> //System.out.println("Version " +
> //dma.getDriverVersion());
> dma.getDriverVersion();
> //System.out.println("");
>
> // Create a Statement object so we can submit
> // SQL statements to the driver
>
> Statement stmt = con.createStatement ();
>
> // Submit a query, creating a ResultSet object
>
> ResultSet rs = stmt.executeQuery (query);
>
> // Display all columns and rows from the result set
>
> ret = dispResultSet (rs);
>
> // Close the result set
>
> rs.close();
>
> // Close the statement
>
> stmt.close();
>
> // Close the connection
>
> con.close();
> }
> catch (SQLException ex) {
>
> // A SQLException was generated. Catch it and
> // display the error information. Note that there
> // could be multiple error objects chained
> // together
> //System.out.println ("\n*** SQLException caught ***\n");
>
> while (ex != null) {
> //System.out.println ("SQLState: " +
> //ex.getSQLState ());
> //System.out.println ("Message: " + ex.getMessage ());
> //System.out.println ("Vendor: " +
> //ex.getErrorCode ());
> ex = ex.getNextException ();
> //System.out.println ("");
> }
> }
> catch (java.lang.Exception ex) {
>
> // Got some other type of exception. Dump it.
>
> //ex.printStackTrace ();
> }
> return(ret);
> }
>
> //-------------------------------------------------------------------
> // checkForWarning
> // Checks for and displays warnings. Returns true if a warning
> // existed
> //-------------------------------------------------------------------
>
> private static boolean checkForWarning (SQLWarning warn)
> throws SQLException {
> boolean rc = false;
>
> // If a SQLWarning object was given, display the
> // warning messages. Note that there could be
> // multiple warnings chained together
>
> if (warn != null) {
> //System.out.println ("\n *** Warning ***\n");
> rc = true;
> while (warn != null) {
> //System.out.println ("SQLState: " +
> //warn.getSQLState ());
> //System.out.println ("Message: " +
> //warn.getMessage ());
> //System.out.println ("Vendor: " +
> //warn.getErrorCode ());
> //System.out.println ("");
> warn = warn.getNextWarning ();
> }
> }
> return rc;
> }
>
> //-------------------------------------------------------------------
> // dispResultSet
> // Displays all columns and rows in the given result set
> //-------------------------------------------------------------------
>
> private static String dispResultSet (ResultSet rs)
> throws SQLException
> {
> int i;
> String ret = "-2";
>
> // Get the ResultSetMetaData. This will be used for
> // the column headings
>
> ResultSetMetaData rsmd = rs.getMetaData ();
>
> // Get the number of columns in the result set
>
> int numCols = rsmd.getColumnCount ();
>
> // Display column headings
> for (i=1; i<=numCols; i++) {
> //if (i > 1) System.out.print(",");
> //System.out.print(rsmd.getColumnLabel(i));
> }
> //System.out.println("");
>
> // Display data, fetching until end of the result set
>
> boolean more = rs.next ();
> while (more) {
>
> // Loop through each column, getting the
> // column data and displaying
>
> for (i = 1; i <= numCols; i++) {
> //if (i > 1) System.out.print(",");
> ret = rs.getString(i);
> //System.out.print(ret);
> //System.out.print(rs.getString(i));
> }
> //System.out.println("");
>
> // Fetch the next result set row
>
> more = rs.next ();
> }
> return(ret);
> }
> }
>
> ===========================================================================
> 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
>
> ===========================================================================
> 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
--
Nam D. Nguyen
QC Data Ltd.
PDS Dept.
(403) 716-3477
email: [EMAIL PROTECTED]
---
"A polar bear is a rectangular bear after a coordinate transform." - anon.
===========================================================================
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