728x90
const express = require("express");
const app = express();
const server = app.listen(3007, () => {
console.log("Start Server: localhost: 3007");
})
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);
app.get("/", function(req, res){
res.render("index2.html");
})
const oracledb = require("oracledb");
oracledb.initOracleClient();
const dbconfig = require("./dbconfig.js");
app.get("/table", function(req, res){
getTable(req, res);
})
app.get("/selectOne", function(req,res){
getSelectOne(req, res);
})
async function getTable(req, res){
let connection;
try {
connection = oracledb.getConnection({
user: dbconfig.user,
password: dbconfig.password,
connectString: dbconfig.xid
})
const result = (await connection).execute("select * from dept order by deptno",
function(error, data){
if(error){
console.log(error);
}
var headName = "";
for (let index = 0; index < data.metaData.length; index++) {
headName += "<th>"+data.metaData[index].name+"</th>"
}
var dataSet = "";
for (let index = 0; index < data.rows.length; index++) {
var dataes = "";
dataes += "<td><a href='/selectOne?deptno="+data.rows[index][0]+"'>"
+data.rows[index][0]+"</a></td>"
+ "<td>"+data.rows[index][1]+"</td>"
+ "<td>"+data.rows[index][2]+"</td>"
dataSet += "<tr>"+dataes+"</tr>";
}
var resultData = "<html><head><body><table border='1'>"
+"<tr>"+headName+"</tr>"
+dataSet
+"</table></body></head></html>"
res.send(resultData);
});
} catch (error) {
console.log(error);
} finally {
if(connection){
try{
(await connection).close
} catch(error){
console.log("error: ", error);
}
}
}
}
async function getSelectOne(req, res){
let connection;
/********************************************************/
let deptno = req.param("deptno"); //부서번호 출력
console.log("parameter deptno: ", deptno);
/*********************************************************/
try {
connection = oracledb.getConnection({
user: dbconfig.user,
password: dbconfig.password,
connectString: dbconfig.xid
})
/*********************************************************************************************************/
const result = (await connection).execute("select * from dept where deptno=:num order by deptno", [deptno],
/*********************************************************************************************************/
function(error, data){
if(error){
console.log(error);
}
/********************************************************/
var headName = ""; //컬럼 추가
for (let index = 0; index < data.metaData.length; index++) {
headName += "<th>"+data.metaData[index].name+"</th>"
}
/********************************************************/
var dataSet = "";
for (let index = 0; index < data.rows.length; index++) {
var dataes = "";
dataes += "<td><a href='/selectOne?deptno="+data.rows[index][0]+"'>"
+data.rows[index][0]+"</a></td>"
+ "<td>"+data.rows[index][1]+"</td>"
+ "<td>"+data.rows[index][2]+"</td>"
dataSet += "<tr>"+dataes+"</tr>";
}
var resultData = "<html><head><body><table border='1'>"
/********************************************************/
+"<tr>"+headName+"</tr>" //컬럼 추가
/********************************************************/
+dataSet
+"</table></body></head></html>"
res.send(resultData);
}
);
} catch (error) {
console.log(error);
} finally {
if(connection){
try{
(await connection).close
} catch(error){
console.log("error: ", error);
}
}
}
}
'단순 코드 기록 > Node' 카테고리의 다른 글
Node_pathVariable_Delete (0) | 2024.03.26 |
---|---|
Node_Form을 이용한 Insert (0) | 2024.03.26 |
Node_99번 부서 중복insert/update/delete/Table (0) | 2024.03.25 |
Node_10번 부서 출력 + insert (0) | 2024.03.25 |
Node_DB의 컬럼과 로우 출력 (0) | 2024.03.25 |