This Text file is old! In a 🏛️Museum, an unsorted archive of (user-)pages. (Saved from Geocities in Oct-2009. The archival story: oocities.org)
--------------------------------------- (To 🚫report any bad content: archivehelp @ gmail.com)
/* CarSearch.java * * * Function: This is a servlet program which receives parameters sent from the client and * make a SQL query then send to the database inventory, get data from it, and then use the * data sent by database to make a HTML file and send it to the client side for display * * */ package hall; import java.io.*; import java.net.*; import java.sql.*; import java.util.*; import java.lang.*; import javax.servlet.*; import javax.servlet.http.*; /** *CarSearch */ public class CarSearch extends HttpServlet { public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver"; public static final String URL = "jdbc:odbc:car"; //override doGet method to get request from client and send response to it public void doGet( HttpServletRequest request, HttpServletResponse response) //catch all the exceptions throws ServletException, IOException { response.setContentType("text/html"); //set output type PrintWriter out = response.getWriter(); String make = request.getParameter("make"); String transtype = request.getParameter("transtype"); String minprice_temp = request.getParameter("minprice"); String maxprice_temp = request.getParameter("maxprice"); String minyear_temp= request.getParameter("minyear"); String maxyear_temp= request.getParameter("maxyear"); int minprice = Integer.parseInt(minprice_temp); int maxprice = Integer.parseInt(maxprice_temp); int minyear = Integer.parseInt(minyear_temp); int maxyear = Integer.parseInt(maxyear_temp); //Display the result set out.println("Search Result"); out.println(""+"
Search Result


"); out.println("
  • make: "+ make + "\n"+"
  • Type:"+transtype+"\n" ); out.println("
  • minprice: "+ minprice_temp + "\n"+"
  • maxprice:"+maxprice_temp+"\n" ); out.println("
  • minyear:"+ minyear_temp+"\n" +"
  • maxyear :"+maxyear_temp+"\n"); out.println(""); out.println(""); out.println(""); out.println(""+ ""+ ""+ "
    "+ "\"\""+ "
    Type
    "+ "Make
    "+ "Price
    "+ "Year
    "); out.println(""+ ""+ "
    "+ ""+ "
    "); String anytype=new String("any"); String anymake=new String("any"); //JDBC database connection Connection con = null; try { //Load the driver Class.forName(DRIVER); //Connect to the database con = DriverManager.getConnection(URL,"",""); //Query the database for the list of different cars Statement stmt = con.createStatement(); String sqltemp; if(!transtype.equals(anytype)) { if(!make.equals(anymake)) { sqltemp = "SELECT * FROM carlist" + " WHERE make='" + make +"' "; sqltemp = sqltemp +"and transtype='"+ transtype + "'" ; sqltemp = sqltemp +"and (price between " + minprice + " and " + maxprice + ")"; sqltemp = sqltemp +"and (year between " + minyear + " and " + maxyear + ")"; } else { sqltemp = "SELECT * FROM carlist" + " WHERE transtype='"+transtype +"'"; sqltemp = sqltemp +"and (price between " + minprice + " and " + maxprice + ")"; sqltemp = sqltemp +"and (year between " + minyear + " and " + maxyear + ")"; } } else { if(!make.equals(anymake)) { sqltemp = "SELECT * FROM carlist" + " WHERE make='" + make +"' "; sqltemp = sqltemp +"and (price between " + minprice + " and " + maxprice + ")"; sqltemp = sqltemp +"and (year between " + minyear + " and " + maxyear + ")"; } else { sqltemp = "SELECT * FROM carlist WHERE"; sqltemp = sqltemp +" (price between " + minprice + " and " + maxprice + ")"; sqltemp = sqltemp +"and (year between " + minyear + " and " + maxyear + ")"; } } //get the ResultSet from the database ResultSet rs = stmt.executeQuery(sqltemp); int i = 0; while (rs.next()) { out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
    " + rs.getString("transtype") + "" + rs.getString("make") + "$" + rs.getString("price") + "" + rs.getString("year") + "
    "); out.println(""); out.println("
    "); i++; out.println(""); } out.println(""+i+" results found"); out.println(""); out.println(""); out.flush(); out.close(); } catch(ClassNotFoundException e) { out.println("Couldn't load database driver: " + e.getMessage()); } catch(SQLException e) { out.println("SQLException caught: " + e.getMessage()); } finally { //Always close the database connection. try { if (con != null) con.close(); } catch (SQLException ignored) { } } } }

    Text file Source (historic): geocities.com/wenxiazheng


    (to report bad content: archivehelp @ gmail)