NodeJS_Unit_7

Database Connectivity using NodeJS


Steps for Database Connectivity using NodeJS with MySql


Step 1: Create Project Folder
Step 2: Install MySql Driver
To Access MySql Database with Nodejs we required MySql Driver for that we use “mysql” module
Command: npm install mysql
Step 3: Create Connection
Step 4: We can start querying using SQL statements
Step 5: Closing the connection

Example:
    //include mysql module
    var mysql = require('mysql');

    //Create connection variable with the require details
    var con = mysql.createConnection({
    host: "localhost", //IP addess of server running mysql
    user: "root", // user name to your mysql database
    password: "root", // corresponding password
    database: "employee", // Database name
    port: ”3306” // port number
    });

    // Make connection to the database
    con.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");
    });

    // Closing the Database connection
    con.end(function(err) {
    if (err) throw err;
    console.log("Closed the active database connection!");
    });

No comments:

Post a Comment