Click here to Skip to main content
15,886,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

			//1. get connection
			DBManager dbMgr=new DBManager();
			Connection conn=dbMgr.getConnection();

			//2.prepare statement

			String sql = "Update keeper Set keepername=?,keeperrank=?,dateofbirth=? where keeperID=?";

			PreparedStatement pstmt=conn.prepareStatement(sql);

			// first and second paramters inserted 1. course name and course text
			
			pstmt.setString(1, c.getKeeperName());
			pstmt.setString(2, c.getKeeperRank());
			pstmt.setString(3, c.getDateKeeper());
			pstmt.setString(4, c.getKeeperId());



			//3.execute statement

			result = pstmt.executeUpdate();

			return result;

			}


getById function
public KeeperK getById(String id) throws SQLException, ClassNotFoundException {
			String sql ;
			//1. get Connection
			DBManager dbMgr = new DBManager();
			Connection conn = dbMgr.getConnection();

			//2. prepare the statement
			sql = "SELECT * FROM keeper WHERE keeperID = ?" ;



			PreparedStatement pstmt = conn.prepareStatement(sql);
			//First parameter is the course id of the course entity
			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) {
	             //out.println( course.getKeeper() + "\t" + course.getKeepername() + "\t" + course.getCourseText());
%>	             

		            
	             	<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.
Posted
Updated 10-Dec-21 2:02am

1 solution

Solution has been found! by myself.
Turns out i put /form wayy up

what a clumsy me
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900