xxxxxxxxxx
async function myfunc() {
const stmts = [
`INSERT INTO ADRESSES (ADDRESS_ID, CITY) VALUES (94065, 'Redwood Shores')`,
`INSERT INTO EMPLOYEES (ADDRESS_ID, EMPLOYEE_NAME) VALUES (94065, 'Jones')`
];
for (const s of stmts) {
await connection.execute(s);
}
}
xxxxxxxxxx
// Close the pool cleanly if Node.js is interrupted
process
.once('SIGTERM', closePoolAndExit)
.once('SIGINT', closePoolAndExit);
xxxxxxxxxx
async function handleRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE html><html><head><title>My App</title></head><body>");
let connection;
try {
connection = await oracledb.getConnection(); // get a connection from the default pool
const result = await connection.execute(`SELECT * FROM locations`);
displayResults(response, result); // do something with the results
} catch (err) {
response.write("<p>Error: " + text + "</p>");
} finally {
if (connection) {
try {
await connection.close(); // always release the connection back to the pool
} catch (err) {
console.error(err);
}
}
}
response.write("</body></html>");
response.end();
}
xxxxxxxxxx
const oracledb = require('oracledb');
oracledb.initOracleClient({
driverName: 'My Great App : 3.1.4'
errorUrl: 'https://example.com/MyInstallInstructions.html',
});
xxxxxxxxxx
const oracledb = require('oracledb');
const mypw = // set mypw to the hr schema password
async function run() {
try {
connection = await oracledb.getConnection({
user : "hr",
password : mypw,
connectString : "localhost/XEPDB1"
});
result = await connection.execute(`SELECT last_name FROM employees`);
console.log("Result is:", result);
} catch (err) {
console.error(err.message);
} finally {
if (connection) {
try {
await connection.close(); // Always close connections
} catch (err) {
console.error(err.message);
}
}
}
}
run();
xxxxxxxxxx
const oracledb = require('oracledb');
const mypw = // set mypw to the hr schema password
// Start a connection pool (which becomes the default pool) and start the webserver
async function init() {
try {
await oracledb.createPool({
user : "hr",
password : mypw, // mypw contains the hr schema password
connectString : "localhost/XEPDB1",
poolIncrement : 0,
poolMax : 4,
poolMin : 4
});
const server = http.createServer();
server.on('error', (err) => {
console.log('HTTP server problem: ' + err);
});
server.on('request', (request, response) => {
handleRequest(request, response);
});
await server.listen(3000);
console.log("Server is running");
} catch (err) {
console.error("init() error: " + err.message);
}
}
xxxxxxxxxx
// Close the default connection pool with 10 seconds draining, and exit
async function closePoolAndExit() {
console.log("\nTerminating");
try {
await oracledb.getPool().close(10);
process.exit(0);
} catch(err) {
console.error(err.message);
process.exit(1);
}
}
xxxxxxxxxx
async function init() {
try {
await oracledb.createPool({ // no need to store the returned pool
user: 'hr',
password: mypw, // mypw contains the hr schema password
connectString: 'localhost/XEPDB1',
poolAlias: 'hrpool'
});
// do stuff
. . .
// get the pool from the cache and use it
const pool = oracledb.getPool('hrpool');
. . .
}