Node.jsでDB接続

 

(8.0.31)

https://programming-engineer.com/nodejs-database-connection/
https://ictdiary.hatenadiary.jp/entry/2018/12/03/221312


OS: CentOS 7

yum -y install epel-release
yum -y install nodejs
yum -y install npm

node -v
npm -v

mkdir test;cd test
npm init --yes

npm install mysql
cat package.json

vim a.js
---------------------

const mysql = require('mysql');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  port : 3306,
  database: 'test'
});

var sql = "select * from tab1";
connection.query(sql, function (err, results, fields) {
  if(err){
    console.error("sql execute error");
  }else{
    console.log(results);
    connection.end();
  }
});

---------------------

node a.js

 

(19c)
https://qiita.com/ho-rai/items/74ee0345590ed5923870
https://hellomyworld.net/posts/access-oracle-db-from-nodejs/

OS: CentOS 7

yum -y install epel-release
yum -y install nodejs
yum -y install npm

node -v
npm -v

su - oracle

mkdir test;cd test
npm init --yes

npm install oracledb
cat package.json

vim a.js
---------------------

const oracledb=require('oracledb');

const dbconfig = {
  user          : "test",
  password      : "test",
  connectString : "mmm065:1521/pdb1.example.com",
};

(async () => {
try {
  sql='select * from tab1';
  parameter=[];
  connection = await oracledb.getConnection(dbconfig);
  options = {outFormat: oracledb.OUT_FORMAT_OBJECT};
  result = await connection.execute(sql,parameter, options);
  console.log(result.rows);
}catch(error){
  console.log("ERROR"+error);
}finally{
  await connection.close();
}
})();

 

---------------------

node a.js

 

(15)
https://www.dailyupblog.com/backend_development/962/

OS: Rocky Linux 8


dnf -y install epel-release
dnf -y install nodejs
dnf -y install npm

node -v
npm -v

su - postgres

mkdir test;cd test
npm init --yes

npm install pg
cat package.json

vim a.js
---------------------

const { Client } = require("pg");
const client = new Client({
  user: "postgres",
  password: "postgres",
  host: "localhost",
  port: 5432,
  database: "test",
});

var sql = "select * from tab1";
client.connect();
client.query(sql, (err, res) => {
  console.log(res.rows);
  client.end();
});


---------------------
node a.js

 

(2019)
https://syon.github.io/refills/rid/1564145/


https://www.npmjs.com/package/mssql


OS: CentOS 7

yum -y install epel-release
yum -y install nodejs
yum -y install npm

node -v
npm -v

mkdir test;cd test
npm init --yes

npm install mssql
cat package.json

vim a.js
---------------------

const mssql = require("mssql");

const config = {
  user: "sa",
  password: "password",
  server: "10.1.1.61",
  database: "test",
  options: {
    encrypt: false
  }
};

(async () => {
try {
  await mssql.connect(config);
  const result = await mssql.query`select * from tab1`;
  console.log(result.recordset);
}catch(error){
  console.log("ERROR"+error);
}finally{
  await mssql.close();
}
})()

---------------------

node a.js