ricky
Posts: 8
Nickname: ricky30000
Registered: Jul, 2003
|
|
Re: How i put an applet on the web, which is communicate with a database
|
Posted: Sep 23, 2004 10:38 AM
|
|
You need to use a three tier architecture. Like an applet connecting to a servlet, which then connects to the database and parses it back through the servlet.
The Search Servlet
/* This servlet retrieves the parameters from the applet in order to carry out the search. The variables * make, model, min Price and max price are put into a SQL string. When the query is executed and put * into a ResultSet, and all the information that is needed like description, audio URL, image URL and * video URL. The information is put into vectors and then are written out to the applet. */
import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*; import java.io.Serializable; import java.applet.*; import javax.swing.*; import java.awt.*;
public class searchDatabaseServlet extends HttpServlet {
private ResultSet resCars; private Vector textDescription, listString, playAudio, addImage, playVideo;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//declaration of variables which are initialized to null
resCars = null; Connection con = null; Statement stmt = null; String searchString = null; String makeSearch = null; String modelSearch = null; String minSearch = null; String maxSearch = null;
try { //load the Sun driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//get a connection to the database con =DriverManager.getConnection("jdbc:odbc:searchengine");
//create a statement object stmt = con.createStatement();
//request the parameters from the applet
makeSearch = req.getParameter("make"); modelSearch = req.getParameter("model"); minSearch = req.getParameter("minprice"); maxSearch = req.getParameter("maxprice");
//The search SQL query
searchString = "SELECT * FROM cars WHERE make = '" + makeSearch + "' AND model = '" + modelSearch + "' AND price >= " + minSearch + " AND price <= " + maxSearch + "";
//A different SQL query is used if the model parameter is equal to the String Any
if(modelSearch.equals("Any")) { searchString = "SELECT * FROM cars WHERE make = '" + makeSearch + "' AND model AND price >= " + minSearch + " AND price <= " + maxSearch + ""; }
//execute an SQL query, get a ReultSet
resCars = stmt.executeQuery(searchString); //create all the vectors
textDescription = new Vector(); listString = new Vector(); textDescription = new Vector(); playAudio = new Vector(); addImage = new Vector(); playVideo = new Vector();
/*Search through the ResultSet and retrieve the strings from the database that is *needed. */
while(resCars.next()) { listString.addElement("Car: " + resCars.getString(1) + " Model: " + resCars.getString(2) + " Registration Year: " + resCars.getString(6) + " Price: " + resCars.getString(3)); textDescription.addElement(resCars.getString(5)); playAudio.addElement(resCars.getString(7)); addImage.addElement(resCars.getString(8)); playVideo.addElement(resCars.getString(9)); }
//Create an ObjectOutputStream to write out all the vectors to the applet.
ObjectOutputStream outPut = new ObjectOutputStream(res.getOutputStream()); outPut.flush(); outPut.writeObject(listString); outPut.flush(); outPut.writeObject(textDescription); outPut.flush(); outPut.writeObject(playAudio); outPut.flush(); outPut.writeObject(addImage); outPut.flush(); outPut.writeObject(playVideo); outPut.close(); } catch(ClassNotFoundException e) { System.out.println("Couldn't load database driver: " + e.getMessage()); } catch(SQLException e) { System.out.println("SQLException caught: " + e.getMessage()); } finally { //always close the database connection try { if (con != null) con.close(); } catch (SQLException ignored) { } } }//end of doGet }//end of Http Servlet
|
|