使用servlet中找到的arraylist使用preparedstatement插入数据库

时间:2014-12-18 08:16:05

标签: java mysql servlet-3.0

我有错误,我发送和数组到servlet那里我得到参数值并尝试使用它们存储到mysql数据库,但两个问题,获取参数值和插入数据库是好的,但我得到的是什么第一行插入的值是不需要的,我不知道它是如何发生的,第二个问题是,当preparestatement将值插入数据库时​​我想使用另一个预备语句来更改另一个表。

这是jsp页面

         <script src="js/jquery-1.11.1.min.js"></script> 
        $("#formGenerate").submit(function(event) { // function to process submitted table
        var a = {};

        var tableData = []; // we will store rows' data into this array
        $("#adminTable") // select table by id
                .find(".tableRow") // select rows by class
                .has(":checked") // select only rows with checked checkboxes
                .each(function() { // for each selected row extract data               
                    var tableRow = {};
                    var jRow = $(this);


                    tableRow[0] = jRow.find('td.customerId').text();
                    tableRow[1] = jRow.find('td.customerName').text();
                    tableRow[2] = jRow.find('td.customerType').text();
                    tableRow[3] = jRow.find('td.customerKWH').text();
                    tableRow[4] = jRow.find('td.customerFaraq').text();
                    tableRow[5] = jRow.find('td.customerLacag').text();
                    tableRow[6] = jRow.find('td.customerTax').text();
                    tableRow[7] = jRow.find('input.name1').val();

                    tableRow[8] = $('.bishaa').val();
                    tableRow[9] = $('.readers').val();
                    tableData.push(tableRow);




                });

        $.post(
                "generateKwd", /*url of consuming servlet*/
                {tableData: tableData}, /*data*/

        function() {
            alert("Success!");

        }, /*function to execute in case of success*/
                "json" /* data type */

                );


        event.preventDefault(); //Prevent sending form by browser

        location.reload();
      }
       );

 <form id="frmShow" action="showKwh" method="POST">
            <label>ID</label>
            <input type="text" id="id" name="id"/>
            <label>Bisha</label>
            <input type="text" id="bisha" name="bisha" class="bishaa" value="${param.bisha}"/>
            <label>Reader</label>
            <input type="text" id="reader" name="reader" class="readers" value="${param.reader}"/>
            <input type="submit" value="show" id="submit"/>
        </form>
       <form id="formGenerate" action="generateKwd" method="POST">
    <input type="submit" value="Generate"/>
    <table id="adminTable" class="detailsTable">

        <tr class="header">
            <th colspan="4">Kilowat Entry</th>
        </tr>

        <tr class="tableHeading">
            <td></td>
            <td>customer id</td>
            <td>name</td>

            <td >group</td>
            <td>kwh</td>

            <td>faraq</td>
            <td>lacag</td>
            <td>tax</td>

            <td>kwd</td>

        </tr>

        <c:forEach  var="cust" items="${customerKwh}" varStatus="iter">
            <tr id="${cust.id}" class="${((iter.index % 2) == 1) ? 'lightBlue' : 'white'} tableRow">
                <td><input type="checkbox" name="check1" class="checker" value="ON" /></td>
                <td id="id?${customer.id}" class="customerId">${cust.id}</td>
                <td class="customerName" id="names">${cust.name}</td>
                <td  class="customerType">${cust.type}</td>

                <td class="customerKWH" >${cust.kwh}</td>

                <td class="customerFaraq"></td>
                <td class="customerLacag"></td>
                <td class="customerTax"></td>

                <td><input type="text" name="kwd" size="8" id="kwd${cust.id}" value="${customer.id}" class="name1"  />

                </td>

            </tr>
        </c:forEach>
    </table>
</form>

这是在servlet中

