如何在JSP中执行更新操作

时间:2014-07-22 09:20:43

标签: oracle jsp sql-update

我一直在学习JSP一周,并试图在示例联系我们表中进行更新操作。我使用oracle 10g作为后端。 这是editmsg.jsp,它选择要更新的特定记录。我有“按名称选择”而不是ID。

 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%@ page import="java.sql.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Edit page</title>
      </head>
      <body>
       <form action="asave.jsp">
     <%
         String n=request.getParameter("val");

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from contactus where name ='"+n+"'");
while(rs.next()){
//con.close();
%>


            <table><tr> <td>Name:</td><td><input type="text" name="aname" value="<%=rs.getString("name")%>"></td></tr>

            <tr> <td>Email:</td><td><input type="text" name="aemail" value="<%=rs.getString(2)%>"></td></tr>
            <tr> <td>Phone:</td><td><input type="text" name="aphone" value="<%=rs.getString(3)%>"></td></tr>
            <tr> <td>Message:</td><td><input type="text" name="amessage" value="<%=rs.getString(4)%>"></td></tr>
            <tr><td></td><td><input type="submit" value="Submit"></td><td><input type="button" value="Cancel" onclick='javascript:window.location='Home.jsp';'></td></tr>
            </table>
            <%} %>


</form>


</body>
</html>

如果单击“提交”按钮,则应执行更新功能 asave.jsp

String aname=request.getParameter("val");
String aemail=(String)session.getAttribute("aemail");
Integer aphone=(Integer)session.getAttribute("aphone");
String amessage=(String)session.getAttribute("amessage");
System.out.print(aname);

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");

PreparedStatement ps=con.prepareStatement("update contactus set email=?,phone=?,message=? where name='"+aname+"'");
ps.setString(1,aemail);
ps.setInt(2,aphone);
ps.setString(3,amessage);
ps.executeQuery();
con.close();
}catch(Exception e){e.printStackTrace();}

%>
<jsp:forward page="messages.jsp"></jsp:forward>

我点击提交,而不是获取更新的记录详细信息,它保持不变.BTW,我也尝试了executeUpdate()。那没起效。 请帮忙..

0 个答案:

没有答案