首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Java applet更新我的MySQL数据库?

如何从Java applet更新我的MySQL数据库?
EN

Stack Overflow用户
提问于 2009-09-21 17:28:30
回答 4查看 1.8K关注 0票数 0

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

我已经创建了一个更新MySQL数据库的applet。在网页中加载小程序似乎没有错误。当我单击我的按钮更新数据库时,它似乎实际上调用了applet,但什么也没有发生,即没有对数据库进行新的插入,并且页面正常返回。

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

如果给我一些关于如何编写Java控制台窗口的提示,这可能会帮助我调试代码-但我不知道如何做到这一点。我不确定还能找出什么问题。在我看来一切都是对的。

顺便说一下:我在Windows7和MySQL服务器上使用NetBeans6.7,在CENTOS (Linux)系统上使用Glassfish 2.1。

下面是我的applet代码:

代码语言:javascript
复制
/*
 * 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");
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-09-21 18:21:00

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

  1. 您必须向所有IP地址开放您的数据库,除非这是一个内部或企业应用程序。
  2. 您的数据库密码将在小程序中,任何具有一点reverse-engineering.

的人都可以读取

如果您真的想这样做,您需要通过对其进行签名来使用trusted Applet。

票数 3
EN

Stack Overflow用户

发布于 2009-09-21 17:36:23

因为您已经将localhost作为服务器的地址……,除非您在同一机器上运行mysql服务器,否则这将导致问题。此外,我相信有安全限制,不允许从Java Applet通过端口联系localhost。

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2009-09-21 17:50:34

Applet在沙箱中运行(当在浏览器中时),这极大地限制了它们可以做的事情。一般来说,它们不能打开到任何主机的连接,而不是连接到它们所在的主机。

这个网站:http://www.securingjava.com/chapter-two/chapter-two-2.html有点过时了,但它给了你一个很好的大体概念,让你知道你将面临哪些限制。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1455735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档