使用JSP从sql下拉列表

时间:2015-09-18 13:58:59

标签: java mysql sql jsp jdbc

我有两个表:第一个表,有两列的专业(profession_id,profession_name)。第二个表,申请人有五列(applicant_id,profession_id,last_name,first_name,entrance_year)。表中的profession_id表和表中的profession_id是MySQL中的相关字段。我需要,而不是profession_id,在表申请人中获得可用职业的下拉列表。我的桌子:

 CREATE TABLE PROFESSION (
  PROFESSION_ID   INT         NOT NULL AUTO_INCREMENT,
  PROFESSION_NAME VARCHAR(50) NOT NULL,
  PRIMARY KEY (PROFESSION_ID)
);

CREATE TABLE APPLICANT (
  APPLICANT_ID  INT         NOT NULL AUTO_INCREMENT,
  PROFESSION_ID INT         NOT NULL,
  LAST_NAME     VARCHAR(30) NOT NULL,
  FIRST_NAME    VARCHAR(30) NOT NULL,
  ENTRANCE_YEAR INT         NOT NULL,
  PRIMARY KEY (APPLICANT_ID),
  FOREIGN KEY (PROFESSION_ID) REFERENCES PROFESSION (PROFESSION_ID)
);

我的申请人和专业方法:

public enum ApplicantDBProvider {

INSTANCE;

private Connection connection;

private ApplicantDBProvider() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_applicant", "root", "quake3arena");
    } catch (ClassNotFoundException | SQLException e) {
        System.err.println("Class not found: com.mysql.jdbc.Driver " + e);
        throw new RuntimeException("Class not found: com.mysql.jdbc.Driver");
    }
}

