iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2
using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a
error message of HTTP STATUS 500
my jdbc program is as follows
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import COM.ibm.db2.jcc.*;
import COM.ibm.db2.jdbc.app.*;
import COM.ibm.db2.jdbc.net.*;
public class tgsample {
static
{
try
{
//Loads Type 2 Driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
//For type 4 Driver: Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (ClassNotFoundException e)
{
System.err.println("Could not load DB2 driver \n");
System.err.println(e.getMessage());
System.exit(1);
}
}
public static void main(String args[])
{
String url = "jdbc:db2://localhost:50000/MYLSTDB";
String uname ="db2admin", psswrd ="varma";
String query = "SELECT * FROM VARMA.STUDENT";
try
{
//Connect to a database
Connection conn = DriverManager.getConnection(url, uname, psswrd);
//Create statement for the connections
Statement stmt = conn.createStatement();
//Execute the query
ResultSet rs = stmt.executeQuery(query);
//Loop through the result and print it out
System.out.println(query);
System.out.println("\nNAME \tROLLNO \tGPA \tCLASS");
System.out.println("----------------------------");
while (rs.next())
{
System.out.print(rs.getString(1) + " \t" );
System.out.print(rs.getString(2)+" \t");
System.out.print(rs.getString(3)+" \t");
System.out.println(rs.getString(4)+" \t");
}
rs.close();
stmt.close();
conn.close();
}
catch (SQLException e)
{
System.out.println("SQL Exception: ");
System.err.println(e.getMessage());
}
}//main
} //class
/**
* This class prompts the user for a password and attempts to mask input with ""
*/
class PasswordField {
/**
*@param prompt The prompt to display to the user.
*@return The password as entered by the user.
*/
String getPassword(String prompt) throws IOException {
// password holder
String password = "";
int counter = 0;
MaskingThread maskingthread = new MaskingThread(prompt);
Thread thread = new Thread(maskingthread);
thread.start();
// block until enter is pressed
while (true) {
char c = (char)System.in.read();
// assume enter pressed, stop masking
System.out.flush();
maskingthread.stopMasking();
if (c == '\r') {
c = (char)System.in.read();
if (c == '\n') {
break;
} else {
continue;
}
} else if (c == '\n') {
break;
} else {
// store the password
password += c;
}
}
return password;
}
}
/**
* This class attempts to erase characters echoed to the console. It's made to
* work with PasswordField class (see above).
*/
class MaskingThread extends Thread {
private boolean stop = false;
private int index;
private String prompt;
/**
*@param prompt The prompt displayed to the user
*/
public MaskingThread(String prompt) {
this.prompt = prompt;
}
/**
* Begin masking until asked to stop.
*/
public void run() {
while(!stop) {
try {
// attempt masking at this rate
this.sleep(1);
}catch (InterruptedException iex) {
iex.printStackTrace();
}
if (!stop) {
System.out.print("\r" + prompt + " \r" + prompt );
}
System.out.flush();
}
}
/**
* Instruct the thread to stop masking.
*/
public void stopMasking() {
this.stop = true;
}
}
also iam able to connect to database when i run it normally.
the problem is when i run it using a servlet.
what all changes i have to make to tomcat?please explain clearly.
also explain clearly about how to set the classpath clearly?
the error list i get is
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.ClassCastException: tgsample
at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:847)
at org.apache.catalina.core.StandardWrapper.allocate( StandardWrapper.java:612)
at org.apache.catalina.servlets.InvokerServlet.serveR equest(InvokerServlet.java:367)
at org.apache.catalina.servlets.InvokerServlet.doGet( InvokerServlet.java:131)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:809)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:198)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:144)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invokeNext(StandardPipeline.j ava:595)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:954)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:138)
at org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invokeNext(StandardPipeline.j ava:595)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:954)
at org.apache.catalina.core.StandardContext.invoke(St andardContext.java:2459)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:132)
at org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invokeNext(StandardPipeline.j ava:595)
at org.apache.catalina.valves.ErrorDispatcherValve.in voke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invokeNext(StandardPipeline.j ava:593)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invokeNext(StandardPipeline.j ava:593)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:954)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:126)
at org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invokeNext(StandardPipeline.j ava:595)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:954)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(Co yoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
--------------------------------------------------------------------------------
Apache Tomcat/4.1.34