0% found this document useful (0 votes)
22 views2 pages

Database Connection and Setup Guide

The document is a Node.js script that connects to a MySQL database named 'student'. It creates the database if it doesn't exist and sets up a 'users' table with fields for id, username, email, and password. Error handling is included for both the database connection and the creation of the database and table.

Uploaded by

diorwebsite2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Database Connection and Setup Guide

The document is a Node.js script that connects to a MySQL database named 'student'. It creates the database if it doesn't exist and sets up a 'users' table with fields for id, username, email, and password. Error handling is included for both the database connection and the creation of the database and table.

Uploaded by

diorwebsite2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// Import required modules

const express = require('express');


const mysql = require('mysql2');
const app = express();

const
connection=[Link]({
host:'localhost',
user:'root',
password:'12345678@N',
database:'student'
});
// Establish the connection
[Link]((err) => {
if (err) {
[Link]('Error connecting
to the database:', [Link]);
return;
}
[Link]('Connected to the database.');
});

//create database calledTeacher


const createDatabaseQuery = 'CREATE DATABASE IF NOT EXISTS Student';
[Link](createDatabaseQuery, (err) => {
if (err) {
[Link]('Error creating the database:', [Link]);
return;
}
[Link]('Database created (or already exists).');
});

const createTableQuery = `
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) not null
)
`;
[Link](createTableQuery, (err) => {
if (err) {
[Link]('Error creating the table:', [Link]);
return;
}
[Link]('Table created (or already exists).');
});

You might also like