如何从数据库中获取值并自动发布到动态文本框中

时间:2016-05-11 10:30:48

标签: javascript jquery ajax jsp servlets

我有一个javacript函数,用于添加和删除动态文本框,如下所示enter image description here

当我点击保存值正在保存到数据库中。但我的问题是当我点击页面(这是链接)时,它应该将数据库中的值显示在这些文本框中,如下所示{{3} }

如上所示,当我点击“Tax Info”时,它应该自动将数据库的值显示到动态创建的文本框中。到目前为止我所做的代码如下所示。任何想法或给出的解决方案将不胜感激。数据库表是 enter image description here

taxInfo.jsp

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    $("#btnAdd").bind("click", function () {
        var div = $("<div />");
        div.html(GetDynamicTextBox(""));
        $("#TextBoxContainer").append(div);
    });
    $("#btnGet").bind("click", function () {
        var values = "";
        var values1 = "";
        var values2 = "";
        $("input[name=DynamicTextBox]").each(function () {
            values += $(this).val() + "\n";
        });

        $("input[name=DynamicTextBox1]").each(function () {
            values1 += $(this).val() + "\n";
        });

        $("input[name=DynamicTextBox2]").each(function () {
            values2 += $(this).val() + "\n";
        });

        document.location.href ="taxInfoDB.jsp?values=" + values + "&values1=" + values1 + "&values2=" + values2 + "";
    });
    $("body").on("click", ".remove", function () {
        $(this).closest("div").remove();
    });
});
function GetDynamicTextBox(value) {
    return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />&nbsp;' + '<input name = "DynamicTextBox1" type="text" value = "' + value + '" />&nbsp;' + '<input name = "DynamicTextBox2" type="text" value = "' + value + '" />&nbsp;' +
            '<input type="button" value="Remove" class="remove" />'
}
</script>

        </div>
     <div id="divMenu">
                <ul>
                    <li><a class="current" href="/SalesPropeller/Admin/Profile/Phome.jsp">Profile</a></li>
                    <li><a href="/SalesPropeller/Admin/ManageUser/Mhome.jsp">Manage User</a></li>
                    <li><a href="/SalesPropeller/Admin/Lead/home.jsp">Lead Management</a></li>
                    <li><a href="/SalesPropeller/Admin/Purchase/Puhome.jsp">Purchase</a></li>
                    <li><a href="/SalesPropeller/Admin/Sales/Shome.jsp">Sales</a></li>
                    <li><a href="/SalesPropeller/Admin/Payroll/Phome.jsp">PayRoll</a></li>
                    <li><a href="/SalesPropeller/Admin/CustomerComplain/chome.jsp">Customer Services</a></li>
                </ul>
            </div>
        <div id="divLeft">
            <div id="divWelcome">
                Welcome<br /> <b>${sessionScope.name}</b>
            </div>
            <div id="divLeftTop">
                <img
                    src="/SalesPropeller/Profile/displayblob.jsp"
                    height="160" width="140" />
            </div>
            <hr />
            <ul id="MenuBar1" class="MenuBarHorizontal">
                <li><a href="/SalesPropeller/Admin/Profile/Phome.jsp">Home</a></li>
                <li><a href="/SalesPropeller/Admin/Profile/profile.jsp">Admin
                        Profile</a></li>
                <li><a href="/SalesPropeller/Admin/Profile/addCompanyProfile.jsp">Company Profile</a></li>

                <li><a class="current" href="/SalesPropeller/Admin/Profile/taxInfo.jsp">Tax Info</a></li>
                <li><a class="MenuBarItemSubmenu"
                    href="/SalesPropeller/Admin/Profile/message.jsp">Messages</a>
                    <ul>
                        <li><a href="/SalesPropeller/Admin/Profile/compose.jsp">Compose
                                New</a></li>
                        <li><a href="/SalesPropeller/Admin/Profile/inbox.jsp">Inbox</a></li>
                        <li><a href="/SalesPropeller/Admin/Profile/outbox.jsp">Sent Messages</a></li>
                    </ul></li>
                <li><a href="/SalesPropeller/logout.jsp">Logout</a></li>
            </ul>
            <script type="text/javascript">
                    <!--
                    var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
                    //-->
                </script>

        </div>
        <div id="divRight">
            <br />


                 <form name="form"  method="post">
            <div align="center">
                <input id="btnAdd" type="button" value="Add" />
<br /><br/>
<b style="padding-right:100px">Name of the Tax</b> <b style="padding-right:100px">Value</b> <b style="padding-right:100px">TaxGroup</b>  
<div id="TextBoxContainer">
    <!--Textboxes will be added here -->
</div>
<br />
<input id="btnGet" type="button" value="Save" />
</div>
            </form>
        </div>

taxInfoDB.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.model.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.DecimalFormat"%>
<!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>Tax Saving Database</title>
</head>
<body>
<%
String values = request.getParameter("values");
String values1 = request.getParameter("values1");
String values2 = request.getParameter("values2");
System.out.println("values are:"+values);
System.out.println("values1 are:" +values1);
System.out.println("values2 are:" +values2);
try{
    DBConnect db =new DBConnect();
    Connection con = db.getCon();
    String sql ="insert IGNORE_DUP_KEY = ON into marketing_database.tax_info (TaxGroup,Name,Value) values (?,?,?);";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, values2);
    ps.setString(2, values);
    ps.setString(3, values1);
    ps.executeUpdate();
    con.close();
    ps.close();

        }
        catch(SQLException ex){
    ex.printStackTrace();
        }

response.sendRedirect("taxInfo.jsp");
%>

</body>
</html>

1 个答案:

答案 0 :(得分:-2)

您可能想尝试MVC模型。 简单来说,您可能希望将所有Java代码放在.java文件中。在该java类中,您可以将数据从Database读取到java对象中,并将该对象放入HttpServletRequest,HttpSession或application和sendRedirect到jsp页面。在jsp页面中,您可以从先前设置的对象中读取数据。