使用Java Applet访问本地客户端数据库

时间:2013-08-05 03:43:24

标签: java applet signed-applet

我正在尝试构建一个Java客户端,它连接到客户端的数据库并执行一些查询。

applet需要使用JDBC ...所以我实现了一个非常简单的JDBC连接到位于我的C:/驱动器上的数据库文件。

签署应用程序后(没有签署应用程序,许多安全警报和墙壁)我遇到了这个错误: “此JVM实例中不允许对Applet ID 1进行Liveconnect调用”

有没有人知道一种更简单的流媒体方式,并通过网络浏览器从客户端数据库中获取结果? 或者有谁知道为什么会这样?

我的代码: AppletStart.java(客户端 - 小程序)

public class AppletStart extends Applet {

    private static final long serialVersionUID = -2032951133100677643L;

    public void queryDatabase(final String databaseFilePath, final String databaseQuery) {

        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                JDBCBean jdbcTest = new JDBCBean();
                jdbcTest.setDatabaseFilePath(databaseFilePath);
                jdbcTest.setDatabaseQuery(databaseQuery);

                try {
                    buildResult(jdbcTest.queryDatabase());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    public void buildResult(ResultSet resultSet) throws SQLException {
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columns = metaData.getColumnCount();
        String text = "";

        while (resultSet.next()) {
            for (int i = 1; i <= columns; ++i) {
                text += "" + metaData.getColumnName(i) + ":\t";
                text += resultSet.getString(i);
                text += "\n";
            }
            text += "\n";
        }

        alert(text);
    }

    public void alert(String text) {
        try {
            JSObject window = JSObject.getWindow(this);
            window.call("showAlert", new Object[] { text });
        } catch (JSException jse) {
            jse.printStackTrace();
        }
    }

}

JDBCBean.java(客户端 - 小程序):

public class JDBCBean {

    private static String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
    private static String CONN_STRING_FRAGMENT = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";

    private Connection connection;
    private Statement statement;

    private String databaseFilePath;
    private String databaseQuery;

    public void setDatabaseFilePath(String databaseFilePath) {
        this.databaseFilePath = databaseFilePath;
    }

    public void setDatabaseQuery(String databaseQuery) {
        this.databaseQuery = databaseQuery;
    }

    public ResultSet queryDatabase() {
        try {

            Class.forName(JDBC_DRIVER).newInstance();
            String connectionString = CONN_STRING_FRAGMENT + databaseFilePath;
            connection = DriverManager.getConnection(connectionString, "", "");

            buildStatement();
            return executeQuery();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public void buildStatement() throws SQLException {
        statement = connection.createStatement();
    }

    public ResultSet executeQuery() throws SQLException {

        boolean foundResults = statement.execute(databaseQuery);

        if (foundResults) {
            ResultSet set = statement.getResultSet();
            if (set != null)
                return set;
        }

        connection.close();
        return null;
    }

}

HTML(服务器端):

<body onload="queryDatabase()">
    <script>

        var databaseFilePath = "C:/test_db.mde";
        var databaseQuery = "SELECT * FROM test_table";

        function showAlert(text) {
            alert(text);
        }

        function queryDatabase() {
            document.dbApplet.queryDatabase(databaseFilePath, databaseQuery);
        }

    </script>
    <applet id="dbApplet" code="br.com.applet.AppletStart" archive="../resources/applet/dbapplet.jar" style="width: 1px; height: 1px; float: left;" mayscript="mayscript"></applet>
</body>

1 个答案:

答案 0 :(得分:0)

如果您尝试从applet创建到localhost的jdbc连接:只要与本地数据库的连接与applet的远程访问相同(由于安全限制),您应该使用通用规则:

http://www.oracle.com/technetwork/java/dba-140353.html#applet

对于db主机解析 - 如果它不能通过'localhost'或'127.0.0.1'访问,那么你可能应该找到另一种方法来解析本地主机连接路径。

如果您尝试访问文件以进行阅读而不是使用jdbc,那么您也需要为此操作获得授予权限:

http://www.coderanch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem