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;
}
}