Save Java Applet Variable to Mysql

Hi I've done a lot of research on the best way to communicate between a java applet and MySql Database. I have a tune player which I have students logging onto, it's a java applet with a speed slider. I want to save the speed that they play each tune at so it goes back to the same speed the next time they open that tune. It seems I have a number of options, none of which seem very neat.

  1. Use a javascript function to periodically check the speed and save it to a cookie, then each page of the site would have to check cookies relationg to each tune.
  2. Make each link on the page call a javascript function to check the speed variable in the applet and add it to a perameter in the url then redirect so the next php page can save the speed to a database. This way when the user navigates away the speed will be saved, but this won;t work if the back button is used.

Is there a better way of doing this?


No need to use JavaScript.

The java code below posts variables to a PHP script on the web server then shows the server response on the console

private void post()
  throws MalformedURLException, IOException
{ URL url;
  URLConnection con;
  OutputStream oStream;
  String parametersAsString;
  byte[] parameterAsBytes;
  String aLine; // only if reading response

  parametersAsString = "msg=hi&to=memo";
  parameterAsBytes = parametersAsString.getBytes();
  // send parameters to server
  url = this.getCodeBase();
  url = new URL(url + "scriptfile.php");
  con = url.openConnection();
// setDoInput(true); // only if reading response
  con.setRequestProperty("Content=length", String.valueOf(parameterAsBytes.length));
  oStream = con.getOutputStream();

 // read response from server & show the server response on the Java console or whatever ...
  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  aLine = in.readLine();
  while (aLine != null)
  { System.out.println(line);
    aLine = in.readLine();


Use the JNLP API and the problems should be solved.

Since Java 1.6.0_10+, it is possible to use the Java Web Start API services (JNLP API) within an embedded applet. The JNLP API provides the PersistenceService. Here is a small demo. of the PersistenceService.

If the user hits the back button (or otherwise leaves the page), the destroy() method will be called. Override the destroy method and persist the data at that time.

I'd suggest you get the applet to update the database. Whenever the speed slider changes you can fire off an update to the database, or you might need to coalesce multiple changes into one request depending on usage.

When the applet changes tune it can also do its own lookup of the correct speed.

Note that the applet will probably not be able to hit the database directly - browsers should restrict what I/O operations are available to applets - but you should be able to get the applet to hit a URL on the server that will actually perform the update. Signing the applet may let you hit the database but you should read up on the applet security model and the various browser quirks first.

It's not really clear how all of this is set up since you don't have a lot of details. However, assuming that you have an actual Java applet, I'd say the following:

  1. If the Java applet requires a login (that is, it knows who the user is) then it can store the preference on the server. To do this you could have the applet connect to the server using JDBC, which isn't generally a good idea for internet-facing applets, or you could have the applet send a message to a server process such as a web server. That process connects to the mysql server.
  2. The applet can communicate directly with the browser using Javascript. So you can have the applet set the cookie when the slider changes, instead of having the Javascript set it.

Need Your Help

I need to put a text on top of an image without CSS or JS

html layout browser cross-browser

Is there a way I can place the text <p> on top of the image? without using css or javascript. just plain html. The reason being is this technique or approach will be use on older phones that ...

How to change CSS property before element is created?

javascript html css

I have to modify the div's css before the window.onload, and body.onload.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.