package com.ynu.sg;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BestQueryServlet extends HttpServlet
{
//only set in the init() method, so concurrency
//issues should be fine.
private LavenderDBSingleton m_dbsingleton = null;
public void init()
{
/*
* This will instantiate it within the Servlet's
* virtual machine if it hasn't already. If it
* has, we have the instance of it.
*/
m_dbsingleton = LavenderDBSingleton.getInstance();
}
/**
* simply forwards all to doPost()
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doPost(request,response);
}
/**
* The main form!
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
PrintWriter out = response.getWriter();
out.println("<TITLE>Internal Inventory Check</TITLE>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>Lavender Fields Farm Internal Inventory</H1>");
//show the date.
SimpleDateFormat sdf =new SimpleDateFormat ("EEE, MMM d, yyyy h:mm a");
java.util.Date newdate = new java.util.Date(Calendar.getInstance().getTime().getTime());
String datestring = sdf.format(newdate);
out.println("<H3>Inventory as of: " + datestring + "</H3>");
out.println("<TABLE BORDER=1>");
out.println("<TR><TD BGCOLOR='yellow'>" +
"<B><CENTER>Name</CENTER></B></TD>" +
"<TD BGCOLOR='yellow'><B>" +
"<CENTER>Description</CENTER></B></TD>" +
"<TD BGCOLOR='yellow'><B>" +
"<CENTER>Inventory Amount</CENTER></B></TD></TR>");
//Load the inventory from the database.
try
{
Connection con = m_dbsingleton.getConnection();
if (con == null)
{
out.println("<B>There are currently database problems. " +
"Please see your administrator for details.</B>");
return;
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Inventory");
while (rs.next())
{
String amtString = "";
int amt = rs.getInt("Amount");
if (amt < 50)
amtString ="<TD><CENTER><FONT COLOR='RED'>" +
amt + "</FONT></CENTER></TD>";
else
amtString ="<TD><CENTER>" +
amt + "</CENTER></TD>";
out.println("<TR><TD><CENTER>" + rs.getString("Name") +
"</CENTER></TD><TD><CENTER>" +
rs.getString("Description") +
"</CENTER></TD>" + amtString + "</TR>");
}
rs.close();
out.println("</TABLE><HR>Items in <FONT COLOR='red'>RED</FONT>" +
" denote a possible low inventory. Click Here to " +
" contact <A HREF='mailto:mgmt@localhost'>" +
"MANAGEMENT</A> to order more supplies.");
//Free the connection!
m_dbsingleton.freeConnection( con );
}
catch (Exception e)
{
out.println("There were errors connecting to the database." +
" Please see your systems administrator for details.");
e.printStackTrace();
}
}
}
DDConnectionBroker数据库连接池运用实例
4星 · 超过85%的资源 需积分: 9 127 浏览量
2012-11-14
10:18:58
上传
评论
收藏 479KB RAR 举报
憨憨的宏
- 粉丝: 0
- 资源: 1