views:

145

answers:

4

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 webpage 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");
    }
}
A: 

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.

Allain Lalonde
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.
Rick
A: 

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.

Tim Howland
+3  A: 

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.

ZZ Coder
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
@Rick, a web application or a Java thick client (Swing GUI) app would be ideal.
Vineet Reynolds
Write a Java App and deployed it using JNLP, See http://en.wikipedia.org/wiki/Java_Web_Start
ZZ Coder
A: 

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.

Vineet Reynolds
Would a servlet be a better fit instead of an applet?
Rick
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.
Vineet Reynolds
I take that suggestion of Tomcat/Jetty back. I didn't remember that you were already using Glassfish.
Vineet Reynolds