Hi All,
I need to store/retrieve data in a local environment. Everything is built as web pages controlled by JavaScript(s), and there is no web server running.
As I'm using FireFox, I figured out that my only way to talk to an SQL engine will be through JDBC.
Here is my attempt to do it:
// the Java applet import java.applet.Applet; import java.sql.*; public class Query extends Applet { public Connection conn = null; public void start() { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://servername:1433;DatabaseName=dbname","username","password"); } catch (Exception e) { e.getMessage(); // -> ? } } public String[][] qSelect(String qry) { ResultSet rs; try { Statement stmt = conn.createStatement(); rs = stmt.executeQuery(qry); stmt.close(); // transform rs in String[][] for JS int rows = 0; while(rs.next()) rows++; rs.first(); ResultSetMetaData metaData = rs.getMetaData(); int cols = metaData.getColumnCount(); String[][] rslt = new String[rows][cols]; int i=0; while(rs.next()) { for (int j=0;j<cols;j++) rslt[i][j] = rs.getString(j+1); i++; } return rslt; } catch (SQLException se) { String[][] rslt = new String[1][1]; rslt[0][0] = "Exception"; return rslt; } } public int qOther(String qry) { try { Statement stmt = conn.createStatement(); stmt.executeUpdate(qry); stmt.close(); return 1; } catch (SQLException se) { return 0; } } public void stop() { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); // -> ? } } }
The idea is to get data with qSelect, and to insert/update/delete with qOther. There is no user communication from the applet - just via the calling JavaScript code.HTML Code:<!-- and the HTML part --> <html> <body> <applet id="sql" code="Query.class" width=1 height=1> </applet> <script type="text/javascript"> var rez = new Array(); rez = document.sql.qSelect('SELECT * FROM aTable;'); l = rez.length; alert(l+'\n'+rez); </script> </body> </html>
In the status bar I get "Applet Query started", but nothing else.
What am I doing wrong?
Thanks,
SxN