Created
August 10, 2017 05:31
-
-
Save darbyluv2code/424eb411fdf9dd130c3fa94a1551c3f2 to your computer and use it in GitHub Desktop.
updates to jsp web student tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mytest.jdbc; | |
| public class Student { | |
| private int id; | |
| private String firstName; | |
| private String lastName; | |
| private String email; | |
| public Student(int id, String firstName, String lastName, String email) { | |
| this.id = id; | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.email = email; | |
| } | |
| public Student(String firstName, String lastName, String email) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.email = email; | |
| } | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| public String getFirstName() { | |
| return firstName; | |
| } | |
| public void setFirstName(String firstName) { | |
| this.firstName = firstName; | |
| } | |
| public String getLastName() { | |
| return lastName; | |
| } | |
| public void setLastName(String lastName) { | |
| this.lastName = lastName; | |
| } | |
| public String getEmail() { | |
| return email; | |
| } | |
| public void setEmail(String email) { | |
| this.email = email; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mytest.jdbc; | |
| import java.io.IOException; | |
| import java.util.List; | |
| import javax.annotation.Resource; | |
| import javax.servlet.RequestDispatcher; | |
| 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 javax.sql.DataSource; | |
| /** | |
| * Servlet implementation class studentControlServlet | |
| */ | |
| @WebServlet("/studentControlServlet") | |
| public class studentControlServlet extends HttpServlet { | |
| private static final long serialVersionUID = 1L; | |
| @Resource(name = "jdbc/web_student_tracker") | |
| private DataSource datasource; | |
| private StudentDbUtil studentDbUtil; | |
| /** | |
| * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) | |
| */ | |
| @Override | |
| public void init() throws ServletException { | |
| // TODO Auto-generated method stub | |
| super.init(); | |
| try { | |
| studentDbUtil = new StudentDbUtil(datasource); | |
| } | |
| catch (Exception exc) { | |
| throw new ServletException(exc); | |
| } | |
| } | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| try { | |
| listStudents(request, response); | |
| } catch (Exception e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| private void listStudents(HttpServletRequest request, HttpServletResponse response) | |
| throws Exception { | |
| // get students from db util | |
| List<Student> students = studentDbUtil.getStudent(); | |
| // add students to the request | |
| request.setAttribute("STUDENT_LIST", students); | |
| // send to JSP page (view) | |
| RequestDispatcher dispatcher = request.getRequestDispatcher("list-student.jsp"); | |
| dispatcher.forward(request, response); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mytest.jdbc; | |
| import java.sql.Connection; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| import java.sql.Statement; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import javax.sql.DataSource; | |
| public class StudentDbUtil { | |
| private DataSource datasource; | |
| public StudentDbUtil(DataSource thedatasource){ | |
| datasource = thedatasource; | |
| } | |
| public List<Student> getStudent() throws SQLException{ | |
| List<Student> students = new ArrayList<>(); | |
| Connection mycon = null; | |
| Statement mystm = null; | |
| ResultSet myrs = null; | |
| try { | |
| mycon = datasource.getConnection(); | |
| String sql = "select * from student"; | |
| mystm = mycon.createStatement(); | |
| myrs = mystm.executeQuery(sql); | |
| while(myrs.next()){ | |
| int id = myrs.getInt("id"); | |
| String firstname = myrs.getString("first_name"); | |
| String lastname = myrs.getString("last_name"); | |
| String email = myrs.getString("email"); | |
| students.add(new Student(id, firstname, lastname, email)); | |
| } | |
| } finally { | |
| close(mycon,mystm,myrs); | |
| } | |
| return students; | |
| } | |
| private void close(Connection mycon, Statement mystm, ResultSet myrs) { | |
| try { | |
| if (myrs != null) { | |
| myrs.close(); | |
| } | |
| if (mystm != null) { | |
| mystm.close(); | |
| } | |
| if (mycon != null) { | |
| mycon.close(); | |
| } | |
| } catch (Exception e) { | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mytest.jdbc; | |
| import java.io.IOException; | |
| import java.sql.SQLException; | |
| import java.util.List; | |
| import javax.annotation.Resource; | |
| import javax.servlet.RequestDispatcher; | |
| 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 javax.sql.DataSource; | |
| /** | |
| * Servlet implementation class TestingStudentServlet | |
| */ | |
| @WebServlet("/TestingStudentServlet") | |
| public class TestingStudentServlet extends HttpServlet { | |
| private static final long serialVersionUID = 1L; | |
| private StudentDbUtil studentDbUti ; | |
| @Resource(name = "jdbc/web_student_tracker") | |
| private DataSource datasource; | |
| /** | |
| * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) | |
| */ | |
| @Override | |
| public void init() throws ServletException { | |
| // TODO Auto-generated method stub | |
| super.init(); | |
| try { | |
| studentDbUti = new StudentDbUtil(datasource); | |
| } catch (Exception e) { | |
| throw new ServletException(e); | |
| } | |
| } | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| // TODO Auto-generated method stub | |
| try { | |
| List<Student> students = studentDbUti.getStudent(); | |
| request.setAttribute("STUDENT_LIST", students); | |
| RequestDispatcher dispatchter = request.getRequestDispatcher("/list-student.jsp"); | |
| dispatchter.forward(request, response); | |
| } catch (SQLException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mytest.jdbc; | |
| import java.io.IOException; | |
| import java.io.PrintWriter; | |
| import java.sql.Connection; | |
| import java.sql.ResultSet; | |
| import java.sql.Statement; | |
| import javax.annotation.Resource; | |
| 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 javax.sql.DataSource; | |
| /** | |
| * Servlet implementation class TestServlet | |
| */ | |
| @WebServlet("/TestServlet") | |
| public class TestServlet extends HttpServlet { | |
| private static final long serialVersionUID = 1L; | |
| @Resource(name="jdbc/web_student_tracker") | |
| private DataSource datasource; | |
| /** | |
| * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) | |
| */ | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| PrintWriter out = response.getWriter(); | |
| Connection con = null; | |
| Statement mystmt = null; | |
| ResultSet myRs = null; | |
| try { | |
| con = datasource.getConnection(); | |
| mystmt = con.createStatement(); | |
| String sql = "select * from student"; | |
| myRs = mystmt.executeQuery(sql); | |
| while(myRs.next()){ | |
| String email = myRs.getString("email"); | |
| out.println(email); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) | |
| */ | |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| // TODO Auto-generated method stub | |
| doGet(request, response); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment