0

Pushing data into my database through JSP giving me ClassNotFoundException, while parsing data from main method is working just fine.

I have my mysql-connector jar and everything in place,

In the following class there are few methods for opening/closing database connections.

package com.socialhub.dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
    public static int client = 0;

    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/socialhub","root","root");
            client++;
        } catch (Exception e) {
            System.out.println("Connection issue " + e.getMessage());
            e.printStackTrace();
        }
        return con;
    }
    public void closeConnection(Connection conn) {
        try {
            conn.close();
            client--;
        } catch (Exception e) {
            System.out.println("Connection Close Issue" + e.getMessage());
        }
    }
    public int getClient() {
        return client;
    }
}

<%@ page import="com.socialhub.action.Service"%>  
<% 
String username=request.getParameter("uname");
String fullname=request.getParameter("fname");
String password=request.getParameter("password");
String country=request.getParameter("country");
String gender=request.getParameter("gender");

Service service=new Service();
String message="";
int valid=0;
try {
 country.length();
}
catch(Exception e) {
 country="na";
}
if(country.equalsIgnoreCase("na")||country.equalsIgnoreCase("null")||country==null) {
 message="Invalid Username and Password";
 valid=service.checkcredential(username,password);
 if(valid==0) {
  response.sendRedirect("profile.jsp");
 } else {
     response.sendRedirect("login.jsp?message="+message);
 }
}
else {
 valid=service.save(fullname, country, gender, username, password);
 if(valid==1) {
  message="Registration Successfully completed";
  response.sendRedirect("login.jsp?message="+message); 
 }
 else {
  message="Some Server Error";
  response.sendRedirect("login.jsp?message="+message); 
 }
}
%>
snegi
  • 171
  • 1
  • 1
  • 13
  • On a related note: http://stackoverflow.com/q/2188706/1189885 – chrylis -cautiouslyoptimistic- Nov 10 '16 at 20:11
  • This works really fast :D, thanks for considering. Hey, that post was quite informative. But I'm debugging this code since last night still doing, why it is doing fine from main and having issue with JSP. – snegi Nov 10 '16 at 20:24
  • Guess despite what you claim you don't have the JDBC driver in place. I'd double check that. – Robert Nov 10 '16 at 22:01
  • @Robert I have my jdbc driver in place, under project root > referenced libraries > mysql-connector.jar. And BTW, as I mentioned from main() if i call service.checkCredential() method It is working fine there. I can share you my service class implementation if required. – snegi Nov 10 '16 at 22:11
  • Do you deploy the JDBC driver with your app? It does not do anything if the web server cannot find it because it's in your workspace, but not deployed. That would also explain why it works from main, when you happen to have the classpath set correctly. – Robert Nov 10 '16 at 22:46
  • Voila, It worked! @Robert thanks man. I was so busy looking for bug. thank you again..... – snegi Nov 10 '16 at 22:57

0 Answers0