I have two tables.
First table,
Profession
with two columns (profession_id, profession_name)
.
Second table,
Applicant
with five columns (applicant_id, profession_id, last_name, first_name, entrance_year)
.
profession_id
in table Applicant
and profession_id
in table Profession
is related fields in MySQL.
I need, instead of profession_id, get a drop-down list of available profession. As, I here implemented using INNER JOIN
This is my class, that working with DB:
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", "toor");
} 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 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.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 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();
}
}
}
My 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>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()}"/>
<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>
I implement that query in database (You can see it in my code):
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
But, when i run my server, and open my jsp, i have error: java.lang.Exception: java.sql.SQLException: Column 'profession_id' not found.
What a mistake I can not understand. What could it be ? Tell me please. Thanks.
EDIT:
In this QUERY,
select a.applicant_id, a.first_name, a.last_name, a.entrance_year, p.profession_name, p.profession_id from applicant a join profession p on a.profession_id = p.profession_id
You only have,
a.applicant_id, a.first_name, a.last_name, a.entrance_year, p.profession_name
There is no p.profession_id
So when you do a,
applicant.setProfessionId(resultSet.getInt("profession_id"));
You get an exception, because you do not have that column in your result list.
ResultSet resultSet = statement.executeQuery("select a.applicant_id, a.first_name, a.last_name, a.entrance_year, p.profession_name, p.profession_id from applicant a join profession p on a.profession_id = p.profession_id");
Answer to your,
i have a class. Applicant.java. in that class, i have professionId with long data type. now my queston. when, i change applicant.setProfessionId(resultSet.getInt("profession_id")) on applicant.setProfessionId(resultSet.getString("profession_name")), i need to change professionId to String data type
You can add a class variable to your Application.java class,
as,
private String professionName;
public String getProfessionName(){
return professionName;
}
public String setProfessionName(String professionName){
this.professionName = professionName
}
and have both the,
applicant.setProfessionId(resultSet.getInt("profession_id"));
applicant.setProfessionName(resultSet.getString("profession_name"));
But Still,
If you are trying, I quote,
instead of profession_id, get a drop down list of available profession
You should approach this differently, because this join will only give one profession_name
for one application
,
Have a loot at taglib and data binding.
EDIT 2:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.