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
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.