在JavaScript中調用Or??acle數據庫,調用可以使用Node.js的數據實(shí)現oracledb模塊,以下是簡(jiǎn)易詳細的步驟和小標題:
(圖片來(lái)源網(wǎng)絡(luò ),侵??刪)2、調用創(chuàng )建項目文件夾
在本地創(chuàng )建一個(gè)項目文件夾,數據實(shí)現oraclejs。簡(jiǎn)易
3、調用初始化項目
在項目文件夾中打開(kāi)命令行,數據實(shí)現運行以下命令初始化項目:
“`
npm init y
“`
運行以下命令安裝oracledb模塊:
“`
npm install oracledb
“`
5、調用(yong)編寫(xiě)代碼
在項目文件夾中創(chuàng )建一個(gè)名為app.??js的數據實(shí)現文件,編寫(xiě)以下代碼:
“`javascript
// 設置連接信息
const connectionString = ‘user/ヽ(′ー`)ノpassword@localhost:1521/xe’; // 替換為實(shí)際的簡(jiǎn)易用戶(hù)名、密碼、主機名、端口號和數據庫名
// 創(chuàng )建連接選項對象
const connect(╯°□°)╯︵ ┻━┻ionOptions = {
connectString: connectionString,
pooヽ(′ー`)ノlMin: 1,
poolMax: 10,
poolIncrement: 1,
poolTimeout: 30000, // 30秒超時(shí)時(shí)間
queueTimeout: 60000, // 60秒隊列等待超時(shí)時(shí)間
useヽ(′▽?zhuān)?ノr: ‘your_username’, // 替換( ???)為實(shí)際的用戶(hù)名
password: ‘your_password’, // 替換為實(shí)際的密碼
autoCommit: true, // 自動(dòng)提交事務(wù)
outFormat: oracledb.OUT_FORMAT_OBJECT, // 輸出格式為對象
readonly="readonly": false, // 非只讀模式
allowNonUTF8??: true, // 允許非UTF8字符集
characterSet: ‘AL32UTF8’ // 字符集為AL32UTF8,可以根據需要修改
};
// 連接到Oracle數據庫
oracledb.getConnect(′ω`)ion(connectionOptions)
.th??en(connection => {
co??nsole.log(‘Connected to the database’);
return connection.execute(R┐(′ー`)┌16;SELECT * FROM your_table’); // 替換為實(shí)際的表名和查詢(xún)語(yǔ)句
})
.then(result => {
console.log(‘Result:’, result);
return connection.close(); // 關(guān)閉連接
})
.catch(??err => {
console.e??rror(‘Error:’, err);
return conn(?????)ection.close(); // 關(guān)閉連接
});
6、運行代碼
在命令行中運行以下命令執行代碼:
“`
node app.js
“`
7、查看結果