Connecting from JSP to DB2 database- having weird problems
843854Sep 22 2003 — edited Sep 23 2003Hi, I'm new to JSP and JDBC concepts. Trying to create an application which will connect to DB2 database (using Tomcat 4.1)and authenticate my userlogin.
I have a weird problem.. My JDBC code executes from the commandprompt successfully. But when I put the same code in the JSP file, it gives me exception error: 
Description: The server encountered an internal error () that prevented it from fulfilling this request.
root cause: "javax.servlet.ServletException: COM.ibm.db2.jdbc.app.DB2Driver" 
My code is below.. can anyone pls tell me what's wrong? I've spent so much time trying to figure out what's wrong.. couldn't really find anything..I'm giving up after 3 full days..
I have marked the point in my code where I have problems as //** and added the comment there.. (Pls. forgive my long code..)
---------------------------------------------
<%@ page import="java.lang.*" %>
<%@ page import="java.sql.*" %>
<%!
//Define class 
class DbConnection {
public String alias;
public String server="DB2/NT 8.1.0";
public int portNumber = 0; // <=0 indicates type 2 connection
public String userId;
public String userPwd;
public Connection con = null;
public DbConnection() {  }
public Connection connect() throws Exception
{
String url = null;
if( portNumber <= 0 ) {
url = "jdbc:db2:DWCTBC";
System.out.println("Connect to 'DWCTBC' database using JDBC type 2 driver." );
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
System.out.println("Executed class.forname"); //**This statement is executed on my Commandprompt.. having problems after this pt.
}
else {
url = "jdbc:db2://" + server + ":" + portNumber + "/" + alias;
System.out.println(
" Connect to 'DWCTBC' database using JDBC type 4 driver." );
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
}
if( null == userId ) {
System.out.println("UserID is null");
con = DriverManager.getConnection( url ); //**System hangs at this point when trying to execute this from JBuilder.
}
else {
	System.out.println("UserID not null" + userId + " URL is " + url);
con = DriverManager.getConnection( url, userId, userPwd);
}
// enable transactions
con.setAutoCommit(false);
return con;
} // connect
} // Db
//creating Instance of class
DbConnection DBcon = new DbConnection();
%>
<html>
<head>
<title>
Login Authorization
</title>
</head>
<body bgcolor="#808080">
<%-- Connect to the database and check if userId and pwd is correct --%>
<%
DBcon.userId= request.getParameter("txt_username");
DBcon.userPwd= request.getParameter("txt_password");
DBcon.connect();
%>
<h2>
Authenticating User.. <br>
</h2>
<% if (DBcon.con==null) {
System.out.println("No connection");
%>
<%@ include file="Login.jsp"%>
<%
}
if (null!=DBcon.con) {
System.out.println("connected");
%>
<%@ include file="Main.jsp" %>
<%
}
%>
</body>
</html>
--------------------------------------------------
Thanks in advance,
NJU