Node.js是一個非常受歡迎的JavaScript執行環境,它可以在伺服器端進行程式設計。與其他語言相比,它非常快速和靈活,並具有處理I/O密集型任務的能力。除此之外,Node.js在與資料庫進行互動時也表現出色。在這篇文章中,我們將學習如何使用Node.js存取和操作Oracle資料庫。
在使用Node.js連接Oracle資料庫之前,我們需要確保已安裝了以下內容:
$ sudo apt-get install libaio1 $ wget https://download.oracle.com/otn_software/linux/instantclient/1912000/oracle-instantclient19.12-basic-19.12.0.0.0-1.x86_64.rpm $ sudo alien -i oracle-instantclient19.12-basic-19.12.0.0.0-1.x86_64.rpm
$ npm install oracledb
module.exports = { user: "用户名", password: "密码", connectString: "连接字符串" }
const oracledb = require('oracledb'); const dbConfig = require('./dbconfig.js'); oracledb.getConnection( { user: dbConfig.user, password: dbConfig.password, connectString: dbConfig.connectString }, function(err, connection) { if (err) { console.error(err.message); return; } console.log('Connection was successful!'); connection.close( function(err) { if (err) { console.error(err.message); return; } console.log('Connection was closed!'); }); });
const oracledb = require('oracledb'); const dbConfig = require('./dbconfig.js'); oracledb.getConnection( { user: dbConfig.user, password: dbConfig.password, connectString: dbConfig.connectString }, function(err, connection) { if (err) { console.error(err.message); return; } console.log('Connection was successful!'); connection.execute( `SELECT empno, ename FROM emp`, function(err, result) { if (err) { console.error(err.message); return; } console.log(result.rows); connection.close( function(err) { if (err) { console.error(err.message); return; } console.log('Connection was closed!'); }); }); });
以上是nodejs如何存取oralce的詳細內容。更多資訊請關注PHP中文網其他相關文章!