编辑和更新相同的文本框 [英] Edit and update same textbox

查看:103
本文介绍了编辑和更新相同的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表,可以在其中编辑值.我想更新相同的内容,下面是我尝试过的代码.

表代码如下

<table border="1px">
    <tr>
        <td><b>DBID</b></td>
        <td><b>Query Raised</b></td>
        <td><b>Time Raised</b></td>
        <td><b>Query Answered</b></td>
        <td><b>Time Answered</b></td>
    </tr>
<%
try {
    ps = con.prepareStatement("Select DBID, Query_Raised, TR, Query_Answered, TA from Scope1 where TR!='null'");

    rs = ps.executeQuery();

    while(rs.next()) {
%>
    <tr>
        <td><%=rs.getString("DBID")%></td>
        <td><input type="Text" value="<%=rs.getString("Query_Raised")%>" name="Updat"></td>
        <td><%=rs.getString("TR")%> </td>
        <td><%=rs.getString("Query_Answered")%></td>
        <td><%=rs.getString("TA")%></td>
        <td><input type="Submit" value="Update"></td>
    </tr>
<%
    } // while loop ends here

    rs.close();
    con.close();
} catch(Exception e) {
    out.println(e);
}
%>
</table>

,并且使用的更新查询是:

String a = request.getParameter("Updat");

ps = con.prepareStatement("Update Scope1 Set Query_Raised = '" + a + "'");

int i = ps.executeUpdate();

if(i == 1) {
    out.print("Done");
} else{
    out.print("Erro");
}

我想知道在同一页面中更新数据所应使用的条件.

谢谢

解决方案

我理解问题中的第一部分代码是JSP,例如update.jsp,第二部分看起来是您的servlet,例如,UpdateServlet.

因此,在每行的update.jsp中,您都有一个<input type="text">submit按钮,但是<form>我认为只有一个必须在<table>之外,所以这里是我的解决方案(选择任何一个):

  1. 每行使用多个<form>标签,例如

    <form action="whatever_action_you_have_which_calls_the_servlet"
          name="form<%=rs.getString("DBID")%>"
          id="formID<%=rs.getString("DBID")%>">
       // Including name and id so that the different forms remain unique
        <tr>
            <td><%=rs.getString("DBID")%></td>
            <td><input type="Text" value="<%=rs.getString("Query_Raised")%>" name="Updat"></td>
            <td><%=rs.getString("TR")%> </td>
            <td><%=rs.getString("Query_Answered")%></td>
            <td><%=rs.getString("TA")%></td>
            <td><input type="Submit" value="Update"></td>
        </tr>
    </form>
    

    因此,当您单击提交"时,它将提交单击了submit按钮的<form>,并且仅提交已编辑的input,瞧!您的servlet代码也可以正常工作.

  2. 您可以将两个代码块放在同一个JSP中,然后按照要点1所述使用<form>代码段,第二个代码段将是:

    String a = request.getParameter("Updat");
    
    if ( (a is not empty) or (a is not null)  ) {
        ps = con.prepareStatement("Update Scope1 Set Query_Raised = '" + a + "'");
    
        int i = ps.executeUpdate();
    
        if(i == 1) {
            out.print("Done");
        } else{
            out.print("Error");
        }
    }
    
    ...
    
    <form action="whatever_action_you_have_which_will_call_this_JSP" ...>
        <tr> ... your code as in point#1
        </tr>
    </form>
    

  3. Endy所述,请使用ajax和其他JavaScript DOM操作方法来完成此操作.
    为此,您将需要具有JSP(以显示)和& Servlet(更新代码).
    这可能需要更多的精力,但是您将学习Ajax,并且将更加接近真实世界.
    到目前为止,jQuery ajax 是最简单的使用. p>

注意:
仅作记录,我知道您可能正在练习,但是如果您打算在实际项目中使用它,请继续阅读.

在JSP中使用scriptlet是 不良做法 (除非确实需要,如果是视图级逻辑而不是业务级或数据级,也是如此)逻辑),甚至 更糟糕的 都是在JSP中使用JDBC代码.因此,如果您可以按照f_puras的建议.

