Spring:使用invoiceId作为外键将多个发票行插入数据库

时间:2015-05-11 11:38:28

标签: javascript mysql spring

<tr>
    <td><input type="text" name="description[0]"/></td>
    <td><input type="text" name="quantity[0]" id="quantity[0]" onkeyup="sum(0);" /></td>
    <td><input type="text" name="price[0]" id="price[0]" onkeyup="sum(0);"/></td>
    <td><input type="text" name="lineTotal[0]" id="lineTotal[0]" readonly /></td>
    <td><input type="button" onclick="addLine();" value="+" /></td>
</tr>

<script type="text/javascript">    
    var i = 0;
    function addLine() {
            i++;        
            var row = document.createElement('tr');
            var td = document.createElement('td');
             td.innerHTML = '<input type="text" name="description['+i+']" id="description['+i+']"/>';
                row.appendChild(td);
                td = document.createElement('td');
                td.innerHTML = '<input type="text" name="quantity['+i+']" id="quantity['+i+']" onkeyup="sum('+i+');"/>';
                row.appendChild(td);
                td = document.createElement('td');
                td.innerHTML = '<input type="text" name="price['+i+']" id="price['+i+']" onkeyup="sum('+i+');"/>';
                row.appendChild(td);
                td = document.createElement('td');
                td.innerHTML = '<input type="text" name="lineTotal['+i+']" id="lineTotal['+i+']" readonly/>';
                row.appendChild(td);
                td = document.createElement('td');
                td.innerHTML = '<input type="button" onclick="removeLine(this.parentNode.parentNode)" value="-">';
                row.appendChild(td);
                td = document.createElement('td');
                td.innerHTML = '<input type="button" onclick="addLine();" value="+" />';
                row.appendChild(td);

                document.getElementById('items').appendChild(row);
            }

            function removeLine(row) {
              document.getElementById('items').removeChild(row);
            }


        function sum(i) {
            var q = document.getElementById('quantity['+i+']').value;
            var p = document.getElementById('price['+i+']').value;
            var result = parseFloat(q) * parseFloat(p);

            if(q=="" || p==""){
                document.getElementById('lineTotal['+i+']').value = 0;
            }

            if (!isNaN(result)) {
                document.getElementById('lineTotal['+i+']').value = result.toFixed(2);
            } 
        }

public static void invoiceDescription(
            long invoiceId, String description, int quantity, 
            int price, int lineTotal){

            Connection dbConnection = null;
            PreparedStatement preparedStatement = null;

            String sql = "INSERT INTO invoice_description"
                    + "(descId, invoiceId, description, quantity, price, total) VALUES"
                    + "(NULL,?,?,?,?,?)";

            try {

                dbConnection = getDBConnection();

                preparedStatement = dbConnection.prepareStatement(sql.toString());


                preparedStatement.setLong(1, invoiceId);
                preparedStatement.setString(2, description);
                preparedStatement.setInt(3, quantity);
                preparedStatement.setInt(4, price);
                preparedStatement.setInt(5, lineTotal);

                preparedStatement.executeUpdate();

                System.out.println("You have successfully added descriptions to invoice");


            } catch (SQLException e){

                System.out.println(e.getMessage());

            }
    }

上面是我的html,javascript和数据库管理器,用于将数据插入数据库。由于我能够通过javascript向html添加新行,但我无法将多个数据输入数据库。

我的控制器看起来像这样:

@RequestMapping(value = "/invoice", method = RequestMethod.POST)
    public String submitInvoice(HttpServletRequest request, 
            @RequestParam("clients") int clientId,
            @RequestParam("invoice_date") String invoice_date, 
            @RequestParam("invoice_due_date") String invoice_due_date, 
            @RequestParam("status") String status, 
            @RequestParam("payment_method") String payment_method, 
            @RequestParam("currency") String currency, 
            @RequestParam("description") String description,        @RequestParam("quantity") int quantity, @RequestParam("price") int price, @RequestParam("lineTotal") String lineTotal, 
) {

        if(!hasRole(request, "ROLE_USER")){
            return "403";
        }


         long invoiceId = DBManager.createInvoice(clientId, invoice_date, invoice_due_date, status, payment_method, currency);
        DBManager.invoiceDescription(invoiceId, description, quantity, price, lineTotal);

        return "redirect:/invoice/detail/" + invoiceId;
    }

0 个答案:

没有答案