如何从第一个表的customer_id获取customer_id到第二个表?

时间:2012-01-05 06:33:35

标签: mysql html jsp

表单用于从客户处获取信息。 这个表格有几个字段。 来自多个字段的数据必须保存在一列中。 其余的需要保存在另一张表中。

以下是我正在使用的电话线

package web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

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

import com.ead.DBConnect;

/**
 * Servlet implementation class New
 */
public class CustomerReg extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerReg() {
        super();
        // TODO Auto-generated constructor stub
    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        //String Password = request.getParameter("Password");
        String addressno = request.getParameter("addressno");
        String city = request.getParameter("city");
        String country = request.getParameter("country");
        String postalcode = request.getParameter("postalcode");
        String telnum = request.getParameter("telnum");

        DBConnect dbconnect = new DBConnect();
        dbconnect.update("INSERT INTO customer (customer_name, email, address_no, city, country, postal_code) VALUES('"+name+"','"+email+"','"+addressno+"', '"+city+"','"+country+"','"+postalcode+"')");
        int[] id = dbconnect.selectInt("Select LAST_INSERTID()", "customer_id");
        dbconnect.update("INSERT INTO customer_tel (customer_id, tel_no) VALUES('+id+', '"+telnum+"')");

    }

}

如何将数据设置为同一个表中的两个表?

1 个答案:

答案 0 :(得分:0)

我认为表customer_idCUSTOMER 自动增加。我建议您在表TRIGGER上创建CUSTOMER,以便每次在该表上插入记录时,它会自动获取customer_id INSERT customer_tel表。

此链接将指导您创建触发器:Mysql Trigger After Insert