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

Reply via email to