package com.telusko.da0;
import com.telusko.model.*;
import java.sql.*;
import java.util.*;
public class StudentDao {
ArrayList<Student> ar = new ArrayList<>();
public ArrayList getStudent(int sid) throws ClassNotFoundException{
//Student s = new Student();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","123");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from student");
while(rs.next()) {
Student s = new Student();
s.setSid(sid);
s.setSname(rs.getString("sname"));
s.setTech(rs.getString("tech"));
ar.add(s);
}
}
catch(Exception e) {
e.getMessage();
}
return ar;
}
}
My Servlet Code is
package com.telusko.controller;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.*;
import com.telusko.model.*;
import com.telusko.da0.*;
import java.util.*;
/**
* Servlet implementation class GetStudent
*/
public class GetStudent extends HttpServlet {
//private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
int id = Integer.parseInt(request.getParameter("id"));
StudentDao sd = new StudentDao();
ArrayList<Student> s = new ArrayList<Student>();
try {
s = sd.getStudent(id);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//out.println("Hello " +s);
request.setAttribute("student", s);
RequestDispatcher rd = request.getRequestDispatcher("ShowStudent.jsp");
rd.forward(request, response);
}
}
I have data in my database(jdbcdemo) in student table, But I'm unbale retrieve that data using my above program. With same driver class and url I'm able to get data using just JSP page, I couldn't understand the issue, Can someone say what is going wrong with it. am I missing anything