目录

一:Javascript访问MYSQL

二:JavaScript中操作Mysql数据库实例


一:Javascript访问MYSQL

1、下载MYSQL的ODBC连接
2、在JS中建立ODBC连接如下:
var con = new ActiveXObject("ADODB.Connection");con.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver};OPTION=3;SERVER=127.0.0.1;User ID=root;Password=123456;Database=mysql;Port=3306"; con.open;var rs = new ActiveXObject("ADODB.Recordset");rs.open("select * from user", con);while (!rs.eof) {var u = rs.Fields("User");document.write(u);rs.moveNext;}rs.close();rs = null;con.close();con = null;

需要注意连接字符串中Driver的名称与安装后的ODBC中的Drivers页中的MYSQL名称一致即可!

二:JavaScript中操作Mysql数据库实例

[js]view plain copy

//创建数据库连接对象 var conn = new ActiveXObject("ADODB.Connection"); //创建数据集对象 var rs = new ActiveXObject("ADODB.Recordset"); try{ //数据库连接串,具体配置请参考:http://www.connectionstrings.com/ //如果不知道如何配置连接串,可以通过配置UDL文件后用文本编辑器打开获得 var connectionstring = "Driver={MySQL ODBC 5.2w Driver};Server=localhost;Database=myDataBase;User=myUsername; Password=myPassword;Option=3;Port=3306"; //打开连接 conn.open(connectionstring);//查询语句 var sql = " select * from tb_col "; //打开数据集(即执行查询语句) rs.open(sql,conn); // (或者rs=conn.execute(sql);) //遍历所有记录 while(!rs.eof){ //WScript是Windows 的脚本宿主对象,详细情况请在windows帮助里查找。 //WScript.Echo输出记录的内容 "id") + "\t" + rs.Fields("name") + "\n");//下一条记录 rs.moveNext(); } //关闭记录集 rs.close(); //关闭数据库连接 conn.close(); } catch(e){ //异常报告 WScript.Echo(e.message); } finally{ // }

JavaScriptDataBase Connector
JSDBC:提供Javascript有效的连接数据库,目前支持MySQL、SQLite、ACCESS,后期会支持更多的数据库;
在从事AJAX开发的工程师肯定会希望有一个通过AJAX直接连接数据库的组件,这样,可以省掉后台很多的操作步骤,比如免去了部署Java的运行环境,免去了写很多复杂的JDBC调用,不管出于调试的需要还是应用的需要,JSDBC都能够很好的集成到您的应用当中。

/*** 类说明:该脚本主要是实现与JSDBC for MySQL 连接,方便用户在js直接使用MySQL* 创建事件:2009-06-12*/ //Include OCX Objectdocument.writeln(" ");document.writeln(" ");//error messagevar lasterr = "";//Exec Falgvar execFlag; /** * Connecte to mysql server * provite:MySQL IP,PORT,DB Name,USER,Password,CharSet */function connectMySQL() {execFlag = mysql.connecte("127.0.0.1", "3306", "testdb", "root", "123210", "GBK");if (execFlag == 1)return 1;else {lasterr = mysql.getLastError();return 0;}} /** * Close already open Connection */function closeMySQL() {execFlag = mysql.close();if (execFlag == 1)return 1;else {lasterr = mysql.getLastError();return 0;}} /** * Exec Insert Into SQL statement * @param {Object} sql */function insertMySQL(sql) {execFlag = mysql.insertData(sql);if (execFlag == 1)return 1;else {lasterr = mysql.getLastError();return 0;}} /** * Exec DataBase Manager Language * @param {Object} sql */function execDMLMySQL(sql) {execFlag = mysql.execDML(sql);if (execFlag == 1)return 0;else {lasterr = mysql.getLastError();return 0;}} /** * Exec Select Data From DataBase * @param {Object} sql * @param {Object} cnum == Number of fields in SQL statement */function selectMySQL(sql, cnum) {var rs = mysql.selectData(sql, cnum);if (rs.length > 0) {var array = new Array();var DataSet = new Array();var rowsplit = '';//行间隔 ,注意,这并不是普通的'-',而是0x06 转换而来的,使用时拷贝过去即可var fieldsplit = '';//字段间隔,注意,这并不是普通的'|',而是0x05 转换而来的,使用时拷贝过去即可array = rs.split(rowsplit);for (var i = 0; i < array.length; i++) {var DataRow = array[i].split(fieldsplit);DataSet[i] = DataRow;}return DataSet;}else {lasterr = mysql.getLastError();return null;}}/** * Exec Delete SQL statement * @param {Object} sql */function deleteMySQL(sql) {execFlag = mysql.deleteData(sql);if (execFlag == 1)return 1;else {lasterr = mysql.getLastError();return 0;}} /** * Exec Update SQL statement * @param {Object} sql */function updateMySQL(sql) {execFlag = mysql.updateData(sql);if (execFlag == 1)return 1;else {lasterr = mysql.getLastError();return 0;}}/** * Exec Call Produce * @param {Object} proname == Produce Name * @param {Object} inparas == Produce IN Parameters * @param {Object} outparas == Produce OUT Parameters * @param {Object} cnum == Number of fields in OUT Parameters */function callProduceMySQL(proname, inparas, outparas, cnum) {var rs = mysql.execProduce(proname, inparas, outparas, cnum);if (rs.length == 0) {lasterr = mysql.getLastError();return null;}else {var array = new Array();var DataSet = new Array();var rowsplit = '';//行间隔 ,注意,这并不是普通的'-',而是0x06 转换而来的var fieldsplit = '';//字段间隔,注意,这并不是普通的'|',而是0x05 转换而来的array = rs.split(rowsplit);var DataRow = new Array();for (var i = 0; i < array.length; i++) {var fieldarray = array[i].split(fieldsplit);DataSet[i] = fieldarray;}return DataSet;}} /** * Exec Transcation * @param {Object} sql */function execTranscationMySQL(sql) {execFlag = mysql.execTranscation(sql);if (execFlag == 1)return 1;else {lasterr = mysql.getLastError();return 0;}} /** * Get Last Error Message if exec error from js */function getLastErrorMySQL() {return lasterr;}/** * Get Last Error Message if exec error from ocx */function getLastErrorFromMySQL() {var lasterrmysql = mysql.getLastError();return lasterrmysql;}