简体   繁体   中英

How can I update my MySQL database from a Java applet?

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, ie 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");
    }
}

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.

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM