Current location - Quotes Website - Collection of slogans - Idea creates nodejs to connect mysql.
Idea creates nodejs to connect mysql.
Create a Node.js project to connect MySQL database in IDEA, and you can follow the following steps:

Open IDEA and select "Create New Project" to create a new Node.js project.

Open the terminal in the new project directory and install mysql module with npm command. The command is as follows:

Rigid cast iron

Copy code

Npm install mysql-save

Create a file named index.js in the root directory of the project, and write the following code:

Java description language

Copy code

const MySQL = require(' MySQL ');

//Create a connection object

const connection = MySQL . create connection({

Host: 'localhost',//Database server address

User:' root',//database user name

Password:' password',//database password

Database: Test.//Database name

Port: 3306 // Database port number, the default is 3306.

});

//Connect to the database

connection . connect();

//Execute SQL query statement

Connection.query ('select * from user', function (error, result, field) {

If (error) throws an error;

Console.log ('the solution is:', the result);

});

//Close the connection

connection . end();

The host, user, password, database and other parameters need to be set as the actual database connection information.

Run the index.js file in the terminal with the following command:

Copy code

Node index.js

In this way, you can connect to MySQL database and query the data in the specified table.

The above is a simple example of connecting mysql database with Node.js If you need more complicated operations, you can refer to the documentation of MySQL module to learn more about API and usage.