简体   繁体   中英

Edit a table in JSP page which edits the database

my question is simple,i have made a JSP page having a table which displays the content of the database,now what m trying is to edit the details in table,which simultaneously edits the values in the database. I have written the code,everything looks good but ,its not editing the database,what to do ?? Help is seriously needed and is appreciated a lot. Thanks in advance.

<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*" %>    

<!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>Insert title here</title>
</head>
<body>

</body>
<form method="post">

<table border="7">
<tr>
<td>ID</td>
<td>NAME</td>
<td>SKILL</td>
<td colspan="2" align="center">ACTION</td>
</tr>


<%
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/test";
String username="root";
String password="root";
String query="select * from jsp1";

Connection conn=DriverManager.getConnection(url,username,password);
Statement stmt=conn.createStatement();


ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{

%>
    <tr>
    <td><%=rs.getInt("ID") %></td>
    <td><input type="text" value="<%=rs.getString("NAME") %>"></td>
    <td><input type="text" value="<%=rs.getString("SKILL") %>"></td>
    <td><input type="button" name="UPDATE" value="UPDATE" onclick="
    <% 
    String qmod="update jsp1 set NAME=?,SKILL=? where ID=? ";
    PreparedStatement pstmt=conn.prepareStatement(qmod);
    String one=request.getParameter("NAME");
    String two=request.getParameter("SKILL");
    String three=request.getParameter("ID");
    pstmt.setString(1,one);
    pstmt.setString(2,two);
    pstmt.setString(3,three);
    pstmt.executeUpdate(); 
    %>"></td>
    <td> <input type="button" name="DELETE" value="DELETE"></td>
    </tr>
        <%

}
%>
    </table>
    <%
    rs.close();
    stmt.close();
    conn.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        }






%>

</form>
</html>

A good way to go around this problem would be

  1. Have a servlet sitting on the backend to which can interact with database.
  2. Whenever user clicks on the table and modifies it, you send an ajax request to this servlet.
  3. The servlet interacts with the database and calls the appropriate UPDATE table methods.

You should have a look at AJAX to get started if you want to have things done on the fly.

onClick is a JavaScript thing. It doesn't submit the form, hence, request.getParameter won't work.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM