The Artima Developer Community
Sponsored Link

Java Answers Forum
How i put an applet on the web, which is communicate with a database

4 replies on 1 page. Most recent reply: Sep 24, 2004 12:32 AM by Periklis

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Periklis

Posts: 5
Nickname: panpol2
Registered: Sep, 2004

How i put an applet on the web, which is communicate with a database Posted: Sep 23, 2004 2:50 AM
Reply to this message Reply
Advertisement
I create an applet, which is communicate with a database (MS Access), but is work only when I run the applet
with the appletviewer and with a policy file. For example my applet name is ConnectTo.java, I execute the applet like this:

Javac ConnectTo.java
appletviewer -J-Djava.security.policy=Policy.txt COnnectTo.html
Now, I want to put that applet on a web server with the database, and run the applet from html file and store the information in the database on the server
How I do this???Run the applet from an html file and works?Where I put the ODBC drivers, so the applet can see my database?
Can anyone help me please?
Thanks


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
Reply to this message Reply
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

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:40 AM
Reply to this message Reply
oh yea and in the applet u need to write this.
I passed all the info back into vectors, u can do whatever u want.


String args = "whereEverTheServLetAddressIS?make=" + makeSearch + "&model=" + modelSearch + "&minprice=" + minSearch + "&maxprice=" + maxSearch;

//Creating the new URL using the String above

URL searchDatabaseUrl = new URL(args);

/*A stream is opened to the servlet to pass the variables to the servlet and
*retrieve objects back.
*/

InputStream in = (searchDatabaseUrl.openStream());

//Retrieving the objects from the servlet which are then put into vectors.

ObjectInputStream results = new ObjectInputStream(in);

Object objList = results.readObject();
listString = (Vector)objList;

Object objDescription = results.readObject();
textDescription = (Vector)objDescription;

Object objAudio = results.readObject();
playAudio = (Vector)objAudio;

Object objImage = results.readObject();
addImage = (Vector)objImage;

Object objVideo = results.readObject();
playVideo = (Vector)objVideo;

Periklis

Posts: 5
Nickname: panpol2
Registered: Sep, 2004

Thanks ricky Posted: Sep 24, 2004 12:14 AM
Reply to this message Reply
Thanks ricky

Periklis

Posts: 5
Nickname: panpol2
Registered: Sep, 2004

Re: How i put an applet on the web, which is communicate with a database Posted: Sep 24, 2004 12:32 AM
Reply to this message Reply
I will try it, and I will tell you if it works!!
Where exactly is the connection between the applet and the servlet in the code you send me??

Thanks again.. :) !!!!!

Flat View: This topic has 4 replies on 1 page
Topic: How can i connect an applet with a database???? Previous Topic   Next Topic Topic: decode java compiled files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use