I want to access a MySQL database directly from JavaScript code in an HTML page in Firefox.
Does such a library exist?
To be very clear, CGI+Ajax wil
Javascript can access MySQL...but generally only on the server. I've done it with Rhino, a java based javascript interpreter. Just included the MySQL driver, and its available. I imagine you could probably do this with an applet as well.
using Rhino, it would be something like this:
var DATABASE = {
database: 'blog_development',
host: 'localhost',
username: 'dbuser',
password: 'dbpass'
};
function ArticleModel(properties) {
for (var p in properties) {
this[p] = properties[p];
}
}
ArticleModel.findAll = function() {
var results = [];
var jsConnectionObj = new Packages.MysqlConnection();
c = jsConnectionObj.open(DATABASE.host,
DATABASE.database,
DATABASE.username,
DATABASE.password);
if (c) {
var s = c.createStatement();
s.executeQuery("SELECT * FROM articles;");
var rs = s.getResultSet();
while (rs.next()) {
results.push(new ArticleModel({
id: rs.getInt("id"),
title: rs.getString("title"),
body: rs.getString("body")
}));
}
rs.close();
c.close();
return results;
}
throw new Error('could not connect to database');
};