如: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语句也不知道怎么写?会者帮忙,谢谢!
本人设计时考虑时,遇到的第一个问题:
a1为A的ID,a2为其名字,b1、c1为B、C的ID,b2,c2为名字,界面设计时把b1,c1字段都设计为下拉框,但现在我遇到的表B中字段特别多,如果都设计为下拉框,半个屏幕都成了下拉框,以及后台操作用的sql语句也不知道怎么写?会者帮忙,谢谢!
如果没有用hibernate 就需要 写n条sql语句了,先删除子表,然后删除主表内容,不过这个要注意使用事务去处理。
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);
}}给你一个没用任何框架的添加数据的例子 你这增删改查 代码太多 不好讲
<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>