Home > Web Front-end > Front-end Q&A > How to run oracle database stored procedure in nodejs

How to run oracle database stored procedure in nodejs

下次还敢
Release: 2024-04-21 05:52:06
Original
808 people have browsed it

In Node.js, use the OracleDB library to execute Oracle database stored procedures: Install the OracleDB library. Create a database connection. Define the stored procedure name and parameters. Execute the stored procedure. Close the database connection.

How to run oracle database stored procedure in nodejs

Node.js executes Oracle database stored procedure

Method

In Node.js, you can use the OracleDB library to execute Oracle database stored procedures.

Steps

  1. Install OracleDB library
<code>npm install oracledb</code>
Copy after login
  1. Create database connection
<code class="javascript">const oracledb = require('oracledb');

async function createConnection() {
  return oracledb.getConnection(connectionParams);
}</code>
Copy after login
  1. Define stored procedure name and parameters
<code class="javascript">const procedureName = 'MY_PROCEDURE';
const parameters = {
  input: oracledb.NUMBER,
  output: oracledb.OUT
};</code>
Copy after login
  1. Execute stored procedure
<code class="javascript">async function executeProcedure(connection) {
  const result = await connection.execute(
    procedureName,
    parameters,
    {
      autoCommit: true
    }
  );

  return result.outBinds;
}</code>
Copy after login
  1. Close database connection
<code class="javascript">async function closeConnection(connection) {
  await connection.close();
}</code>
Copy after login

Sample code

<code class="javascript">const connectionParams = {
  user: 'username',
  password: 'password',
  connectString: 'host:port/databasename'
};

createConnection()
  .then(connection => executeProcedure(connection))
  .then(result => console.log(result))
  .catch(err => console.error(err))
  .finally(() => closeConnection(connection));</code>
Copy after login

The above is the detailed content of How to run oracle database stored procedure in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template