User Image
Here is a basic example of how to use Node.js with Oracle Database:

Here is a basic example of how to use Node.js with Oracle Database:

GooAdmin Team

Creating DateApril 3, 2023

OracleJavascriptNodeJs

Here is a basic example of how to use Node.js with Oracle Database:

  1. Install the "node-oracledb" driver using npm:

npm install oracledb

  1. Connect to the Oracle Database:
1const oracledb = require('oracledb');
2
3async function connect() {
4  try {
5    const connection = await oracledb.getConnection({
6      user: 'my_user',
7      password: 'my_password',
8      connectString: 'localhost:1521/orcl'
9    });
10    console.log('Connected to database');
11    return connection;
12  } catch (err) {
13    console.error(err.message);
14  }
15}
16
17const connection = await connect();
18

This code connects to an Oracle Database instance running on localhost with the service name orcl, using the specified username and password.

  1. Query the database:
1async function query() {
2  try {
3    const result = await connection.execute(
4      'SELECT * FROM employees WHERE department_id = :id',
5      [50]
6    );
7    console.log(result.rows);
8  } catch (err) {
9    console.error(err.message);
10  }
11}
12
13await query();
14

This code executes a SELECT statement that retrieves all rows from the employees table where the department_id is 50.

  1. Close the connection:
1async function close() {
2  try {
3    await connection.close();
4    console.log('Connection closed');
5  } catch (err) {
6    console.error(err.message);
7  }
8}
9
10await close();
11

This code closes the connection to the Oracle Database.

This is a very basic example, but it demonstrates how to connect to an Oracle Database instance and execute a query using Node.js. You can build on this foundation to create more complex applications that leverage the power of Node.js and Oracle Database

May you like Heart Icon

No posts found in the category "Javascript"

Comments