Node.js Program that retrieves all the details in a MySQL table and displays in a neat format.

Pranat
0
Lamp

 const mysql=require('mysql');

const express = require("express");

const app = express(); //Alias for express

//create connection to MySql

var connection = mysql.createConnection({

host:"localhost",

user: "root",

password: "123456",

database:"Srinivas",

port:"3306"

})

//Now Connect to MySQL

connection.connect(function(err){ //Connect is a callBack method

if(err)

{

throw err

}else{

console.log("Connected");


}

})

//------------------------------------To Create a Table-------------------

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

// connection.query('create table pramod(RollNo int(255) primary key, Name

varchar(255) not null)',function(err,rows){

// if(err){

// throw err

// }else{

// console.log("DATA SENT BOIS");

// console.log(rows);

// }

// })

//------------------------------To Insert a Record into MySql Table-------

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

// connection.query("insert into

pramod(RollNo,Name)values(2,'PRAJWAL')",function(err,rows) {

// if(err){

// throw err

// }else{

// console.log("Data Sent !");

// console.log(rows);

// }

// })

//------------------------------Retrieving All the record from MySql

Table---------------------------

connection.query("SELECT * FROM pramod",function(err,rows){

if(err){

console.log(err.message);

}else{

console.log(rows);

}

let studentData = JSON.parse(JSON.stringify(rows));

for(var i=0;i<studentData.length;i++)

{

console.log("Student ID is :"+studentData[i].RollNo);

console.log("Student Name is :"+studentData[i].Name);

console.log("\n");

}

})

const port = process.env.PORT || 5000; // OR const port = 5000; port

may be 8080 -----------------2

app.listen(port); //OR express().listen(port);

console.log("App is listenin on port "+port);

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !