npm官网 https://www.npmjs.com/package/oracledb
node-oracledb api https://oracle.github.io/node-oracledb/doc/api.html#getstarted
node-oracledb例子 https://github.com/oracle/node-oracledb/blob/master/examples/example.js#L32
1 查看安装教程 https://github.com/oracle/node-oracledb/blob/master/INSTALL.md#instwin
找到 使用Instant Client ZIP文件在Windows上安装Node-oracledb,按照说明操作
Client ZIP 文件下载地址 https://www.oracle.com/technetwork/topics/winsoft-085727.html
2 安装oracledb 模块
npm install oracledb
3 连接数据库
Oracledb.js
const Oracledb = require('oracledb');
const Oracle = {
connect: (connectInfo) => {
return new Promise(function (resolve, reject) {
Oracledb.getConnection(connectInfo, function (err,conn) {
if (err) {
console.log(err.message);
}
console.log("oracleDB连接成功");
resolve(conn);
});
});
},
close: conn => {
conn.close(err => {
if (err)
console.error(err.message);
console.log("关闭连接");
});
}
}
module.exports = Oracle;
操作数据库
Common.js
class Common {
getOracleData(conn,query) {
return new Promise(function (resolve, reject) {
conn.execute(
query,
[],
{ resultSet: true },
function (err, result) {
if (err) console.log(err.message);
result.resultSet.getRows(
1000000,
function(err,row){
if (err) console.log(err);
resolve(row);
});
});
});
}
}
建立连接并操作
const Oracle = require('./Oracledb');
const Common= require('./Common');
const Config = {
ORACLE_DEMO: {
user: 'test', //用户
password: '123',//密码
connectString: '192.168.1.121/ORCL' //连接地址
}
}
//建立数据库连接
let Connect = await Oracle.connect(Config.ORACLE_DEMO);
//查询数据数据
let totalDataArr = await Common.getOracleData(Connect , "SELECT DATA FROM TESTDATABASE");
//关闭数据库连接
Connect.close();