如何从 Java 小程序更新我的 MySQL 数据库?

发布于 2024-08-06 05:02:49 字数 2790 浏览 5 评论 0原文

我是 Java 新手,我需要一些有关如何调试 Java Applet 的建议/信息。

我创建了一个只更新 MySQL 数据库的小程序。小程序似乎加载到网页中没有错误。当我单击按钮更新数据库时,它似乎实际上调用了小程序,但没有任何反应,即没有向数据库进行新插入,并且页面正确返回。

我已经获取了小程序代码并在 Java 桌面应用程序中对其进行了测试。它工作正常,除了删除“扩展小程序”修饰符之外没有任何更改。在桌面应用程序中,数据库得到正确更新。

如果我得到一些关于如何写入 Java 控制台窗口的指示,这可能会帮助我调试代码 - 但我不知道该怎么做。我不确定还可以尝试什么来找到问题。对我来说一切似乎都是正确的。

顺便说一句:我在 Windows 7 中使用 Netbeans 6.7,在 CENTOS (Linux) 系统上使用 MySQL 服务器和 Glassfish 2.1。

这是我的小程序代码:

/*
 * 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");
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪淡了忧伤 2024-08-13 05:02:49

如果可能的话,Applet 中应该避免使用 JDBC。以下是您将面临的安全问题,

  1. 您必须向所有 IP 地址开放数据库,除非这是内部或企业应用程序。
  2. 您的数据库密码将位于小程序中,任何进行一点逆向工程的人都可以读取。

如果您确实想这样做,您需要通过签名来使用受信任的 Applet。

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.

十年不长 2024-08-13 05:02:49

由于您已经将 localhost 作为服务器地址...,除非您在同一个机器上运行 mysql 服务器,否则这将导致问题。另外,我相信存在安全限制,不允许通过 Java Applet 的端口联系本地主机。

希望这有帮助。

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.

我纯我任性 2024-08-13 05:02:49

小程序在沙箱中运行,(在浏览器中时)沙箱极大地限制了它们的功能。一般来说,除了为其提供服务的主机之外,它们无法打开与任何主机的连接。

本网站: http://www.securingjava.com/chapter-two /chapter-two-2.html 有点过时,但让您对将面临的限制有一个很好的总体了解。

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.

毁梦 2024-08-13 05:02:49

失败最可能的原因是类加载器异常。 applet 的类加载器是一个 URLClassloader,出于安全考虑,它只能从某些 URL 加载类。

在这种情况下,applet 类加载器很可能无法加载 MySQL JDBC 驱动程序。如果必须使小程序工作,请将 MySQL 驱动程序的 jar 文件放置在小程序可以访问的 Web 服务器上的区域中,并使用 applet 标记的 archive 属性 使类加载器能够加载驱动程序。

为什么不应该这样做?

虽然上面给出的答案在技术上可行,但将数据库暴露在互联网或 DMZ(非军事区)上是一种非常糟糕的做法;在某些公司中通常还包括内部网。据推测,您这样做是为了研究小程序,而不是为了生产用途。 ZZ Coder 已经指出了这一点

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文