0

How can I solve this error in my Java project?

    package Com.Admin.Controller;

    import Com.Admin.Dao.Admin_Questions_Dao;
    import Com.Admin.Modal.Questions_Beam;

    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.ArrayList;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    @WebServlet("/addQuestions")
    public class Questions extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String categoryname = request.getParameter("categoryname");
            String str = request.getParameter("category_topic");
            String categorytopic = str.toLowerCase();
            String[] category = request.getParameterValues("category");
            String[] categorytype = request.getParameterValues("categorytype");
            String[] option = request.getParameterValues("option");
            String[] question = request.getParameterValues("question");
            String[] formula_ans = request.getParameterValues("formula_ans");
            String[] shortcut_ans = request.getParameterValues("shortcut_ans");
            String[] option1 = request.getParameterValues("option1");
            String[] option2 = request.getParameterValues("option2");
            String[] option3 = request.getParameterValues("option3");
            String[] option4 = request.getParameterValues("option4");

            ArrayList<Questions_Beam> list = new ArrayList<>(); 

            for(int index=0; index < question.length; index++) {

               list.add(new Questions_Beam(category[index], categorytype[index], option[index], question[index],
                       formula_ans[index], shortcut_ans[index], option1[index], option2[index], option3[index], 
                       option4[index]));    

            }
            //Admin_Questions_Dao obj = new Admin_Questions_Dao();

            try {
                Admin_Questions_Dao.addQuestions(list, categorytopic);
            } 
            catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }


}


MODAL CLASS:
package Com.Admin.Modal;

import java.util.Arrays;

public class Questions_Beam {

    public String category ;
    public String categorytype;
    public String option ;
    public String question ;
    public String formula_ans ;
    public String shortcut_ans;
    public String option1;
    public String option2;
    public String option3;
    public String option4;

    public Questions_Beam(String category, String categorytype, String option, String question, String formula_ans,
            String shortcut_ans, String option1, String option2, String option3, String option4) 
    {
        super();
        this.category = category;
        this.categorytype = categorytype;
        this.option = option;
        this.question = question;
        this.formula_ans = formula_ans;
        this.shortcut_ans = shortcut_ans;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.option4 = option4;
    }

    @Override
    public String toString() {
        return "Questions_Beam [category=" + category + ", categorytype=" + categorytype + ", option=" + option
                + ", question=" + question + ", formula_ans=" + formula_ans + ", shortcut_ans=" + shortcut_ans
                + ", option1=" + option1 + ", option2=" + option2 + ", option3=" + option3 + ", option4=" + option4
                + "]";
    }
}


DAO LAYER:
package Com.Admin.Dao;

import Com.Admin.Controller.Questions;

import java.sql.*;
import java.util.ArrayList;

import com.mysql.jdbc.Connection;

import Com.Admin.Modal.Questions_Beam;
import Common.DBConnection;

public class Admin_Questions_Dao {

    public static void addQuestions(ArrayList<Questions_Beam> list , String categorytopic) throws ClassNotFoundException, SQLException {
        Connection con = null;
        con = (Connection) DBConnection.getConnection();
        PreparedStatement stmt = null;

        try {
            for (int index=0; index < list.size(); index++) {

                 Questions_Beam data = list.get(index); 

                 stmt = con.prepareStatement("insert into problems on trains values(?,?,?,?,?,?,?,?,?,?)");
                 stmt.setString(1, data.category);
                 stmt.setString(2, data.categorytype);
                 stmt.setString(3, data.option);
                 stmt.setString(4, data.question);
                 stmt.setString(5, data.formula_ans);
                 stmt.setString(6, data.shortcut_ans);
                 stmt.setString(7, data.option1);
                 stmt.setString(8, data.option2);
                 stmt.setString(9, data.option3);
                 stmt.setString(10, data.option4);
                 int result = stmt.executeUpdate();

            }
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

RESULT ERROR: 500 - Internal Server Error. ype Exception Report

Message Servlet execution threw an exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) Root Cause

java.lang.NoClassDefFoundError: com/mysql/jdbc/Connection Com.Admin.Dao.Admin_Questions_Dao.addQuestions(Admin_Questions_Dao.java:45) Com.Admin.Controller.Questions.doPost(Questions.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) Note The full stack trace of the root cause is available in the server logs.

AkhilSai
  • 1
  • 3

1 Answers1

0

As I can see you are facing no class def for my SQL driver class. This means at classpath mysql class is not available. If you application is maven based add the below MySQL driver jar dependency https://mvnrepository.com/artifact/mysql/mysql-connector-java And if it's not maven based download the MySQL driver jar and add it to your lib folder. And another suggestion please use proper standard way to name the package and class. Package name should not contain capital letter and class and variable should follow camel casing format, where variable name should start with lower case and class name should start with upper case letter.

Ashutosh
  • 1
  • 2
  • Thanks for your suggestion.But i included the mysql jar properly in my project still i am not getting the correct output. – AkhilSai Feb 26 '19 at 09:11
  • Why are you specifically linking to the 5.1.6 version of MySQL Connector/J? That version is extremely old (and will not work correctly with MySQL 8). – Mark Rotteveel Feb 26 '19 at 14:54
  • Apologies Mark ... My intention was to make him aware about the jar not specific to 5.1.6 version ..... – Ashutosh Feb 27 '19 at 11:06
  • @Akhil as I can see in your code you are using com.mysql.jdbc.Connection ..... Please replace it with java.sql.Connection – Ashutosh Feb 27 '19 at 11:10