【问题标题】:Cant see the contents of the database mysql看不到数据库mysql的内容
【发布时间】:2015-09-16 13:36:39
【问题描述】:

我在 MySQL 中有一个表,其中包含 sp_sb_id、professional_id、subject_id 列。我需要获取数据库的内容。我写方法:那是我的课:

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>

当我写入数据库类时,我可以看到:

ID  Profession  Subject
                       Delete Edit
                       Delete Edit
Add new speciality subject

但在数据库中,我有:

1   3       5
2   4       5

我的代码哪里出错了?

【问题讨论】:

  • 据我了解,问题是您没有从页面中的数据库中检索您的值,对吧?

标签: java mysql jsp jdbc


【解决方案1】:

我认为问题在于您如何在循环中显示数据,您必须使用循环变量speciality_subject 在迭代步骤中引用列表specialitys 中的特定对象,例如:

 <c:forEach items="${specialitys}" var="speciality_subject">
        <tr>
          <td>
            <c:out value="${speciality_subject.getId()}"/>
          </td>
          <td>
            <c:out value="${speciality_subject.getProfessionSubject()}"/>
          </td>
          <td>
            <c:out value="${speciality_subject.getSubjectId()}"/>
          </td>
          <td>
            <a href="controller?command=deleteSpeciality&id=${speciality_subject.getId()}">Delete</a>
            <a href="controller?command=editSpeciality&id=${speciality_subject.getId()}">Edit</a>
          </td>
        </tr>
  </c:forEach>

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2015-02-17
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多