📄 manage.java
字号:
* 插入订单。
* @return 返回插入订单。
*/
public int insertOrder() throws Exception
{
if(m_order.getIndate().equals(""))
throw new Exception("入住日期不能为空。");
else if(m_order.getOutdate().equals(""))
throw new Exception("离店日期不能为空。");
else if(m_order.getBooker().equals(""))
throw new Exception("入住登记人不能为空。");
else if(m_order.getNumber().equals(""))
throw new Exception("入住人数不能为空。");
else if(m_order.getCertificate().equals(""))
throw new Exception("所持证件不能为空。");
else if(m_order.getCertificatecode().equals(""))
throw new Exception("证件号码不能为空。");
else if(m_order.getLinkman().equals(""))
throw new Exception("联系人不能为空。");
else if(m_order.getTel().equals(""))
throw new Exception("电话不能为空。");
else if(m_order.getFax().equals(""))
throw new Exception("传真不能为空。");
int new_orderid = 0;
String sql = new String();
sql+="insert into tt_order (hotelid, indate, outdate, booker, certificate, certificatecode, number, receive, ";
sql+="howby, earliestarrive, latestarrive, linkman, company, tel, fax, handset, memo) values (";
sql+="'"+Integer.toString(m_order.getHotelid())+"','"+m_order.getIndate()+"','"+m_order.getOutdate()+"','";
sql+=m_order.getBooker()+"','"+m_order.getCertificate()+"','"+m_order.getCertificatecode()+"','";
sql+=m_order.getNumber()+"','"+m_order.getReceive()+"','"+m_order.getHowby()+"','"+m_order.getEarliestarrive()+"','";
sql+=m_order.getLatestarrive()+"','"+m_order.getLinkman()+"','"+m_order.getCompany()+"','"+m_order.getTel()+"','";
sql+=m_order.getFax()+"','"+m_order.getHandset()+"','"+m_order.getMemo()+"')";
new_orderid = insertData(sql,"tt_order");
for(int i=0;i<m_order.getRoomid().length;i++)
{
/*sql="insert into tt_roomlist (orderid, roomid, rooms, breakfast) ";
sql+="select max(orderid), "+Integer.toString(m_order.getRoomid()[i])+","+Integer.toString(m_order.getRooms()[i])+",";
sql+=Integer.toString(m_order.getBreakfast()[i])+" from tt_order";*/
sql="insert into tt_roomlist (orderid, roomid, rooms, breakfast) values (";
sql+=Integer.toString(new_orderid)+","+Integer.toString(m_order.getRoomid()[i])+","+Integer.toString(m_order.getRooms()[i])+",";
sql+=Integer.toString(m_order.getBreakfast()[i])+")";
insertData(sql);
}
return new_orderid;
}
/**
* 浏览订单信息。只包括tt_order表中的字段orderid, indate, outdate, number, linkman, tt_order.tel, fax, handset, howby, receive 及tt_hotel表中cname字段。
* @return 返回一个字符串。生成HTML表格。
*/
public String browseOrder()
{
String retnStr = new String();
Connection cn = null;
Statement stm = null;
ResultSet rst = null;
int recordCount = 0;
try
{
cn=db.getMySQL(mysql_usr,mysql_psw);
stm=cn.createStatement();
String sql = new String();
sql="select count(*) from tt_hotel, tt_order where tt_hotel.hotelid=tt_order.hotelid";
rst=stm.executeQuery(sql);
if(rst.next())
recordCount=rst.getInt(1);
rst.close();
sql="select orderid, tt_hotel.cname, indate, outdate, number, tt_order.linkman, tt_order.tel, tt_order.fax, handset, howby, receive, date_format(orderdate,'%Y:%m:%d %H:%i:%S') od ";
sql+="from tt_hotel, tt_order where tt_hotel.hotelid=tt_order.hotelid "+(m_browseorder_filter.equals("")?"":(" and "+m_browseorder_filter))+" order by orderid desc ";
sql+="limit "+Integer.toString((m_curPage-1)*m_pageSize)+","+Integer.toString((m_curPage*m_pageSize>recordCount)?recordCount-(m_curPage-1)*m_pageSize:m_pageSize);
rst=stm.executeQuery(sql);
int i = 0;//
while(rst.next())
{
if(i++%2!=0)
retnStr+="<tr bgcolor=#efefee>\n";
else
retnStr+="<tr>\n";
retnStr+="<td><a href=\"browseorderdetails.jsp?oId="+rst.getString("orderid")+"\">"+rst.getString("cname")+"</a></td>\n";
retnStr+="<td align=center>"+rst.getString("indate")+"</td>\n";
retnStr+="<td align=center>"+rst.getString("outdate")+"</td>\n";
retnStr+="<td align=center>"+rst.getString("number")+"</td>\n";
retnStr+="<td align=center>"+rst.getString("linkman")+"</td>\n";
retnStr+="<td align=center>"+rst.getString("tel")+"</td>\n";
retnStr+="<td align=center>"+rst.getString("fax")+"</td>\n";
//retnStr+="<td align=center>"+rst.getString("handset")+"</td>\n";
retnStr+="<td align=center>"+rst.getString("howby")+"</td>\n";
retnStr+="<td align=center>"+rst.getString("od")+"</td>\n";
retnStr+="<td align=center><a href=\"deleteOrder.jsp?oId="+rst.getString("orderid")+"\">删除</a></td>\n";
retnStr+="</tr>\n";
}
rst.close();
}
catch(Exception e)
{
Error(e);
}
finally
{
db.CloseConnection(cn);
db.CloseStatement(stm);
}
retnStr+="<tr align=center><td colspan=11>"+splitPage(recordCount,m_pageSize,m_curPage)+"</td></tr>\n";
return (retnStr.equals("")?getBlankRow(11):retnStr);
}
/**
* 发送电子邮件
* @param to 收件人地址。
* @param sub 主题。
* @param body 内容。
* @return 返回一个布尔值。发送成功返回true,否则返回false。
*/
public boolean SendMail(String to,String sub,String body)
{
boolean isSuccess = false;
String _Port = smtp_port;
String _Host = smtp_host;
String _Protocol = "smtp";
String _UserName = "aaa";
String _Password = "aaa";
String _From = "send_order@abc.com";
Session session = null; //javax.mail.Session
Message msg;
Transport tp = null;
java.util.Properties pe=System.getProperties();
pe.put("mail.transport.protocol","smtp");
pe.put("mail.smtp.port",_Port);
pe.put("mail.smtp.host",_Host);
try
{
session=javax.mail.Session.getInstance(pe,null);
tp=session.getTransport(_Protocol);
tp.connect(_Host,(new Integer(_Port)).intValue(),_UserName,_Password);
//write mail
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_From));
InternetAddress[] tos=InternetAddress.parse(to);
msg.setRecipients(Message.RecipientType.TO,tos);
msg.setSubject(sub);
//msg.setText(body); //only send plain text
msg.setContent(body,"text/html");
//send mail.
tp.send(msg);
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
Error(e);
}
finally
{
return isSuccess;
}
}
/**
* 发送订单通知邮件。
* @param hotelid 酒店标识。
* @param orderid 订单标识。
* @throws Exception
*/
public void sendOrderMail(int hotelid,int orderid) throws Exception
{
String body = new String();
body = "<a href=\""+ mail_order_url + "?oId="+orderid+"\">" + getHotelName(hotelid) + " --- 订单</a>";
if (!SendMail(mail_to,mail_sub,body))
throw new Exception("发送订单通知时出现错误,但订单内容已发出!");
}
/**
* 读取酒店图片的URL。
* @return 返回一个字符串。酒店图片的URL。
*/
public String getPicURL()
{
return pic_url;
}
//private:
//
private String transBreakfast(int breakfast)
{
if (breakfast==0)
return "不含早餐";
else if (breakfast==1)
return "中式早餐";
else if (breakfast==2)
return "西式早餐";
else
return "未定";
}
/**
* 分页
* @param recordCount
* @param pageSize
* @param curPage
* @return
*/
private String splitPage(int recordCount,int pageSize,int curPage)
{
int pageCount;
String firstPageUrl,lastPageUrl,prePageUrl,nextPageUrl;
String pageStatus = new String();
pageCount=(recordCount%pageSize>0)?(recordCount/pageSize+1):(recordCount/pageSize);
pageStatus="第"+Integer.toString(curPage)+"页,共"+Integer.toString(pageCount)+"页";
if(curPage==1) //当前就是第一页,则第一页和前一页无连接
{
firstPageUrl="第一页";
prePageUrl="上一页";
}
else
{
firstPageUrl="<a href=\"Javascript:showPage(1)\">第一页</a>";
prePageUrl="<a href=\"Javascript:showPage("+Integer.toString(curPage-1)+")\">上一页</a>";
}
if(curPage==pageCount) //当前就是最后一页,则最后一页和后一页无连接
{
lastPageUrl="最后一页";
nextPageUrl="下一页";
}
else
{
lastPageUrl="<a href=\"Javascript:showPage("+Integer.toString(pageCount)+")\">最后一页</a>";
nextPageUrl="<a href=\"Javascript:showPage("+Integer.toString(curPage+1)+")\">下一页</a>";
}
return pageStatus+" "+firstPageUrl+" "+prePageUrl+" "+nextPageUrl+" "+lastPageUrl;
}
/**
* 生成一个空行
* @param cols
* @return
*/
private String getBlankRow(int cols)
{
return ("<tr><td colspan="+Integer.toString(cols)+"> </td></tr>\n");
}
/**
* 生成一个表格
* @param rst
* @return
*/
private String generateTable(ResultSet rst)
{
String retnStr = new String();
try
{
ResultSetMetaData rstmd = rst.getMetaData();
int cols = rstmd.getColumnCount(); //返回记录集包含的列数
int curRow = 0; //记录当前行号
while(rst.next())
{
if(curRow%2!=0)
retnStr+="<tr bgcolor=\"#eeefee\">\n";
else
retnStr+="<tr>\n";
for(int i=1;i<=cols;i++)
{
retnStr+="<td>"+rst.getString(i)+"</td>\n";
}
}
rst.close();
}
catch(Exception e)
{
Error(e);
}
return retnStr;
}
//处理数据
private void transact(String sql)
{
Connection cn = null;
Statement stm = null;
try
{
cn=db.getMySQL(mysql_usr,mysql_psw);
stm=cn.createStatement();
stm.executeUpdate(sql); //插入新记录
}
catch(Exception e)
{
Error(e);
}
finally
{
db.CloseConnection(cn);
db.CloseStatement(stm);
}
}
//增加记录
private void insertData(String sql)
{
transact(sql);
}
//for insert order - get orderid
private int insertData(String sql,String table_name)
{
int retnInt = 0;
Connection cn = null;
Statement stm = null;
ResultSet rst = null;
try
{
cn=db.getMySQL(mysql_usr,mysql_psw);
stm=cn.createStatement();
stm.executeUpdate(sql);
rst=stm.executeQuery("select last_insert_id() from " + table_name);
if(rst.next())
retnInt = rst.getInt(1);
rst.close();
}
catch(Exception e)
{
Error(e);
}
finally
{
db.CloseConnection(cn);
db.CloseStatement(stm);
}
return retnInt;
}
//删除记录
private void deleteData(String sql)
{
transact(sql);
}
//更新记录
private void updateData(String sql)
{
transact(sql);
}
//显示错误信息
private void Error(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -