无法看到数据库mysql的内容

时间:2015-09-16 13:36:39

标签: java mysql jsp jdbc

我在MySQL中有一个表,其中包含sp_sb_id,profession_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

哪里,我的代码中有错误?

1 个答案:

答案 0 :(得分:1)

我认为问题在于如何在循环中显示数据,必须使用循环变量array(2) { ["2015-06-09"]=> array(2) { ["DE"]=> array(2) { ["column"]=> string(2) "GK" ["downloads"]=> int(555) } ["FR"]=> array(2) { ["column"]=> string(2) "GN" ["downloads"]=> int(123) } } ["2015-06-08"]=> array(1) { ["DE"]=> array(2) { ["column"]=> string(2) "GL" ["downloads"]=> int(234) } } } 在迭代步骤中从列表speciality_subject中复制到特定对象,例如:< / p>

specialitys