У меня есть таблица с именем clubs
с 2 полями (clubid, club), и я хотел бы создать форму для добавления нескольких клубов.
Мои методы addClub()
и getAllClubs()
приведены ниже:
public class ClubDao {
private Connection conn;
public ClubDao() {
conn = ConnectionProvider.getConnection();
}
public void addClub(ClubBean clubBean) {
try {
String sql = "INSERT INTO clubs(club) "
+ " VALUES (?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, clubBean.getnameClub());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List getAllClubs() {
List clubs = new ArrayList();
try {
String sql = "SELECT * FROM clubs";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
ClubBean clubBean = new ClubBean();
clubBean.setId(rs.getInt("clubid"));
clubBean.setnameClub(rs.getString("club"));
clubs.add(clubBean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return clubs;
}
Моя проблема в том, что мне нужен сервлет, я не понимаю, как создать мою форму создания
public class ClubHandler extends HttpServlet {
private static String INSERT = "/club.jsp";
private static String Edit = "/edit.jsp";
private static String ClubRecord = "/listClub.jsp";
private ClubDao dao;
public ClubHandler() {
super();
dao = new ClubDao();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
?????
club. jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add New Club</title>
</head>
<body>
<form method="POST" action='ClubHandler' name="frmAddClub"><input
type="hidden" name="action" value="insert" />
<p><b>Add New Record</b></p>
<table>
<tr>
<td>Club Name : </td>
<td><input type="text" name="club" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<p><a href="ClubHandler?action=listClub">View-All-Records</a></p>
</body>
</html>
Спасибо за вашу помощь.