Помогите с выводом данных из базы данных используя JSP

 
 
 
Сообщения:1
Привет всем. Народ, помогите сделать задание, не могу понять как сделать. Кто-то может написать рабочий нормальный код ? Код должен выводить список студентов. Скидываю то что у меня есть.
Вопрос, если запрос нужен из двух или более таблиц - что-то меняется или нет ? Вроде код выглядеть должен по другому, так как используются PK и FK.
Спасибо за помощь!


File name:
viewStudents.jsp

<%@page import="model.dao.listOfStudentsDAO"%>
<%@page import="model.dao.listOfStudentsDAOImpl"%>
<%@page import="model.dto.Student"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"; prefix="c"%>
<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>View Students</title>
</head>
<body>


<h1>Users List</h1>

<table border="1">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Address</th>
<th>City</th>
<th>Country</th>
<th>Phone number</th>
<th>Course ID</th>


</tr>
<tr>
<td>${getFirst_name()}</td>
<td>${getLast_name()}</td>
<td>${getAddress}</td>
<td>${getCity}</td>
<td>${getCountry}</td>
<td>${getPHONE_NUMBER}</td>
<td>${getCOURSE_ID}</td>
<td>${getStudent_id}</td>

</tr>
</table>
<br/><a href="adduserform.jsp">Add New User</a>

</body>
</html>

----------------------------------------------------
model.dto.Student


package model.dto;


public class Student {

private String first_name;
private String last_name;
private String address;
private String city;
private String country;
private Long PHONE_NUMBER;
private Integer COURSE_ID;
private String Username;
private String Password;
private String Role;
private String usernameRole;
private Integer student_id;

public Integer getStudent_id() {
return student_id;
}

public void setStudent_id(Integer student_id) {
this.student_id = student_id;
}

public String getUsernameRole() {
return usernameRole;
}

public void setUsernameRole(String usernameRole) {
this.usernameRole = usernameRole;
}

public String getRole() {
return Role;
}

public void setRole(String Role) {
this.Role = Role;
}

public String getUsername() {
return Username;
}

public void setUsername(String Username) {
this.Username = Username;
}

public String getPassword() {
return Password;
}

public void setPassword(String Password) {
this.Password = Password;
}


public String getFirst_name() {
return first_name;
}

public void setFirst_name(String first_name) {
this.first_name = first_name;
}

public String getLast_name() {
return last_name;
}

public void setLast_name(String last_name) {
this.last_name = last_name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Long getPHONE_NUMBER() {
return PHONE_NUMBER;
}

public void setPHONE_NUMBER(Long PHONE_NUMBER) {
this.PHONE_NUMBER = PHONE_NUMBER;
}


public Integer getCOURSE_ID() {
return COURSE_ID;
}

public void setCOURSE_ID(Integer COURSE_ID) {
this.COURSE_ID = COURSE_ID;
}

}

-------------------------------------------------------
model.dao.listOfStudentsDAO.java


package model.dao;

import model.dto.Student;
import java.sql.SQLException;
import java.util.List;


public interface listOfStudentsDAO {
public void addStudents(Student stud) throws SQLException;
public List<Student> getAllStudents() throws SQLException;
}
---------------------------------------------------------
listOFStudentsServlet.java


package listOfTeachers;


import model.dao.listOfTeachersDAO;
import model.dao.listOfTeachersDAOImpl;
import model.dto.Teacher;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/listOfTeachers",
initParams = {
@WebInitParam(name = "url", value = "jdbc:oracle:thin:@localhost:1521:xe"),
@WebInitParam(name = "username", value = "HR"),
@WebInitParam(name = "password", value = "HR")})

public class listOfTeachersServlet extends HttpServlet{

private listOfTeachersDAO dao;

public void init() {
String url = getServletConfig().getInitParameter("url");
String user = getServletConfig().getInitParameter("username");
String password = getServletConfig().getInitParameter("password");

//Create DAO
dao = new listOfTeachersDAOImpl(url, user, password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

try{
List<Teacher> teachers = dao.getAllTeachers();
PrintWriter out = response.getWriter();
for (Teacher teach : teachers) {
out.println("First name: " + teach.getFirst_name() + " Last name: " + teach.getLast_name()
+ " Address: " + teach.getAddress() + " " + teach.getCity()
+ " City: " + teach.getCountry() + " Country: " + teach.getPHONE_NUMBER()
+ " Phone number: "
+ " Teacher Id: " + teach.getTeacher_id()
+ " ");
out.println("");

}

} catch (SQLException ex) {
Logger.getLogger(listOfTeachersServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request, response);
}

}
Изменен:10 янв 2019 14:18
 
Модераторы:Нет
Сейчас эту тему просматривают:Нет