Hello, fairly new to coding here.
I basically want to
"Update" data that is connected to a mysql database
but i have a problem
is that , The Update Button is not working.
Heres my related codes
Keeper.java
This is where the update and getbyId is defined i really dont think that theres any problem here.
maybe i have some kind of format wrong on
"Update keeper set"
Update function
public int update(KeeperK c) throws ClassNotFoundException, SQLException{
int result=-1;
DBManager dbMgr=new DBManager();
Connection conn=dbMgr.getConnection();
String sql = "Update keeper Set keepername=?,keeperrank=?,dateofbirth=? where keeperID=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, c.getKeeperName());
pstmt.setString(2, c.getKeeperRank());
pstmt.setString(3, c.getDateKeeper());
pstmt.setString(4, c.getKeeperId());
result = pstmt.executeUpdate();
return result;
}
getById function
public KeeperK getById(String id) throws SQLException, ClassNotFoundException {
String sql ;
DBManager dbMgr = new DBManager();
Connection conn = dbMgr.getConnection();
sql = "SELECT * FROM keeper WHERE keeperID = ?" ;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery();
KeeperK course = null;
while(rs.next()) {
course = new KeeperK(rs.getString("keeperID"),
rs.getString("keepername"),
rs.getString("keeperrank"), rs.getString("dateofbirth"));
}
return course;
}
course_list.jsp
so basically , this will open up a page that have all the data
and after clicking "Edit"
Another page will load.
try {
DBManager dbMgr = new DBManager();
dbMgr.getConnection();
Keeper dao = new Keeper();
ArrayList<KeeperK> courses = dao.fetchAll();
%>
<table border="1">
<tr>
<th>Keeper Id</th>
<th>Keeper Name</th>
<th>Keeper Rank</th>
<th>Date of birth</th>
<th>Edit Keeper</th>
<th>Delete Keeper</th>
</tr>
<%
for (KeeperK course : courses) {
%>
<tr>
<td> <%=course.getKeeperId()%> </td>
<td> <%=course.getKeeperName()%> </td>
<td> <%=course.getKeeperRank()%> </td>
<td> <%=course.getDateKeeper()%> </td>
<td><a href="edit_course.jsp?id=<%=course.getKeeperId()%>">Edit</a></td>
<td><a href="delete_course.jsp?id=<%=course.getKeeperId()%>">Delete</a></td>
</tr>
edit_course.jsp
This will take another action from update_action.jsp
This is where i think the problem is but i cant find it ._.
<form method="POST" action="update_action.jsp"></form>
<%
String keeperID = request.getParameter("id");
if (keeperID == null){
out.println("Keeper ID is not found");
} else {
String id = keeperID;
Keeper dao = new Keeper();
try {
KeeperK c = dao.getById(id);
out.print(c);
%>
<input type="hidden" name="keeperID" value="<%=c.getKeeperId()%>" />
<label for="keepername">Keeper Name</label>
<input type="text" name="keepername" id="keepername" value="<%=c.getKeeperName()%>" required/>
<label for="keeperrank">Keeper Rank</label>
<input type="text" name="keeperrank" id="keeperrank" value="<%=c.getKeeperRank()%>" />
<label for="dateofbirth">Date of birth</label>
<input type="text" name="dateofbirth" id="dateofbirth" value="<%=c.getDateKeeper()%>" />
<input type="submit" value="Update" />
<%
}catch (ClassNotFoundException ex){
out.println( ex.getMessage());
}catch (SQLException e){
out.println(e.getMessage());
}
}
%>
update_action.jsp
<body>
<%
String keeperID = request.getParameter("keeperID");
String keepername = request.getParameter("keepername");
String keeperrank = request.getParameter("keeperrank");
String dateofbirth = request.getParameter("dataofbirth");
out.println (keeperID);
out.println (keepername);
out.println (keeperrank);
out.println (dateofbirth);
Keeper dao = new Keeper();
KeeperK c = new KeeperK(keeperID, keepername, keeperrank, dateofbirth);
int rowsAffected;
try {
rowsAffected = dao.update(c);
out.println("No of rows affected - " + rowsAffected);
}catch (ClassNotFoundException ex){
out.println( ex.getMessage());
}catch (SQLException e){
out.println(e.getMessage());
}
%>
</body>
What I have tried:
I tried changing , the input type="submit" value="Update" to
a button type but that also doesnt work.
It doesnt show errors either , it just kinda like not accpeting it?
telling me what could be wrong would help as well if you cant find a direct solution.