{"id":61053,"date":"2018-11-09T11:00:05","date_gmt":"2018-11-09T09:00:05","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=61053"},"modified":"2018-11-07T16:29:48","modified_gmt":"2018-11-07T14:29:48","slug":"mysql-nodejs-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/","title":{"rendered":"MySQL NodeJS Example"},"content":{"rendered":"<h2 id=\"introduction\">1. Introduction<\/h2>\n<p>In this post, we feature a comprehensive Tutorial on integrating MySQL in a simple NodeJS application and execute basic CRUD operations with the same.\u00a0<a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noopener\">MySQL<\/a>\u00a0is one of the most used SQL databases in many of the world-enterprise applications due to its ability to manage transactions and the ACID-behaviour which is built into its core. One of the reasons MySQL is so popular is due to the easiness it provides for its usage and its related commands. In the open source web application framework LAMP (which consist of Linux, Apache, MySQL and PHP),\u00a0MySQL server is a central &amp; important component. The MySQL Database server is written using C and C++ which internally uses\u00a0a lexical analyser to parse and understand the SQL queries.<\/p>\n<p>For this lesson, we need to have a complete MySQL Server installation so that we can run examples for the NodeJS application we create. Please go through\u00a0the <a href=\"https:\/\/examples.javacodegeeks.com\/enterprise-java\/sql-enterprise-java\/mysql-server-tutorial\/\" target=\"_blank\" rel=\"noopener\">MySQL Server Tutorial<\/a> lesson to understand how installation can be done with some simple commands to get started with the MySQL operations in a NodeJS application.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#setup_nodejs\">2. Setting up NodeJS application<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#connect_mysql\">2.1 Connecting to MySQL from NodeJS<\/a><\/dt>\n<dt><a href=\"#close_connection\">2.2 Closing connection to MySQL<\/a><\/dt>\n<dt><a href=\"#connection_pooling\">2.3 Connection Pooling<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#make_table\">3. Creating Table in NodeJS<\/a><\/dt>\n<dt><a href=\"#insert_data\">4. Inserting Data in Table<\/a><\/dt>\n<dt><a href=\"#update_data\">5. Update Data in Table<\/a><\/dt>\n<dt><a href=\"#delete_data\">6. Delete Data from Table<\/a><\/dt>\n<dt><a href=\"#stored_proc\">7. Calling a Stored Procedure from NodeJS application<\/a><\/dt>\n<dt><a href=\"#conclusion\">8. MySQL NodeJS Example &#8211; Conclusion<\/a><\/dt>\n<dt><a href=\"#download\">9. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2 id=\"setup_nodejs\">2. Setting up NodeJS application<\/h2>\n<p>In this section, we will start by setting up a simple\u00a0NodeJS application\u00a0with important techniques to establish and close the connection with the server along with database connection pooling techniques which is very important to have in any production-grade application. In this lesson, we will make use of MySQL driver for NodeJS for communication between the two. Let us start by creating a new directory for our project:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>New Directory<\/em><\/span><\/p>\n<pre class=\"brush:bash\">mkdir jcg-node-mysql\r\n<\/pre>\n<p>Once we have made this new directory, we can move inside it with the following command:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Change Directory<\/em><\/span><\/p>\n<pre class=\"brush:bash\">cd jcg-node-mysql\r\n<\/pre>\n<p>Now, we can make this directory a NodeJS project by creating a <code>package.json<\/code> file inside it. If you want to understand the NodeJS installation process and how it works, deep dive into NodeJS, I recommend you download the <a href=\"https:\/\/www.javacodegeeks.com\/minibook\/building-web-apps-node-js\" target=\"_blank\" rel=\"noopener\">NodeJS minibook by JavaCodeGeeks<\/a>. Run the following command so that the directory is initialised as a NodeJS app and the corresponding <code>package.json<\/code> file is created in it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Initialise NodeJS<\/em><\/span><\/p>\n<pre class=\"brush:bash\">npm init\r\n<\/pre>\n<p>Once we run the above command, we will be presented with a series of questions which we can answer to finalise the creation of the JSON file. Here is what we get when we run the above command (we selected all the default options presented to us):<\/p>\n<p><figure id=\"attachment_61330\" aria-describedby=\"caption-attachment-61330\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-nodejs-init-1.png\"><img decoding=\"async\" class=\"wp-image-61330 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-nodejs-init-1.png\" alt=\"MySQL NodeJS - NodeJS Init\" width=\"820\" height=\"769\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-nodejs-init-1.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-nodejs-init-1-300x281.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-nodejs-init-1-768x720.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61330\" class=\"wp-caption-text\">NodeJS Init<\/figcaption><\/figure><\/p>\n<p>Now, here is the <code>package.json<\/code> file which we got once we created the above command:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>package.json<\/em><\/span><\/p>\n<pre class=\"brush:bash\">{\r\n  \"name\": \"jcg-node-mysql\",\r\n  \"version\": \"1.0.0\",\r\n  \"description\": \"NodeJS app for SystemCodeGeeks\",\r\n  \"main\": \"index.js\",\r\n  \"scripts\": {\r\n    \"start\": \"node server.js\"\r\n  },\r\n  \"dependencies\": {\r\n    \"express\": \"^4.16.1\"\r\n  },\r\n  \"author\": \"Shubham Aggarwal &lt;sbmaggarwal@gmail.com&gt;\",\r\n  \"license\": \"ISC\"\r\n}\r\n<\/pre>\n<p>If your <code>package.json<\/code> file doesn&#8217;t look like above, do not worry, you can copy from above and paste it in your <code>package.json<\/code> file. Now we can install mysql driver for NodeJS so that we can use MySQL packages in the app as well. Run the following command so that MySQL driver is installed:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Installed MySQL Driver for NodeJS<\/em><\/span><\/p>\n<pre class=\"brush:bash\">npm install mysql\r\n<\/pre>\n<p>Here is what we get when we run the above command:<\/p>\n<p><figure id=\"attachment_61162\" aria-describedby=\"caption-attachment-61162\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-mysql-driver-nodejs.png\"><img decoding=\"async\" class=\"wp-image-61162 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-mysql-driver-nodejs.png\" alt=\"MySQL NodeJS - NodeJS MySQL Driver Install\" width=\"820\" height=\"273\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-mysql-driver-nodejs.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-mysql-driver-nodejs-300x100.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-mysql-driver-nodejs-768x256.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61162\" class=\"wp-caption-text\">NodeJS MySQL Driver Install<\/figcaption><\/figure><\/p>\n<p>Now, <code>package.json<\/code> file would have changed to the following with an extra MySQL dependency:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>package.json<\/em><\/span><\/p>\n<pre class=\"brush:bash\">{\r\n  \"name\": \"jcg-node-mysql\",\r\n  \"version\": \"1.0.0\",\r\n  \"description\": \"NodeJS app for SystemCodeGeeks\",\r\n  \"main\": \"index.js\",\r\n  \"scripts\": {\r\n    \"start\": \"node server.js\"\r\n  },\r\n  \"dependencies\": {\r\n    \"express\": \"^4.16.1\",\r\n    \"mysql\": \"^2.16.0\"\r\n  },\r\n  \"author\": \"Shubham Aggarwal &lt;sbmaggarwal@gmail.com&gt;\",\r\n  \"license\": \"ISC\"\r\n}\r\n<\/pre>\n<p>There is just one more thing which we need to do to start working on our app. We need to create a new database which will be used in the NodeJS application we create. Open MySQL shell and run the following command to create a new database <code>jcg_schema_nodejs<\/code> which we will use:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Create Database<\/em><\/span><\/p>\n<pre class=\"brush:bash\">CREATE SCHEMA jcg_schema_nodejs;\r\n<\/pre>\n<p>Once this is done, we can start connecting to the <a href=\"https:\/\/examples.javacodegeeks.com\/enterprise-java\/sql-enterprise-java\/mysql-server-tutorial\/\" target=\"_blank\" rel=\"noopener\">MySQL Server<\/a>.<\/p>\n<h3 id=\"connect_mysql\">2.1 Connecting to MySQL from NodeJS<\/h3>\n<p>There are multiple ways with which we can connect our application to the MySQL server on the local machine. For this, we create a new file known as <code>database.js<\/code> in the project home directory and add the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Import MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let mysql = require('mysql');\r\n<\/pre>\n<p>Now, once we have imported the MySQL into our file, we can connect to it by providing the connection parameters:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Connecting to MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let connection = mysql.createConnection({\r\n    host: 'localhost',\r\n    user: 'root',\r\n    password: 'qweRTY1!',\r\n    database: 'jcg_schema_nodejs'\r\n});\r\n<\/pre>\n<p>We only created a simple JSON and passed it to the <code>createConnection<\/code> function which accept credentials for connection and returns a brand new connection object for MySQL which we can use later to execute queries. Now, even though we have a connection object but it doesn&#8217;t actually represent a connection unless we call <code>connect<\/code> on it which will actually create a connection which will be persisted across the lifecycle of our program:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Connecting to MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">connection.connect(function(err) {\r\n  if (err) {\r\n    return console.error('error: ' + err.message);\r\n  }\r\n \r\n  console.log('Connected to the SystemCodeGeeks MySQL server.');\r\n});\r\n<\/pre>\n<p>If the user credentials are correct, the appropriate message will be printed to the console. Let us check the same by running the following command:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run Node app<\/em><\/span><\/p>\n<pre class=\"brush:bash\">node database.js\r\n<\/pre>\n<p>We will see the following output once our application is able to connect to the MySQL server:<\/p>\n<p><figure id=\"attachment_61219\" aria-describedby=\"caption-attachment-61219\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs.png\"><img decoding=\"async\" class=\"wp-image-61219 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs.png\" alt=\"MySQL NodeJS - NodeJS app connected to MySQL\" width=\"820\" height=\"315\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs-300x115.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs-768x295.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61219\" class=\"wp-caption-text\">NodeJS app connected to MySQL<\/figcaption><\/figure><\/p>\n<h3 id=\"close_connection\">2.2 Closing connection to MySQL<\/h3>\n<p>Now that we have an open MySQL connection, we will look at another simple NodeJS which can be used to close the MySQL connection in our app. We just need to call a single destroy function in our app to close the database connection, as shown in the following code snippet:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run Node app<\/em><\/span><\/p>\n<pre class=\"brush:bash\">connection.end(function(err) {\r\n    if (err) {\r\n      return console.log('error:' + err.message);\r\n    }\r\n    console.log('Database connection is closing.');\r\n    connection.destroy();\r\n    console.log('Database connection is closed.');\r\n  });\r\n<\/pre>\n<p>Let us run our NodeJS app again to see what happens:<\/p>\n<p><figure id=\"attachment_61221\" aria-describedby=\"caption-attachment-61221\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs-close.png\"><img decoding=\"async\" class=\"wp-image-61221 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs-close.png\" alt=\"MySQL NodeJS - Close MySQL connection\" width=\"820\" height=\"354\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs-close.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs-close-300x130.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-connect-nodejs-close-768x332.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61221\" class=\"wp-caption-text\">Close MySQL connection<\/figcaption><\/figure><\/p>\n<p>Please note that once a connection the MySQL server is closed, it cannot be used again for any queries or prepared statements. If we try to do so, we will get an error.<\/p>\n<h3 id=\"connection_pooling\">2.3 Connection Pooling<\/h3>\n<p>In any application, once we focus on database connection pooling for productionisation of our application, it makes a huge difference because <strong>database connections are expensive<\/strong> to make and keep. The implementation for database connection pooling is very simple and we just need to pass one more parameter when we provide details for connection to be made:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Connecting to MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let connection = mysql.createConnection({\r\n    connectionLimit: 10,\r\n    host: 'localhost',\r\n    user: 'root',\r\n    password: 'qweRTY1!',\r\n    database: 'jcg_schema_nodejs'\r\n});\r\n<\/pre>\n<p>The above code snippet makes sure that the database connection count never exceeds 10 in the application. Whenever we need to execute a query, we can call a single function to get a connection:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Get connection from pool<\/em><\/span><\/p>\n<pre class=\"brush:bash\">pool.getConnection(function(err, connection) {\r\n  \/\/ execute query here\r\n  \r\n});\r\n<\/pre>\n<p>This way, we get a connection from the pool and simply release the connection once we are done executing any query so that connection can be made available in the pool again:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Release connection to the pool<\/em><\/span><\/p>\n<pre class=\"brush:bash\">pool.getConnection(function(err, connection) {\r\n  \/\/ execute query here\r\n  connnection.release();\r\n});\r\n<\/pre>\n<p>Once we call the <code>release<\/code> function, we release the connection to be made available in the pool again.<\/p>\n<p>Finally, note that the connection pool will create connections lazily, which means that even when we set the connection limit to be 10, if we are using only 2 connections at a time, the pool will not create other 8 connections. Each connection will be made only when it is actually needed in the app.<\/p>\n<h2 id=\"make_table\">3. Creating Table in NodeJS<\/h2>\n<p>Now we are ready to create some tables from our application directly once we have made a connection to the MySQL server in the NodeJS app we created in the last section.<\/p>\n<p>For this, we need to create an SQL statement which will be executed each time the application is run. Please note that we don&#8217;t want to create a new table each time we run the app because that will raise an error. We only want to create a new table if it doesn&#8217;t exist already. Here is the SQL statement we will make use for the same:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Make new Table<\/em><\/span><\/p>\n<pre class=\"brush:bash\">CREATE TABLE IF NOT EXISTS Posts (\r\n    id INT PRIMARY KEY,\r\n    title VARCHAR(64) NOT NULL,\r\n    author_id INT NOT NULL\r\n);\r\n<\/pre>\n<p>Let us embed this SQL statement in the code so that is executed once a connection is open to the database:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Create new Table in code<\/em><\/span><\/p>\n<pre class=\"brush:bash\">\/\/ connect to the MySQL server\r\nconnection.connect(function(err) {\r\n    if (err) {\r\n      return console.error('error: ' + err.message);\r\n    }\r\n   \r\n    let createPosts = `CREATE TABLE IF NOT EXISTS Posts (\r\n                            id INT PRIMARY KEY,\r\n                            title VARCHAR(64) NOT NULL,\r\n                            author_id INT NOT NULL\r\n                        )`;\r\n   \r\n    connection.query(createPosts, function(err, results, fields) {\r\n      if (err) {\r\n        console.log(err.message);\r\n      }\r\n    });\r\n   \r\n    connection.end(function(err) {\r\n      if (err) {\r\n        return console.log(err.message);\r\n      }\r\n    });\r\n  });\r\n<\/pre>\n<p>With the above code, we are creating a new table where our data will be stored. The query function takes input as one parameters which are explained here and also provide a single callback function whose parameters are also described here:<\/p>\n<ol>\n<li>createPosts: This is the SQL statement which will be executed once this function is executed in NodeJS application.<\/li>\n<li>err: This contains a list of errors which are obtained if the query we passed raised an error.<\/li>\n<li>results: This signifies the result we obtained if the passed query did not raise any error<\/li>\n<li>fields: This field contains results fields information. if there are any<\/li>\n<\/ol>\n<p>Now that our code is ready, we can execute it again. Once we run the code, we will see the following output when we check what all tables exist in our database:<\/p>\n<p><figure id=\"attachment_61331\" aria-describedby=\"caption-attachment-61331\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-create-table-1.png\"><img decoding=\"async\" class=\"wp-image-61331 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-create-table-1.png\" alt=\"MySQL NodeJS - Create Table\" width=\"820\" height=\"436\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-create-table-1.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-create-table-1-300x160.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-create-table-1-768x408.png 768w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-create-table-1-620x330.png 620w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61331\" class=\"wp-caption-text\">Create Table<\/figcaption><\/figure><\/p>\n<h2 id=\"insert_data\">4. Inserting Data in Table<\/h2>\n<p>In the final section of our tutorial, we will show how we can insert data into our table using the NodeJS application we created. We will follow 3 steps to do:<\/p>\n<ol>\n<li>Create a new database connection<\/li>\n<li>Write and Execute an Insert statement in SQL<\/li>\n<li>Close or release the database connection<\/li>\n<\/ol>\n<p>Let&#8217;s get started with these steps. We will create a new file and call it <code>insert.js<\/code>. This file will contain code used to insert new data in our table we created in the last section. We need to start with our first step and define the connection parameters in this file as well:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Connection to the MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let mysql = require('mysql');\r\n\r\nlet connection = mysql.createConnection({\r\n    connectionLimit: 10,\r\n    host: 'localhost',\r\n    user: 'root',\r\n    password: 'root',\r\n    database: 'jcg_schema'\r\n});\r\n<\/pre>\n<p>Now, we need to write an SQL statement which will be an <code>INSERT<\/code> statement for the table <code>Posts<\/code> we defined:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Defining INSERT statement<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let insertQuery = \"INSERT INTO Posts VALUES (1, 'Introduction to Python', 23)\";\r\n<\/pre>\n<p>Finally, we can insert the data and close the database connection:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Connection to the MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">\/\/ execute the insert query\r\nconnection.query(insertQuery);\r\nconnection.end();\r\n<\/pre>\n<p>We can now run the file now:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run insert file<\/em><\/span><\/p>\n<pre class=\"brush:bash\">node insert.js\r\n<\/pre>\n<p>We will see the following data in our table once we have run the above file:<\/p>\n<p><figure id=\"attachment_61223\" aria-describedby=\"caption-attachment-61223\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-insert-statement.png\"><img decoding=\"async\" class=\"wp-image-61223 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-insert-statement.png\" alt=\"MySQL NodeJS - NodeJS Insert statement\" width=\"820\" height=\"732\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-insert-statement.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-insert-statement-300x268.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/nodejs-insert-statement-768x686.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61223\" class=\"wp-caption-text\">NodeJS Insert statement<\/figcaption><\/figure><\/p>\n<p>We were able to insert data into our table using the NodeJS application. Please note that we can perform any database operation just like what we did above, may it be a Delete or Update operation. We can even work on Express view which will help us to view all the data we have in our table into a nice UI view.<\/p>\n<h2 id=\"update_data\">5. Update Data in Table<\/h2>\n<p>Now that we have some data in our database after the previous section code is complete, we can move to perform some update operations on that data. To make this more interesting, we inserted some more data in our Posts table and now, the data looks like:<\/p>\n<p><figure id=\"attachment_61321\" aria-describedby=\"caption-attachment-61321\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-current-data.png\"><img decoding=\"async\" class=\"wp-image-61321 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-current-data.png\" alt=\"MySQL NodeJS - Current data in MySQL\" width=\"820\" height=\"468\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-current-data.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-current-data-300x171.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-current-data-768x438.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61321\" class=\"wp-caption-text\">Current data in MySQL<\/figcaption><\/figure><\/p>\n<p>We will follow 3 steps to update data in the Posts table:<\/p>\n<ol>\n<li>Create a new database connection<\/li>\n<li>Write and Execute an Update statement in SQL<\/li>\n<li>Close or release the database connection<\/li>\n<\/ol>\n<p>Let&#8217;s get started with these steps. We will create a new file and call it <code>update.js<\/code>. This file will contain code used to update existing data in our table we created in the last section. We need to start with our first step and define the connection parameters in this file as well:<br \/>\n<span style=\"text-decoration: underline;\"><em>Connection to the MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let mysql = require('mysql');\r\n\r\nlet connection = mysql.createConnection({\r\n    connectionLimit: 10,\r\n    host: 'localhost',\r\n    user: 'root',\r\n    password: 'root',\r\n    database: 'jcg_schema'\r\n});\r\n<\/pre>\n<p>Now, we need to write an SQL statement which will be an <code>UPDATE<\/code> statement for the table <code>Posts<\/code> we defined. We will update the Author ID for posts whose author ID was previously 1 and change the same to 19 (or any integer value):<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Defining UPDATE statement<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let updateQuery = \"UPDATE Posts SET author_id = ? WHERE author_id = ?\";\r\n<\/pre>\n<p>We see that we are not setting the update and where values in the query directly because that is a security vulnerability we can avoid just by using Prepared statements. We can define what data needs to be substituted with the following definition:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Preparing statements<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let author_ids = [19, 1];\r\n<\/pre>\n<p>Note that the variables will be substituted in the order they appear in the SQL statement. Finally, we can update the data and close the database connection:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Connection to the MySQL<\/em><\/span><\/p>\n<pre class=\"brush:bash\">connection.query(updateQuery, author_ids, (error, results, fields) =&gt; {\r\n  if (error){\r\n    return console.error(error.message);\r\n  }\r\n  console.log('Rows affected:', results.affectedRows);\r\n});\r\n<\/pre>\n<p>We can now run the file now:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run update file<\/em><\/span><\/p>\n<pre class=\"brush:bash\">node update.js\r\n<\/pre>\n<p>We will see the following data in our table once we have run the above file:<\/p>\n<p><figure id=\"attachment_61322\" aria-describedby=\"caption-attachment-61322\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-updated-data.png\"><img decoding=\"async\" class=\"wp-image-61322 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-updated-data.png\" alt=\"MySQL NodeJS - Update data\" width=\"820\" height=\"735\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-updated-data.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-updated-data-300x269.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/mysql-updated-data-768x688.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61322\" class=\"wp-caption-text\">Update data<\/figcaption><\/figure><\/p>\n<p>Please note that <a href=\"https:\/\/www.javacodegeeks.com\/2012\/11\/sql-injection-in-java-application.html\" target=\"_blank\" rel=\"noopener\">SQL Injection<\/a> is a pretty common vulnerability we need to avoid in our application. Finally, if you are sill interested in exploring more, read <a href=\"https:\/\/www.javacodegeeks.com\/2013\/12\/using-sql-injection-vulnerabilities-to-dump-your-database.html\" target=\"_blank\" rel=\"noopener\">Using SQL Injection Vulnerabilities to Dump Your Database<\/a> for much more information and knowledge.<\/p>\n<h2 id=\"delete_data\">6. Delete Data from Table<\/h2>\n<p>One of the most important operation in SQL is DELETE operation. This is what we will be demonstrating in this section by executing one of the DELETE statement in the application. Note that all of the other steps remains the same like making a connection, executing the query and closing the connection after use, so we won&#8217;t be redundant here and show only the queries we execute here:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run update file<\/em><\/span><\/p>\n<pre class=\"brush:bash\">\/\/ DELETE statement\r\nlet deleteQuery = `DELETE FROM Posts WHERE author_id = ?`;\r\n \r\n\/\/ delete the post with id 19\r\nconnection.query(deleteQuery, 1, (error, results, fields) =&gt; {\r\n  if (error)\r\n    return console.error(error.message);\r\n \r\n  console.log('Deleted Post(s):', results.affectedRows);\r\n});\r\n \r\nconnection.end();\r\n<\/pre>\n<p>This code will also print the number of rows which were deleted once this query is executed and it is really helpful to understand if any data was affected with the provided query.<\/p>\n<h2 id=\"stored_proc\">7. Calling a Stored Procedure from NodeJS application<\/h2>\n<p>It is possible to run a stored procedure from inside of a NodeJS application. A stored procedure is a very good way to execute a number of SQL statements when you want to do a complex operation which needs more than a couple of statements needed to be executed sequentially. We can consider a stored procedure as a program written in SQL.<\/p>\n<p>Again, the procedure to run a stored procedure is same as well, make an SQL connection (or pick one from the pool), prepare and execute the stored procedure and finally, close the connection (or release it into the pool). Read more about stored procedures in <a href=\"https:\/\/www.javacodegeeks.com\/2018\/06\/mysql-stored-procedure-tutorial.html\" target=\"_blank\" rel=\"noopener\">MySQL Stored Procedure Tutorial<\/a> and also how to use them in a Java application with <a href=\"https:\/\/www.javacodegeeks.com\/2013\/09\/java-stored-procedures-in-java-db.html\" target=\"_blank\" rel=\"noopener\">Java Stored Procedures in Java DB<\/a>.<\/p>\n<p>We can define a simple SQL statement to call a stored procedure which you have defined in the SQL DB as:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Call Stored procedure SQL Statement<\/em><\/span><\/p>\n<pre class=\"brush:bash\">let sql = `CALL findPosts(?)`;\r\n<\/pre>\n<p>Here, we define a stored procedure defined as <code>findPosts<\/code> in SQL DB. Please note that this query can be executed just like any other SQL query in which we could have passed parameters and this will be treated as prepared statements. I do not highly recommend using SQL procedures personally due to some of the limitations they present:<\/p>\n<ul>\n<li>If we want to make sure that database integrity and consistency is maintained, Stored Procedures can become quite long and coupled with application logic and still can\u2019t replace all validations which are needed at an application level.<\/li>\n<li>Stored Procedures work completely behind the scene. For this reason, it is not possible to figure out everything which happens at the database layer.<\/li>\n<li>If there are too many Stored Procedures at the database level, they can block database transactions which are originated from application level until a procedure is complete.<\/li>\n<li>It is possible that we want to trigger an operation only when an event happens 10 times or 100 times. This is not possible with SQL Stored Procedures as they are stateless in themselves and cannot be invoked conditionally inside of them.<\/li>\n<\/ul>\n<h2 id=\"conclusion\">8. MySQL NodeJS Example &#8211; Conclusion<\/h2>\n<p>In this lesson, we looked at a very simple yet effective example of setting up a connection to a MySQL server from a NodeJS application. We started with important techniques to establish and close the connection with the server along with database connection pooling techniques which is very important to have in any production-grade application based on any framework and not just NodeJS. We encourage you to study more about NodeJS and MySQL to run more complex examples which involve Prepared statements and result sets. One of the more important things which can be done inside a NodeJS application is calling stored procedures, <a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/mysql-triggers-tutorial\/\" target=\"_blank\" rel=\"noopener\">define triggers<\/a> and <a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/mysql-full-text-search-tutorial\/\" target=\"_blank\" rel=\"noopener\">adding full-text search capabilities<\/a> to the application.<\/p>\n<p>MySQL Stored procedures are a very powerful tool with very high performance (usually) but the bad thing is that they can grow very large very easily and quickly once your application starts to grow. The main database consistency should be managed at the application level wherever and whenever possible. Still, MySQL Stored procedures make a very good companion if you are a database administrator and just want to make things consistent without relying on too much code because after all, the performance of a Stored procedures is very high because database operations are very fast on the database server itself. Don\u2019t forget to check the examples of using Java with\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/enterprise-java\/sql-enterprise-java\/jdbc-best-practices-tutorial\/\" target=\"_blank\" rel=\"noopener\">SQL: JDBC Best Practices Tutorial<\/a>\u00a0and\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/resultset-sql\/java-jdbc-resultset-example\/\" target=\"_blank\" rel=\"noopener\">Java JDBC ResultSet Example<\/a>. These lessons explain how to effectively use SQL commands with Java &amp;\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/enterprise-java\/sql-enterprise-java\/jdbc-driver-types-example\/\" target=\"_blank\" rel=\"noopener\">JDBC drivers<\/a>. There are many more examples present on MySQL which can be studied for a deeper understanding of the database. Finally, do read about some of <a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/mysql-most-popular-functions-tutorial\/\" target=\"_blank\" rel=\"noopener\">the most popular MySQL functions<\/a> which you will find interesting and useful in your daily life when you deal with MySQL. There are many more examples present on MySQL which can be studied for a deeper understanding of the database.<\/p>\n<h2 id=\"download\">9. Download the Source Code<\/h2>\n<p>This was an example of NodeJS Framework and MySQL database.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <strong><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/jcg-node-mysql.zip\" target=\"_blank\" rel=\"noopener\">NodeJS MySQL Example<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction In this post, we feature a comprehensive Tutorial on integrating MySQL in a simple NodeJS application and execute basic CRUD operations with the same.\u00a0MySQL\u00a0is one of the most used SQL databases in many of the world-enterprise applications due to its ability to manage transactions and the ACID-behaviour which is built into its core. &hellip;<\/p>\n","protected":false},"author":154,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[1259,647,1055],"class_list":["post-61053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","tag-nodejs","tag-mysql","tag-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MySQL NodeJS Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn more about MySQL? Then check out our detailed example on MySQL NodeJS!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL NodeJS Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about MySQL? Then check out our detailed example on MySQL NodeJS!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-09T09:00:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Shubham Aggarwal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sbmaggarwal\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shubham Aggarwal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/\"},\"author\":{\"name\":\"Shubham Aggarwal\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2\"},\"headline\":\"MySQL NodeJS Example\",\"datePublished\":\"2018-11-09T09:00:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/\"},\"wordCount\":2807,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"#nodejs\",\"mysql\",\"sql\"],\"articleSection\":[\"sql\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/\",\"name\":\"MySQL NodeJS Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2018-11-09T09:00:05+00:00\",\"description\":\"Interested to learn more about MySQL? Then check out our detailed example on MySQL NodeJS!\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"sql\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/sql\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"MySQL NodeJS Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2\",\"name\":\"Shubham Aggarwal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg\",\"caption\":\"Shubham Aggarwal\"},\"description\":\"Shubham is a Java Backend and Data Analytics Engineer with more than 3 years of experience in building quality products with Spring Boot, MongoDB, Elasticsearch, MySQL, Docker, AWS, Git, PrestoDB tools and I have a deep knowledge and passion towards analytics, Micro-service based architecture, design patterns, antipatterns and software design thinking.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sbmaggarwal\/\",\"https:\/\/x.com\/sbmaggarwal\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/shubham-aggarwal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL NodeJS Example - Java Code Geeks","description":"Interested to learn more about MySQL? Then check out our detailed example on MySQL NodeJS!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/","og_locale":"en_US","og_type":"article","og_title":"MySQL NodeJS Example - Java Code Geeks","og_description":"Interested to learn more about MySQL? Then check out our detailed example on MySQL NodeJS!","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-11-09T09:00:05+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Shubham Aggarwal","twitter_card":"summary_large_image","twitter_creator":"@sbmaggarwal","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Shubham Aggarwal","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/"},"author":{"name":"Shubham Aggarwal","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2"},"headline":"MySQL NodeJS Example","datePublished":"2018-11-09T09:00:05+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/"},"wordCount":2807,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["#nodejs","mysql","sql"],"articleSection":["sql"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/","name":"MySQL NodeJS Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2018-11-09T09:00:05+00:00","description":"Interested to learn more about MySQL? Then check out our detailed example on MySQL NodeJS!","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-nodejs-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"sql","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/sql\/"},{"@type":"ListItem","position":5,"name":"MySQL NodeJS Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2","name":"Shubham Aggarwal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg","caption":"Shubham Aggarwal"},"description":"Shubham is a Java Backend and Data Analytics Engineer with more than 3 years of experience in building quality products with Spring Boot, MongoDB, Elasticsearch, MySQL, Docker, AWS, Git, PrestoDB tools and I have a deep knowledge and passion towards analytics, Micro-service based architecture, design patterns, antipatterns and software design thinking.","sameAs":["https:\/\/www.linkedin.com\/in\/sbmaggarwal\/","https:\/\/x.com\/sbmaggarwal"],"url":"https:\/\/examples.javacodegeeks.com\/author\/shubham-aggarwal\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/61053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/154"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=61053"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/61053\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=61053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=61053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=61053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}