public Profession getProfession(long professionId) throws Exception {
    PreparedStatement preparedStatement = null;
    Profession profession = null;
    try {
        preparedStatement = connection.prepareStatement("SELECT * FROM profession WHERE profession_id=?");
        preparedStatement.setInt(1, (int) professionId);

        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            profession = new Profession();
            profession.setId(resultSet.getInt("profession_id"));
            profession.setProfessionName(resultSet.getString("profession_name"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
    }

    return profession;
}

public List<Profession> getProfessions() throws Exception {
    Statement statement = null;

    List<Profession> professions = new ArrayList<>();
    try {
        statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT * FROM profession");
        Profession profession = null;
        while (resultSet.next()) {
            profession = new Profession();
            profession.setId(resultSet.getInt("profession_id"));
            profession.setProfessionName(resultSet.getString("profession_name"));
            professions.add(profession);
        }

    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (statement != null) {
            statement.close();
        }
    }

    return professions;
}

public void saveProfession(Profession profession) throws Exception {
    PreparedStatement preparedStatement = null;

    try {
        if (profession.getId() == -1) {
            preparedStatement = connection.prepareStatement("INSERT INTO profession (profession_name) VALUES (?) ");

            preparedStatement.setString(1, profession.getProfessionName());
        } else {
            preparedStatement = connection.prepareStatement("UPDATE profession SET profession_name=? WHERE profession_id=?");

            preparedStatement.setString(1, profession.getProfessionName());
            preparedStatement.setInt(2, (int) profession.getId());
        }
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
    }

}

public void deleteProfession(long professionId) throws Exception {
    PreparedStatement preparedStatement = null;

    try {
        preparedStatement = connection.prepareStatement("DELETE FROM profession WHERE profession_id=?");

        preparedStatement.setInt(1, (int) professionId);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
    }
}

public Applicant getApplicant(long applicantId) throws Exception {
    PreparedStatement preparedStatement = null;
    Applicant applicant = null;
    try {
        preparedStatement = connection.prepareStatement("SELECT * FROM applicant WHERE applicant_id=?");
        preparedStatement.setInt(1, (int) applicantId);

        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            applicant = new Applicant();
            applicant.setId(resultSet.getInt("applicant_id"));
            applicant.setFirstName(resultSet.getString("first_name"));
            applicant.setLastName(resultSet.getString("last_name"));
            applicant.setProfessionId(resultSet.getInt("profession_id"));
            applicant.setEntranceYear(resultSet.getInt("entrance_year"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
    }

    return applicant;
}

public List<Applicant> getApplicants() throws Exception {
    Statement statement = null;
    List <Applicant> applicants = new ArrayList<>();

    try {
        statement = connection.createStatement();
        //ResultSet resultSet = statement.executeQuery("SELECT * FROM applicant");
        ResultSet resultSet = statement.executeQuery("select a.applicant_id, a.first_name, a.last_name, a.entrance_year, p.profession_name from applicant a join profession p on a.profession_id = p.profession_id");
        Applicant applicant = null;
        while (resultSet.next()) {
            applicant = new Applicant();
            applicant.setId(resultSet.getInt("applicant_id"));
            applicant.setFirstName(resultSet.getString("first_name"));
            applicant.setLastName(resultSet.getString("last_name"));
            //applicant.setProfessionId(resultSet.getInt("profession_id"));
            applicant.setProfessionName(resultSet.getString("profession_name"));
            applicant.setEntranceYear(resultSet.getInt("entrance_year"));
            applicants.add(applicant);

        }

    } catch (SQLException e) {
        throw new Exception(e);
    }

    return applicants;
}

public void saveApplicant(Applicant applicant) throws Exception {
    PreparedStatement preparedStatement = null;

    try {
        if (applicant.getId() == -1) {
            preparedStatement = connection.prepareStatement("INSERT INTO applicant (first_name, last_name, profession_id, entrance_year) VALUES (?,?,?,?)");

            preparedStatement.setString(1, applicant.getFirstName());
            preparedStatement.setString(2, applicant.getLastName());
            preparedStatement.setInt(3, (int)applicant.getProfessionId());
            preparedStatement.setInt(4, applicant.getEntranceYear());

        } else {
            preparedStatement = connection.prepareStatement("UPDATE applicant SET first_name=?, last_name=?, profession_id=?, entrance_year=?  WHERE applicant_id=?");

            preparedStatement.setString(1, applicant.getFirstName());
            preparedStatement.setString(2, applicant.getLastName());
            preparedStatement.setInt(3, (int) applicant.getProfessionId());
            preparedStatement.setInt(4, applicant.getEntranceYear());
            preparedStatement.setInt(5, (int) applicant.getId());
        }
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
    }
}

public void deleteApplicant(long applicantId) throws Exception {
    PreparedStatement preparedStatement = null;

    try {
        preparedStatement = connection.prepareStatement("DELETE FROM applicant WHERE applicant_id=?");

        preparedStatement.setInt(1, (int) applicantId);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
    }
}
}

我创建了一个实现专业列表的类:

public class AddApplicantCommand implements ICommand {

    ApplicantDBProvider applicantDBProvider = ApplicantDBProvider.INSTANCE;

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse resp) {

        List<Profession> professions = null;

        try {
            professions = applicantDBProvider.getProfessions();
        } catch (Exception e) {
            request.setAttribute("error", e);
            return "pages/error.jsp";
        }

        request.setAttribute("professions", professions);
        return "pages/edit_applicant.jsp";
    }
}

在jsp中,我创建了select:

<%@ 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>Add applicant</h1>

<form method="post" action="controller?command=saveApplicant">

  <table>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Profession ID</th>
      <th>Entrance Year</th>
    </tr>
  </table>

  <c:choose>
    <c:when test="${applicant ne null}">
      <input type="text" name="first_name" value="${applicant.getFirstName()}"/>
      <input type="text" name="last_name" value="${applicant.getLastName()}"/>
      <%--<input type="text" name="profession_id" value="${applicant.getProfessionId()}"/>--%>
      <select name="profession_name">
        <c:forEach items="${professions}" var="profession">
        <option value="${profession.getId}">
          <c:out value="${profession.getProfessionName}"></c:out>
          </option>
            </c:forEach>
            </select>
      <input type="text" name="entrance_year" value="${applicant.getEntranceYear()}"/>
      <input type="hidden" name="applicant_id" value="${applicant.getId()}"/>
    </c:when>
    <c:otherwise>
      <input type="text" name="first_name" value=""/>
      <input type="text" name="last_name" value=""/>
      <input type="text" name="profession_id" value=""/>
      <input type="text" name="entrance_year" value=""/>
    </c:otherwise>
  </c:choose>
  <input type=submit value=submit>
</form>
</body>
</html>

但是,当我开始使用TomCat时,我没有专业的下拉列表。什么,我做错了?请帮忙。对不起,我需要阅读很多代码。

0 个答案:

没有答案