如:A表中有 a1,b1,c1,a2 四个字段,B表中有b1,b2,两个字段,C表中有c1,c2两个字段,a1,b1,c1分别为 A,B,C表的主键,现要对A表进行增、删、改,sql语句写法? 
本人设计时考虑时,遇到的第一个问题:
a1为A的ID,a2为其名字,b1、c1为B、C的ID,b2,c2为名字,界面设计时把b1,c1字段都设计为下拉框,但现在我遇到的表B中字段特别多,如果都设计为下拉框,半个屏幕都成了下拉框,以及后台操作用的sql语句也不知道怎么写?会者帮忙,谢谢!

解决方案 »

  1.   

    如果你用了hibernate就设置级联删除
    如果没有用hibernate 就需要 写n条sql语句了,先删除子表,然后删除主表内容,不过这个要注意使用事务去处理。
      

  2.   

    beanpackage org.bean;public class FligthInfo {
    private int flightId;
    private String fligthNo;
    private String fligthTime;
    private String fligthPrice;
    private int corporationId;                 //B表字段
    private String corporationName;            //B表字段
    public int getFlightId() {
    return flightId;
    }
    public void setFlightId(int flightId) {
    this.flightId = flightId;
    }
    public String getFligthNo() {
    return fligthNo;
    }
    public void setFligthNo(String fligthNo) {
    this.fligthNo = fligthNo;
    }
    public String getFligthTime() {
    return fligthTime;
    }
    public void setFligthTime(String fligthTime) {
    this.fligthTime = fligthTime;
    }
    public String getFligthPrice() {
    return fligthPrice;
    }
    public void setFligthPrice(String fligthPrice) {
    this.fligthPrice = fligthPrice;
    }
    public int getCorporationId() {
    return corporationId;
    }
    public void setCorporationId(int corporationId) {
    this.corporationId = corporationId;
    }
    public String getCorporationName() {
    return corporationName;
    }
    public void setCorporationName(String corporationName) {
    this.corporationName = corporationName;
    }
    }DAOpackage org.dao;import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;import org.bean.FligthInfo;
    import org.util.ConDB;public class FligthDao {
    //查
    public ArrayList findAllFligth(){
    ArrayList fligthList =new ArrayList();
    Connection con=ConDB.getConnection();
    String sql="select * from T_flight a,T_corporation b where a.corporation_Id=b.corporation_Id";
    System.out.println(sql);
    Statement stm=null;
    ResultSet rs=null;
    try {
    stm=con.createStatement();
    rs=stm.executeQuery(sql);
    while(rs.next()){
    FligthInfo fligth=new FligthInfo();
    fligth.setCorporationId(rs.getInt("corporation_Id"));
    fligth.setCorporationName(rs.getString("corporation_name"));
    fligth.setFlightId(rs.getInt("flight_Id"));
    fligth.setFligthNo(rs.getString("flight_no"));
    fligth.setFligthPrice(rs.getString("price"));
    fligth.setFligthTime(rs.getString("flight_time"));
    fligthList.add(fligth);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConDB.closeRs(rs);
    ConDB.closeStm(stm);
    ConDB.closeCon(con);
    }
    return fligthList;
    }
    //增
    public int insertFligth(FligthInfo fligth){
    int flag=-1;
    Connection con=ConDB.getConnection();
    String sql="insert into T_flight values('"+fligth.getFligthNo()+"','"+fligth.getFligthTime()+"',"+fligth.getFligthPrice()+","+fligth.getCorporationId()+")";
    System.out.println(sql);
    Statement stm=null;
    try {
    stm=con.createStatement();
    flag=stm.executeUpdate(sql);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    ConDB.closeStm(stm);
    ConDB.closeCon(con);
    }

    return flag;
    }
    servlet  添加数据package org.service;import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.bean.FligthInfo;
    import org.dao.FligthDao;public class InsertFligth extends HttpServlet { /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { FligthInfo fligth=new FligthInfo();
    fligth.setFligthNo(request.getParameter("fligthNo"));
    fligth.setFligthPrice(request.getParameter("fligthPrice"));
    fligth.setCorporationId(Integer.parseInt(request.getParameter("corporationId")));
    fligth.setFligthTime(request.getParameter("fligthTime"));
    FligthDao fDao=new FligthDao();
    fDao.insertFligth(fligth);
    request.getRequestDispatcher("/servlet/FindAllFligth").forward(request, response);
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { this.doGet(request, response);
    }}给你一个没用任何框架的添加数据的例子    你这增删改查  代码太多 不好讲
      

  3.   

    jsp页面<form action="<%=path %>/servlet/InsertFligth" method="post">
         <table>
         <tr>
         <td>飞机编号</td>
    <td><input type="text" name="fligthNo"></td>
    </tr>
         <tr>
         <td>时间</td>
         <td><input type="text" name="fligthTime"></td>
         </tr>
         <tr>
         <td>价格</td>
         <td><input type="text" name="fligthPrice"></td>
         </tr>
         <tr>
         <td>所属公司</td>
         <td><select name="corporationId">
         <c:forEach var="fligth" items="${FligthList}">
         <option value="${fligth.corporationId }">${fligth.corporationName }</option>
         </c:forEach>
         </select>
         </td>
         </tr>
         <tr>
         <td><input type="submit" value="添加信息"></td>
         <td><input type="reset" value="取消"></td>
         </tr>
         </table>
         </form>
      

  4.   

    不用ORM框架做的话确实比较麻烦,就需要写N条SQL,先删除外键关联,再删除本身,在执行此操作时一定要绑定事务来管理。如果用了ORM框架的话,一切就要简单的多,设置级联关系就可!
      

  5.   

    还是自己写好一些吧,虽然要写好多的SQL。
      

  6.   

     A,B,C 这三个表是有关联的因此才操作A表时,B、C表也可能发生变动因此在对表A进行增删改操作时,建议使用存储过程来处理事务,同时修改B、C表。