0

I'm new to Java and I need some advice/information on how to debug my Java Applet.

I have created a applet that simply updates a MySQL database. The applet seems to load in the web page with no errors. When I click on my button to update the database it seems to actually make the call to the applet, BUT nothing happens, i.e. no new inserts are made to the database, and the page returns properly.

I have taken the applet code and tested it in a Java desktop app. It works fine, no changes other than removing the "extend Applet" modifier. In the desktop app the database gets updated properly.

If I was given some pointers on how to write to the Java Console window that might help me in debugging the code - but I don't know how to do that. I'm not sure what else to try to find the issue. Everything seems correct to me.

BTW: I'm using Netbeans 6.7 in Windows 7 with the MySQL server and Glassfish 2.1 on a CENTOS (Linux) system.

Here is my code for the applet:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.db;
import java.applet.*;
import java.sql.*;

/**
 *
 * @author Rick
 */
public class dbapplet extends Applet {

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    public void init() {
        // TODO start asynchronous download of heavy resources
    }

    public long SaveToDatabase(String subject, String note, int priority,
            String category, String isOpen, String currentSession) {
        Connection con = null;
        Statement stmt = null;
        StringBuilder sb = new StringBuilder();
        long lastInsertId = -1;

        try {
            //build the insert
        int IsOpen = (isOpen.contains("1")) ? 1 : 2;
            sb.append("INSERT INTO 'LogDetails' ('category', 'priority', 
                 'subject', 'note', 'is_open', 'has_attachements') VALUES");
            sb.append(" (");
            sb.append("'" + category + "',");
            sb.append(priority + ",");
            sb.append("'" + subject + "',");
            sb.append("'" + note + "',");
            sb.append("b'" + IsOpen + "',");
            sb.append("b'0');");

            //connect and execute the insert
            String dbURL = "jdbc:mysql://localhost/authentication";
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(dbURL, "xxxxxxx", "yyyyyyyy");
            stmt = con.createStatement();
            stmt.execute(sb.toString());

            //get the last inserted id
            ResultSet rs = null;
            rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");

            if (rs.next()) {
                lastInsertId = rs.getLong(1);
            }
            rs.close();

        } catch (Exception e) { //database problem
             System.out.println("Error " + e.getMessage());
             System.out.println(sb.toString());
        }
        return lastInsertId;
    } //end of SaveToDatabase

     public void QuickSaveToDataBase() {
        //disgard the result for now - lets see if we can get this working
        this.SaveToDatabase("Quick Save", "Testing of the Quick Save Function",
               1, "Testing", "1", "skjdkjd-junk");
    }
}
3
  • The Webpage Code looks like this.... <script> function ajaxQuickSave() { document.applets[0].QuickSaveToDatabase(); alert("After Database Call"); } </script> <applet code="org.me.db.dbapplet" archive="DBLookup.jar" width=300 height=50> <param name="bgcolor" value="ffffff"> <param name="fontcolor" value="000000"> Your browser is not Java enabled. </applet> Commented Sep 21, 2009 at 17:33
  • Are you sure that the username and the password are correct? ;-) Commented Sep 21, 2009 at 17:38
  • Seeing the exception being displayed on the Console would help greatly when diagnosing your problem. Commented Sep 21, 2009 at 18:15

4 Answers 4

3

JDBC in Applet should be avoided if all possible. Here are the security issues you will be facing,

  1. You have to open up your database to all IP addresses unless this is an inhouse or enterprise app.
  2. Your database password will be in the applet, readable by anyone with a little reverse-engineering.

If you really want do this, you need to use trusted Applet by signing it.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, this is an internal app But I get your point. What should be used instead of an applet for database connectivity that would give me the same results?
@Rick, a web application or a Java thick client (Swing GUI) app would be ideal.
Write a Java App and deployed it using JNLP, See en.wikipedia.org/wiki/Java_Web_Start
@ZZ: a distributed JNLP client application does not really help for your security problems, does it?
0

Since you've got localhost as the server's address..., unless you're running the mysql server on the same box, this will cause a problem. Also, I believe there are security restrictions that disallow contacting localhost over a port from a Java Applet.

Hope this helps.

1 Comment

lassfish and MySQL reside on the same system. However I did change the code to use the IP Address. The DBLookup Applet is a separate project from the web project. So I did a clean and build on the Applet, then a clean and build, deploy on the web project. I think that would make sure I have the latest DBLookup jar file on the server. Right? Same issue - no difference with the proper ip.
0

Applets run in a sandbox that (when in browser) dramatically restrict what they can do. In general, they can't open up connection to any host other than the one they were served up from.

This site: http://www.securingjava.com/chapter-two/chapter-two-2.html is a little dated, but gives you a good general idea for what restrictions you'll be facing.

Comments

0

The most likely reason for the failure is a classloader exception. The applet's classloader is a URLClassloader that can load classes only from certain URLs due to the security implications.

In this case, the applet classloader is most likely unable to load the MySQL JDBC driver. If you have to make the applet work, place the MySQL driver's jar files in an area on the web server that is accessible by the applet, and use the archive attribute of the applet tag to enable the classloader to load the driver.

Why should you not do this?

Although the answer given above will work technically, it is a really bad practice to expose your database on the internet or a DMZ(de-militarized zone); that normally includes an intranet as well in certain companies. Presumably, you are doing this for studying applets and not for production usage. ZZ Coder has already pointed this out.

3 Comments

Would a servlet be a better fit instead of an applet?
Yes, it would be if your users indeed require a web application. That way database connections would be initialized by the application server and you do not have to fight to open up the database to all client workstations. You could start off with a lightweight servlet container like Tomcat or Jetty.
I take that suggestion of Tomcat/Jetty back. I didn't remember that you were already using Glassfish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.