2

I am trying to build a Java Applet that connects to database on the client side and executes some query.

The applet needs to use JDBC... so I implemented a very simple JDBC connection to a database file located on my C:/ drive.

After signing the app (without signing the app, many security alerts and walls) I'm stuck at this error: "Liveconnect call for Applet ID 1 is not allowed in this JVM instance"

Does anyone knows a simpler way of streaming and getting the results from a client located database through a web-browser? Or does anyone knows why this is happening?

My codes: AppletStart.java (Client Side - Applet)

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 (Client Side - Applet):

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 (Server side):

<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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
BBacon
  • 2,456
  • 5
  • 32
  • 52

1 Answers1

0

If you are trying to create jdbc connection to localhost from applet: as far as connection to local database is same as access to remote for applets(due to security restriction), you should use common rules:

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

As for db host resolving - if it will not access via 'localhost' or '127.0.0.1', than your probably should find another way to resolve local host connection path.

If you are trying to access file for reading and than work with jdbc, you shuld grant rights for this operation too:

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

msangel
  • 9,895
  • 3
  • 50
  • 69
  • I'm still getting "Liveconnect call for Applet ID 1 is not allowed in this JVM instance" error. – BBacon Aug 05 '13 at 04:55
  • did you try these(first response on google querying of this error): [1](https://code.google.com/p/jzebra/issues/detail?id=126), [2](https://bugzilla.mozilla.org/show_bug.cgi?id=146458) ? – msangel Aug 05 '13 at 08:33