0

So after reading what was suggested to me. here are all the files I'm using. There's one problem, the method Authorfindall() in the AuthorServlet file is giving me an error that it's undeclared, which's not true because it's in the AuthorService file.

Can I get some help please? Thank you.

Author class

package user.domain;

public class Author {
private String email;
private String name;
private String affiliation;

public String getEmail(){
    return email;
}

public void setEmail(String email){
    this.email = email;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAffiliation(){
    return affiliation;
}

public void setAffiliation(String affiliation){
    this.affiliation = affiliation;
}

@Override
public String toString() {
    return "Author [ email=" + email + ", name="
            + name + ", affiliation=" + affiliation +"]";
}

}

Author service

package user.service;

import java.util.List;

import user.dao.UserDao;
import user.domain.Author;

public class AuthorService {
private UserDao userDao = new UserDao();



public List<Object> Authorfindall() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    // TODO Auto-generated method stub
    return userDao.Authorfindall();
}

}

Author Servlet

package user.web.servlet;

import java.io.IOException;
import java.util.List;

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

import user.service.AuthorService;
import user.domain.Author;

/**
 * Servlet implementation class AuthorServlet
 */
@WebServlet("/AuthorServlet")
public class AuthorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */


/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    AuthorService authorservice = new AuthorService();
    try {           
        request.setAttribute("AuthorList", authorservice.Authorfindall());

    } catch (InstantiationException | IllegalAccessException
            | ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        List<Object> li = authorservice.Authorfindall();
        for(int i = 0; i < li.size();i++){
            System.out.println(li.get(i).toString());
        }

    } catch (InstantiationException | IllegalAccessException
            | ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    request.getRequestDispatcher("/Queryresult/author.jsp").forward(request, response);
}

}

UserDao

Public Class UserDao {
public List<Object> Authorfindall()throws InstantiationException, 
IllegalAccessException, ClassNotFoundException{
    List<Object> list = new ArrayList<>();
    try {
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        Connection connect = DriverManager
                  .getConnection("jdbc:mysql://127.0.0.1:3306/sampledb?"
                          + "user=root&password=Shafiko93!");


        String sql = "select * from author";
        PreparedStatement preparestatement = connect.prepareStatement(sql); 
        ResultSet resultSet = preparestatement.executeQuery();

        while(resultSet.next()){
            Author author = new Author();
            author.setEmail(resultSet.getString("email"));
            author.setName(resultSet.getString("name"));
            author.setAffiliation(resultSet.getString("affiliation"));
            list.add(author);
         }

    } catch(SQLException e) {
        throw new RuntimeException(e);
    }
    return list;
}

}

  • Can you share your query too? How are you creating `rs`? – Mureinik Jun 11 '18 at 21:36
  • You should not do that, get data in servlet and pass it to jsp – Georgy Gobozov Jun 11 '18 at 21:43
  • I don't know how to do that. Teachers throw projects on you and expect you to know. @GeorgyGobozov – Shafik Oudeh Jun 11 '18 at 21:45
  • Servlet is better for this kind of code, which connects to the database and fetches data. – Bhesh Gurung Jun 11 '18 at 21:52
  • This is a good read: https://stackoverflow.com/a/3180202/738746 – Bhesh Gurung Jun 11 '18 at 21:56
  • Your code looks all good. Only missing piece is how you are using AuthorList in jsp. May be that is causing the error ? NOTE: Your code has obvious resource leak, please close DB connection & prepared statement in finally block of DAO layer. I always recommend using [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) (for Java 7+ users). – Amith Kumar Jun 15 '18 at 00:04

0 Answers0