JAVA实现随机生成航班数据
实现效果:通过java实现随机生成航班数据并保存到数据库
页面请求调用
先获取要生成数据的月份等参数,然后进行请求,对返回结果进行回填并不断递归调用。
function sc() {
//获取参数
var sd = Number($("#shudu").val()) - Number($("#shudu").val())/2;
var yue = $("#yue").val();
if (riqi == "" || riqi=="0") {riqi = 0;}else{
if(Number(riqi)<9){riqi="0"+riqi;}
}
//请求servlet
$.getJSON("<%=path %>/servlet/PNRReServlet?fun=schxsj&yue=" + yue+ "&hbrq=" + riqi,
function (data) {//返回数据处理
if (data != null) {
var cg = $("#cg").text();
var cgsj = Number(cg) + 1;
$("#cg").text(cgsj);
$("#sjid").text(data.hanbanID);
$("#hbh").text(data.hanbanname);
$("#diqu").text(data.jixintype);
$("#canwei").text(data.shifadi);
$("#jiage").text(data.mudidi);
$("#qfsj").text(data.begintime);
$("#zwsn").text(data.endtime);
$("#brq").text(data.riqitime);
} else {
var sb = $("#sb").text();
var sbsj = Number(sb) + 1;
$("#sb").text(sbsj);
}
var sd = $("#shudu").val();
setTimeout(function () {//递归调用
if ($("#xh").text() == "0") {
return;
}
sc();
}, sd);
});
}
Servlet 对请求进行处理
Servlet 处理发送过来的请求,并进行返回处理。
public void schxsj(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//获取传过来的参数
String dqyf =request.getParameter("yue");
String jtrq = request.getParameter("hbrq");
//调用Service的方法进行操作
PNRydServlet pnrService = new PNRydServletImpl();
Jc_hanbanPo hxsa=pnrService.Randomhb(dqyf, jtrq);
//返回json
JSONObject jsonObject=JSONObject.fromObject(hxsa);
out.write(jsonObject.toString());
out.close();
}
进行数据库操作
进行最终的数据随机生成及保存
//随机生成航班
private static String xinzhnab="INSERT into jc_hanban(hanbanname,jixintype,shifadi,mudidi,begintime,endtime,riqitime) values(?,?,?,?,?,?,?)";
@Override
public Jc_hanbanPo Randomhb(String yue, String rqda) {
Jc_hanbanPo hbsj=null;
String[] fjname = new String[]{"波音767","波音747","波音777","波音757","波音737"};
String[] citydi=new String[]{"北京","杭州","长沙","南京","深圳","成都","广州"};
try {
String shifadi=citydi[scnumber(7)];
String mudidi=citydi[scnumber(7)];
//判断始发地与目的是否冲突
if(shifadi==mudidi){return null;}
String riqitime=yue;
if(rqda==null || rqda=="" || rqda.equals("0")){
Random random = new Random();
String i2 = addnin(random.nextInt(30));
riqitime=i2+riqitime;
}else{
riqitime=rqda+riqitime;
}
con=DbUtil.getConnection();
String hanbanname="MU"+scnumber(10)+scnumber(10)+scnumber(10)+scnumber(10)+scnumber(10);
String jixintype=fjname[scnumber(5)];
//判断当前航班机型是否存在
ps=con.prepareStatement("select * from jc_hanban where jc_hanban.shifadi='"+shifadi+"' and jc_hanban.mudidi= '"+mudidi+"' and jc_hanban.jixintype='" + jixintype + "' and jc_hanban.riqitime= '"+ riqitime +"'");
rs=ps.executeQuery();
int hbjx=0;
while (rs.next()) {hbjx++;}
if(hbjx>0){System.out.println("航班机型已存在");return null;}
//判断航班号是否存在
ps=con.prepareStatement("select * from jc_hanban where jc_hanban.hanbanname='"+hanbanname+"'");
rs=ps.executeQuery();
int hanbhao=0;
while (rs.next()) {hanbhao++;}
if(hanbhao>0){System.out.println("航班号已存在");return null;}
//判断当天航班总量是否已满
ps=con.prepareStatement("select * from jc_hanban where jc_hanban.shifadi='"+shifadi+"' and jc_hanban.mudidi='"+mudidi+ "' and jc_hanban.riqitime='"+ riqitime+"'");
rs=ps.executeQuery();
int hbzn=0;
while (rs.next()) {hbzn++;}
if(hbzn>=3){System.out.println("航班总量已满");return null;}
int shiz=scnumber(22);int fenz=scnumber(61);
String begintime=addnin(shiz)+":"+addnin(fenz);
String endtime=addnin(shiz+2)+":"+addnin(fenz);
ps=con.prepareStatement(xinzhnab,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, hanbanname);
ps.setString(2, jixintype);
ps.setString(3, shifadi);
ps.setString(4, mudidi);
ps.setString(5, begintime);
ps.setString(6, endtime);
ps.setString(7, riqitime);
int a=0;
int key = ps.executeUpdate();
ResultSet rs=ps.getGeneratedKeys();
while(rs.next()){a=rs.getInt(1);}
if(key>0){
hbsj=new Jc_hanbanPo();
hbsj.setbegintime(begintime);
hbsj.setendtime(endtime);
hbsj.sethanbanname(hanbanname);
hbsj.setjixintype(jixintype);
hbsj.setmudidi(mudidi);
hbsj.setriqitime(riqitime);
hbsj.setshifadi(shifadi);
hbsj.sethanbanID(a);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.close(con, ps, rs);
}
return hbsj;
}