(userPath.equals("/generateKwd")) {

        RateDao rate = new RateDao();
        Enumeration tableData1 = request.getParameterNames();



       while (tableData1.hasMoreElements()) {



           String paramId  = (String) tableData1.nextElement();
          String paramName = (String) tableData1.nextElement();
           String paramType = (String) tableData1.nextElement();
           String paramKWH  = (String) tableData1.nextElement();
            String paramFaraq = (String) tableData1.nextElement();
             String paramLacag = (String) tableData1.nextElement();
             String paramTax = (String) tableData1.nextElement();
            String paramKWD = (String) tableData1.nextElement();
            String paramBisha = (String) tableData1.nextElement();
            String paramReader = (String) tableData1.nextElement();

            String[] paramIdValues = request.getParameterValues(paramId);
            String[] paramNameValues = request.getParameterValues(paramName);
            String[] paramTypeValues = request.getParameterValues(paramType);
            String[] paramKWHValues = request.getParameterValues(paramKWH);
            String[] paramFaraqValues = request.getParameterValues(paramFaraq);
             String[] paramLacagValues = request.getParameterValues(paramLacag);
            String[] paramTaxValues = request.getParameterValues(paramTax);
           String[] paramKWDValues= request.getParameterValues(paramKWD);
            String[] paramBishaValues= request.getParameterValues(paramBisha);
           String[] paramReaderValues= request.getParameterValues(paramReader);

            for (int i = 0; i < paramIdValues.length; i++) {
                String paramIdValue = paramIdValues[i];
                String paramNameValue = paramNameValues[i];

                String paramTypeValue = paramTypeValues[i];
                String paramKWHValue = paramKWHValues[i];
                String paramFaraqValue = paramFaraqValues[i];
                String paramLacagValue = paramLacagValues[i];
                String paramTaxValue = paramTaxValues[i];
                String paramKWDValue = paramKWDValues[i];
                String paramBishaValue = paramBishaValues[i];
                String paramReaderValue = paramReaderValues[i];

                rate.addKulmiye(paramIdValue, paramNameValue, paramTypeValue, paramKWHValue,
                       paramFaraqValue, paramLacagValue, paramTaxValue, paramKWDValue, paramBishaValue, paramReaderValue);

                //customerDao.updateKwh(paramIdValue, paramKWHValue);

            }
        }

        userPath = "/kilowat";
    }

这是用于插入值的java方法

 public void addKulmiye(String strId, String name, String group, String strKwh, String strFaraq, String strLacag, String strTax, String strKwd, String bisha, String reader) {
    String insertQuery = "INSERT INTO KULMIYE( ID,NAME,GROUPS,KWH,FARAQ,LACAG,CANSHUUR,KWD,BISHA,READER,BIXIN,DISCOUNT) "
            + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
    try {
        pStmt = dbConnection.prepareStatement(insertQuery);
        //pStmt.setInt(1, customer.getId());
        int id = Integer.parseInt(strId);
        int kwh = Integer.parseInt(strKwh);
        int kwd = Integer.parseInt(strKwd);
        float faraq = Float.parseFloat(strFaraq);
        float lacag = Float.parseFloat(strLacag);
        float tax = Float.parseFloat(strTax);
        pStmt.setInt(1, id);
        pStmt.setString(2, name);
        pStmt.setString(3, group);
        pStmt.setInt(4, kwh);

        pStmt.setFloat(5, faraq);
        pStmt.setFloat(6, lacag);
        pStmt.setFloat(7, tax);
        pStmt.setInt(8, kwd);
        pStmt.setString(9, bisha);
        pStmt.setString(10, reader);
        pStmt.setFloat(11, 0);
        pStmt.setFloat(12, 0);


        pStmt.execute();

    } catch (SQLException e) {
        System.err.println(e.getMessage());
    }
    finally {
        try {
            if (pStmt != null) {
            pStmt.close();
            }
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}

这是我用来插入

的mysql表
  CREATE TABLE `kulmiye` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `bisha` varchar(45) DEFAULT NULL,
  `kwh` int(11) DEFAULT NULL,
  `kwd` int(11) DEFAULT NULL,
  `faraq` int(11) DEFAULT NULL,
  `lacag` float DEFAULT NULL,
  `canshuur` float DEFAULT NULL,
  `reader` varchar(45) DEFAULT NULL,
  `bixin` float DEFAULT NULL,
  `discount` float DEFAULT NULL,
  `trno` int(11) NOT NULL AUTO_INCREMENT,
   `taariikh` datetime DEFAULT NULL,
   `groups` varchar(45) DEFAULT NULL,
   PRIMARY KEY (`trno`)
   ) ENGINE=InnoDB AUTO_INCREMENT=560 DEFAULT CHARSET=latin1;

插入成功,但第一个显示意外的值,而不是导致

的原因

0 个答案:

没有答案