0
我在MySQL中有一个表sp_sb_id,profession_id,subject_id。我需要获取数据库的内容。我写的方法:那是我的课:不能看到数据库的内容mysql
public class Speciality extends Entity {
private long professionSubject;
private long subjectId;
public Speciality() {
this.id = -1;
}
public Speciality(long professionSubject, long subjectId) {
this.professionSubject = professionSubject;
this.subjectId = subjectId;
}
public long getProfessionSubject() {
return professionSubject;
}
public void setProfessionSubject(long professionSubject) {
this.professionSubject = professionSubject;
}
public long getSubjectId() {
return subjectId;
}
public void setSubjectId(long subjectId) {
this.subjectId = subjectId;
}
@Override
public String toString() {
return "SpecialitySubject{" +
"professionSubject=" + professionSubject +
", subjectId=" + subjectId +
'}';
}
}
这是我的方法,即使用数据库:这回我的网页
public Speciality getSpeciality(long specialityId) throws Exception {
PreparedStatement preparedStatement = null;
Speciality speciality = null;
try {
preparedStatement = connection.prepareStatement("SELECT * FROM speciality_subject WHERE sp_sb_id=?");
preparedStatement.setInt(1, (int) specialityId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
speciality = new Speciality();
speciality.setId(resultSet.getInt("sp_sb_id"));
speciality.setProfessionSubject(resultSet.getInt("profession_id"));
speciality.setSubjectId(resultSet.getInt("subject_id"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
}
return speciality;
}
public List<Speciality> getSpecialitys() throws Exception {
Statement statement = null;
List <Speciality> specialitys = new ArrayList<>();
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM speciality_subject");
Speciality speciality = null;
while (resultSet.next()) {
speciality = new Speciality();
speciality.setId(resultSet.getInt("sp_sb_id"));
speciality.setProfessionSubject(resultSet.getInt("profession_id"));
speciality.setSubjectId(resultSet.getInt("subject_id"));
specialitys.add(speciality);
}
} catch (SQLException e) {
throw new Exception(e);
}
return specialitys;
}
public void saveSpeciality (Speciality speciality) throws Exception {
PreparedStatement preparedStatement = null;
try {
if (speciality.getId() == -1) {
preparedStatement = connection.prepareStatement("INSERT INTO speciality_subject (profession_id, subject_id) VALUES (?,?)");
preparedStatement.setInt(1, (int) speciality.getProfessionSubject());
preparedStatement.setInt(2, (int) speciality.getSubjectId());
} else {
preparedStatement = connection.prepareStatement("UPDATE speciality_subject SET profession_id=?, subject_id=? WHERE sp_sb_id=?");
preparedStatement.setInt(1, (int) speciality.getProfessionSubject());
preparedStatement.setInt(2, (int) speciality.getSubjectId());
preparedStatement.setInt(3, (int) speciality.getId());
}
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new Exception(e);
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
}
}
public void deleteSpeciality(long specialityId) throws Exception {
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement("DELETE FROM speciality_subject WHERE sp_sb_id=?");
preparedStatement.setInt(1, (int) specialityId);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new Exception(e);
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
}
}
类:
public class SpecialityCommand implements ICommand {
ApplicantDBProvider applicantDBProvider = ApplicantDBProvider.INSTANCE;
@Override
public String execute(HttpServletRequest request, HttpServletResponse resp) {
List<Speciality> specialitys = null;
try {
specialitys = applicantDBProvider.getSpecialitys();
} catch (Exception e) {
request.setAttribute("error", e);
return "pages/error.jsp";
}
request.setAttribute("specialitys", specialitys);
return "pages/specialitys.jsp";
}
}
最后,我jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title></title>
</head>
<body>
<h1>Speciality subjects</h1>
<c:choose>
<c:when test="${specialitys.size() == 0}">
<p><c:out value="No speciality subjects yet"></c:out></p>
</c:when>
<c:otherwise>
<table>
<tr>
<th>ID</th>
<th>Profession</th>
<th>Subject</th>
</tr>
<c:forEach items="${specialitys}" var="speciality_subject">
<tr>
<td>
<c:out value="${speciality.getId()}"/>
</td>
<td>
<c:out value="${speciality.getProfessionSubject()}"/>
</td>
<td>
<c:out value="${speciality.getSubjectId()}"/>
</td>
<td>
<a href="controller?command=deleteSpeciality&id=${speciality.getId()}">Delete</a>
<a href="controller?command=editSpeciality&id=${speciality.getId()}">Edit</a>
</td>
</tr>
</c:forEach>
</table>
</c:otherwise>
</c:choose>
<a href="controller?command=addSpeciality">Add new speciality subject</a>
</body>
</html>
当我写t Ø数据库类,我可以看到:
ID Profession Subject
Delete Edit
Delete Edit
Add new speciality subject
但在数据库中,我有:
1 3 5
2 4 5
哪里,我有错误在我的代码?
正如我理解,问题是你不从页面中的数据库中检索你的值,对吧? – drgPP
是的!你是对的 – Valeriu