列数与row的值计数不匹配。如何匹配该值

时间:2014-02-25 09:58:29

标签: java mysql sql servlets

我正在使用jsp将数据插入mysql数据库,为什么它没有进入数据库。错误显示“列数与第1行的值计数不匹配” 任何人都可以解决我的问题,并说我出错的地方请帮助我

 java code
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Dealer extends HttpServlet {
private static final long serialVersionUID = 1L;


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{
int a1=0;
response.setContentType("text/html");
PrintWriter out= response.getWriter();


    String dealId=request.getParameter("dealer_id");
    System.out.println(dealId);

    String dealName=request.getParameter("dealer_name");
    System.out.println(dealName);

    String doorNo=request.getParameter("door_no");
    System.out.println(doorNo);

    String street=request.getParameter("street");
    System.out.println(street);

    String city=request.getParameter("city");
    System.out.println(city);

    String district=request.getParameter("district");
    System.out.println(district);

    String state=request.getParameter("state");
    System.out.println(state);

    String pinCode=request.getParameter("pin_code");
    System.out.println(pinCode);

    String mob=request.getParameter("mobile");
    System.out.println(mob);

    String contactPerson=request.getParameter("contact_person");
    System.out.println(contactPerson);

    String phoneNo=request.getParameter("phone_no");
    System.out.println(phoneNo);

    String emailId=request.getParameter("email_id");
    System.out.println(emailId);

    String fax=request.getParameter("fax");
    System.out.println(fax);

    String crdOffI=request.getParameter("credit_offered_i");
    System.out.println(crdOffI);

    String crdOff=request.getParameter("credit_offered");
    System.out.println(crdOff);

    String vendorRating=request.getParameter("vendor_rating");
    System.out.println(vendorRating);

    String gstNo=request.getParameter("gst_no");
    System.out.println(gstNo);

    String cstNo=request.getParameter("cst_no");
    System.out.println(cstNo);

    String remarks=request.getParameter("remarks");
    System.out.println(remarks);


    String saveOrUpdate = request.getParameter("hiddenValue");
    System.out.println(saveOrUpdate);



try

{
Class.forName("com.mysql.jdbc.Driver");

System.out.println("driver loaded");
System.out.println("Driver is loaded");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/charms?user=root&password=root");
System.out.println("Connection created");
PreparedStatement ps =null;
    if(saveOrUpdate.equals("update"))
    {
        ps= ((java.sql.Connection) con).prepareStatement("update dealer_masters set deal_name_v =?, deal_door_no_v =?, deal_street_v =?, deal_city_v =?, deal_district_v=?, deal_state_v=?, deal_contactperson_v=?, deal_phone_no_v=?, deal_mobile_no_v=?, deal_faxno_v=?, deal_email_id_v=?, deal_creditoffered_i=?, deal_period_v=?, deal_vendor_rating_v=?, deal_CST_No_v=?, deal_GST_No_V=?,Remarks_v =? where deal_id_v=? ");

        ps.setString(1,dealName);
        ps.setString(2,doorNo);
        ps.setString(3,street);
        ps.setString(4,city);
        ps.setString(5,district);
        ps.setString(6,state);
        ps.setString(7,pinCode);
        ps.setString(8,contactPerson);
        ps.setString(9,phoneNo);
        ps.setString(10,mob);
        ps.setString(11,fax); 
        ps.setString(12,emailId);
        ps.setString(13,crdOffI);
        ps.setString(14,crdOff);
        ps.setString(15,vendorRating);
        ps.setString(16,cstNo);
        ps.setString(17,gstNo);
        ps.setString(18,remarks);
        ps.setString(19,dealId);


        a1=ps.executeUpdate();
        if(a1==1)
        {
            System.out.println("Inserted");
            request.getSession().setAttribute("status1", "updatesuccess");
        }

    }
    else
    {   

         ps= ((java.sql.Connection) con).prepareStatement("insert into dealer_masters(deal_id_v,deal_name_v, deal_door_no_v, deal_street_v, deal_city_v, deal_district, deal_state_v, deal_contactperson_v, deal_phone_no_v, deal_mobile_no_v, deal_faxno_v, deal_email_id_v, deal_creditoffered_i, deal_period_v, deal_vendor_rating_v, deal_CST_No_v, deal_GST_No_V,Remarks_v) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            ps.setString(1,dealId);
            ps.setString(2,dealName);
            ps.setString(3,doorNo);
            ps.setString(4,street);
            ps.setString(5,city);
            ps.setString(6,district);
            ps.setString(7,state);
            ps.setString(8,pinCode);
            ps.setString(9,contactPerson);
            ps.setString(10,phoneNo);
            ps.setString(11,mob);
            ps.setString(12,fax); 
            ps.setString(13,emailId);
            ps.setString(14,crdOffI);
            ps.setString(15,crdOff);
            ps.setString(16,vendorRating);
            ps.setString(17,cstNo);
            ps.setString(18,gstNo);
            ps.setString(19,remarks);


        a1=ps.executeUpdate();
        System.out.println("Inserted");
        request.getSession().setAttribute("status1", "success");
    }

}
catch(Exception e1)

{

System.out.println(e1.getMessage());
request.getSession().setAttribute("status1", "fail");
}

    response.sendRedirect("dealer.jsp");

}
}

2 个答案:

答案 0 :(得分:1)

在此声明中:

insert into dealer_masters(
deal_id_v,
deal_name_v, 
deal_door_no_v, 
deal_street_v, 
deal_city_v, 
deal_district, 
deal_state_v, 
deal_contactperson_v, 
deal_phone_no_v, 
deal_mobile_no_v, 
deal_faxno_v, 
deal_email_id_v, 
deal_creditoffered_i, 
deal_period_v, 
deal_vendor_rating_v, 
deal_CST_No_v, 
deal_GST_No_V,
Remarks_v) /* 18 columns */
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) /* 19 parameters*/

列列表包含18列,但值列表包含19个参数。我假设你添加了一个额外的参数或错过了一列。必须更改SQL语句,以使列和参数列表等于大小。

答案 1 :(得分:1)

您的查询中使用了18个绑定参数,但您设置了19个接缝,另一个deal_creditoffered缺失