import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class OrderStatusServlet extends HttpServlet {
//mysql的url
static final String dbURL ="jdbc:mysql://localhost/j2mebook?" +
"user=j2meapps&password=bigsecret";
//access的url
static final String aURL = "jdbc
dbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\Data Import\\j2mebook.mdb";
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
Connection conn = null, conn1 = null;
Statement stmt = null;
String query = null;
String nextJSP = null;
try {
Class.forName("org.gjt.mm.mysql.Driver"
;
}
catch (ClassNotFoundException e) {
throw new ServletException("Unable to load JDBC driver"
;
}
try {
String orderNumber = request.getParameter("orderNumber"
;
conn = DriverManager.getConnection(dbURL);
if(conn == null){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
;
}catch(ClassNotFoundException e){
throw new ServletException("Unable to load JDBC driver"
;
}
try{
conn1 = DriverManager.getConnection(aURL, "", ""
;
stmt = conn1.createStatement();
query = "select status, dateplaced, shipdate from orders where ordernumber = '"+orderNumber+"'";
}
catch(Exception e){}
}else{
stmt = conn.createStatement();
query = "select status, dateplaced, shipdate from orders where ordernumber = '"+orderNumber+"'";
}
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
request.setAttribute("orderNumber", orderNumber);
request.setAttribute ("status", rs.getString(1));
request.setAttribute ("datePlaced", rs.getString(2));
String shipDate = rs.getString(3);
if (shipDate == null) {
shipDate = "***";
}
request.setAttribute ("shipDate", shipDate);
nextJSP = "/ReportOrderStatus.jsp";
}
else {
nextJSP = "/OrderNotFound.jsp";
}
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
RequestDispatcher rd = context.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}catch(Exception e){}
finally {
if (conn != null) {
try {
conn.close();
}
catch (SQLException e) {
throw new ServletException("connection close failed"
;
}
}
if (conn1 != null) {
try {
conn1.close();
}
catch (SQLException e) {
throw new ServletException("connection close failed"
;
}
}
}
}
}
|