{"id":1233,"date":"2017-06-21T16:40:56","date_gmt":"2017-06-21T16:40:56","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1233"},"modified":"2024-09-04T15:38:35","modified_gmt":"2024-09-04T15:38:35","slug":"write-a-jdbc-program-to-connect-mysql-database","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-jdbc-program-to-connect-mysql-database\/","title":{"rendered":"Write a JDBC program to connect MySQL Database?"},"content":{"rendered":"<h1>Write a JDBC program to connect MySQL Database?<\/h1>\n<p>JDBC stands for Java DataBase Connectivity. It is an API for Java to connect to various databases. Before writing the program to connect to MySQL database, we will do some initial setup in the back-end.<\/p>\n<h2>JDBC API<\/h2>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-21622\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JDBC-API-TestingDocs.jpeg\" alt=\"JDBC API TestingDocs\" width=\"1616\" height=\"599\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JDBC-API-TestingDocs.jpeg 1616w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JDBC-API-TestingDocs-300x111.jpeg 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JDBC-API-TestingDocs-1024x380.jpeg 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JDBC-API-TestingDocs-768x285.jpeg 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JDBC-API-TestingDocs-1536x569.jpeg 1536w\" sizes=\"auto, (max-width: 1616px) 100vw, 1616px\" \/><\/p>\n<h3><\/h3>\n<h3><strong>MySQL Database Setup<\/strong><\/h3>\n<p>We will create a sample database and an employee table in the back-end MySQL database.<\/p>\n<p>Connect to MySQL and Create database command as shown below:<\/p>\n<p>CREATE DATABASE testingdocs;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-551\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Create-Database-MySQL.jpeg\" alt=\"Create Database MySQL\" width=\"1361\" height=\"635\" title=\"\"><\/p>\n<h3><strong>Table creation:<\/strong><\/h3>\n<pre>CREATE TABLE IF NOT EXISTS employee (\r\n emp_id INT(10) NOT NULL AUTO_INCREMENT,\r\n name VARCHAR(45) DEFAULT NULL,\r\n salary INT(10) DEFAULT NULL,\r\n dept VARCHAR(100) DEFAULT NULL,\r\n PRIMARY KEY (emp_id)\r\n) ENGINE=InnoDB<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-552\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Create-Table-MySQL.jpeg\" alt=\"Create Table MySQL\" width=\"1358\" height=\"670\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>Populate the table with sample data as shown in the picture.<\/p>\n<p>INSERT INTO employee VALUES(2,&#8221;Surendra Kumar&#8221;,10000,&#8221;Quality Assurance&#8221;);<br \/>\nINSERT INTO employee VALUES(2,&#8221;Regan M&#8221;,18000,&#8221;Product Development&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-553\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Insert-into-Table-MySQL.jpeg\" alt=\"Insert into Table MySQL\" width=\"1365\" height=\"678\" title=\"\"><\/p>\n<h3><strong>MySQL Connector\/J Maven dependency<\/strong><\/h3>\n<p>To connect to MySQL database, I have used\u00a0 MySQL Connector\/J. It is a JDBC Type 4 driver for communicating with MySQL servers. Maven dependency of the connector version 6.xx is below. Place this dependency in your project pom.xml and update your project.<\/p>\n<pre><\/pre>\n<pre>&lt;!-- https:\/\/mvnrepository.com\/artifact\/mysql\/mysql-connector-java --&gt; \r\n\r\n&lt;dependency&gt;\r\n&lt;groupId&gt;mysql&lt;\/groupId&gt;\r\n&lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\r\n&lt;version&gt;6.0.6&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Building the database url for MySQL is as follows:<\/p>\n<p>jdbc:mysql:\/\/&lt;domain_where_mysql_server_runs&gt;\/&lt;database_name_to_connect&gt;<\/p>\n<p>or<\/p>\n<p>jdbc:mysql:\/\/&lt;HOST&gt;:&lt;PORT&gt;\/&lt;database_name_to_connect&gt;<\/p>\n<p>example:<\/p>\n<p>jdbc:mysql:\/\/127.0.0.1:3306\/&lt;database_name_to_connect&gt;<\/p>\n<h2><strong>Retrieve data from MySQL<\/strong><\/h2>\n<pre><\/pre>\n<pre>import java.sql.*;\r\n\r\npublic class JDBCMySQLExample {\r\n    public static void main(String args[]){\r\n        String dbEmpID;\r\n        String dbEmpName;\r\n        String dbSalary;\r\n        String dbMySQLUrl = \"jdbc:mysql:\/\/localhost\/testingdocs?user=root&amp;amp;password=\";\r\n        String sqlQuery = \"SELECT emp_id , name , salary FROM employee \";\r\n        try {\r\n            Connection con = DriverManager.getConnection (dbMySQLUrl);\r\n            Statement stmt = con.createStatement();\r\n            ResultSet rs = stmt.executeQuery(sqlQuery);\r\n\r\n            while (rs.next()) {\r\n                dbEmpID = rs.getString(1);\r\n                dbEmpName = rs.getString(2);\r\n                dbSalary = rs.getString(3);\r\n                System.out.println(\"Employee ID :\"+ dbEmpID);\r\n                System.out.println(\"Employee Name :\"+ dbEmpName);\r\n                System.out.println(\"Employee Salary :\"+ dbSalary);\r\n                System.out.println(\"***************\");\r\n            }\r\n            con.close();\r\n        }\r\n        catch(SQLException e) {\r\n            e.printStackTrace();\r\n        }\r\n\r\n    }\r\n\r\n}<\/pre>\n<pre><\/pre>\n<p>Run the program to display employee name and salary from the DB.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-554\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JDBC-Program-to-connect-to-MySQL-Database.jpeg\" alt=\"MySQL Database\" width=\"1365\" height=\"722\" title=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a JDBC program to connect MySQL Database? JDBC stands for Java DataBase Connectivity. It is an API for Java to connect to various databases. Before writing the program to connect to MySQL database, we will do some initial setup in the back-end. JDBC API &nbsp; MySQL Database Setup We will create a sample database [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-1233","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1233"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":24183,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1233\/revisions\/24183"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}