如果您想知道为什么我应该听您提出的建议,那么请考虑一下:

希望这会有所帮助.

I have a database Table where I can edit a value. I want to update the same, below is the code that I have tried.

The table code is as below

<table border="1px">
    <tr>
        <td><b>DBID</b></td>
        <td><b>Query Raised</b></td>
        <td><b>Time Raised</b></td>
        <td><b>Query Answered</b></td>
        <td><b>Time Answered</b></td>
    </tr>
<%
try {
    ps = con.prepareStatement("Select DBID, Query_Raised, TR, Query_Answered, TA from Scope1 where TR!='null'");

    rs = ps.executeQuery();

    while(rs.next()) {
%>
    <tr>
        <td><%=rs.getString("DBID")%></td>
        <td><input type="Text" value="<%=rs.getString("Query_Raised")%>" name="Updat"></td>
        <td><%=rs.getString("TR")%> </td>
        <td><%=rs.getString("Query_Answered")%></td>
        <td><%=rs.getString("TA")%></td>
        <td><input type="Submit" value="Update"></td>
    </tr>
<%
    } // while loop ends here

    rs.close();
    con.close();
} catch(Exception e) {
    out.println(e);
}
%>
</table>

and the update query that is used is:

String a = request.getParameter("Updat");

ps = con.prepareStatement("Update Scope1 Set Query_Raised = '" + a + "'");

int i = ps.executeUpdate();

if(i == 1) {
    out.print("Done");
} else{
    out.print("Erro");
}

I wanted to know the where condition that I should use to update the data in same page.

Thanks

解决方案

I understand the first code part in the question is a JSP say update.jsp and the second part it seems is your servlet, say UpdateServlet.

So in your update.jsp on every row you have an <input type="text"> and submit button, but the <form> I think is only one which must be outside the <table>, so here goes my solution (choose any one):

  1. Use multiple <form> tags for each row, like

    <form action="whatever_action_you_have_which_calls_the_servlet"
          name="form<%=rs.getString("DBID")%>"
          id="formID<%=rs.getString("DBID")%>">
       // Including name and id so that the different forms remain unique
        <tr>
            <td><%=rs.getString("DBID")%></td>
            <td><input type="Text" value="<%=rs.getString("Query_Raised")%>" name="Updat"></td>
            <td><%=rs.getString("TR")%> </td>
            <td><%=rs.getString("Query_Answered")%></td>
            <td><%=rs.getString("TA")%></td>
            <td><input type="Submit" value="Update"></td>
        </tr>
    </form>
    

    So when you click on submit it would submit the <form> for which the submit button was clicked and would submit only the input which was edited and voila! your servlet code also works fine.

  2. You can have both the blocks of code in the same JSP, then use the <form> code snippet as described in point#1 and the second code snippet would be:

    String a = request.getParameter("Updat");
    
    if ( (a is not empty) or (a is not null)  ) {
        ps = con.prepareStatement("Update Scope1 Set Query_Raised = '" + a + "'");
    
        int i = ps.executeUpdate();
    
        if(i == 1) {
            out.print("Done");
        } else{
            out.print("Error");
        }
    }
    
    ...
    
    <form action="whatever_action_you_have_which_will_call_this_JSP" ...>
        <tr> ... your code as in point#1
        </tr>
    </form>
    

  3. Use ajax and other javascript DOM manipulative methods to accomplish this as said by Endy.
    For this you will need to have JSP (to display) & Servlet (to update the code).
    This might be a little more effort but you will learn Ajax and will be a little more close to real world.
    By far jQuery ajax is the simplest to use.

Note:
Just for the record, I know you might be practicing but if you are planning to use it in a real project then please read ahead.

It is a bad-practice to use scriptlets in JSP (unless really required and that too if it is for view-level logic and not business or data-level logic) and even worse is to use JDBC code inside a JSP. So it would be really great if you could follow f_puras's advice.

If you are wondering why I should listen to your unsolicited advice then here is some food for thought:

Hope this helps.

这篇关于编辑和更新相同的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