{"id":327,"date":"2025-09-08T16:52:01","date_gmt":"2025-09-08T16:52:01","guid":{"rendered":"https:\/\/iotnoob.com\/wordpress\/?p=327"},"modified":"2025-09-09T10:09:11","modified_gmt":"2025-09-09T10:09:11","slug":"connect-mysql-%e0%b9%83%e0%b8%99-java","status":"publish","type":"post","link":"https:\/\/iotnoob.com\/wordpress\/2025\/09\/08\/connect-mysql-%e0%b9%83%e0%b8%99-java\/","title":{"rendered":"Connect MySQL \u0e43\u0e19 Java"},"content":{"rendered":"\n<p>\u0e43\u0e19 Gradle \u0e43\u0e2b\u0e49 add library \u0e01\u0e48\u0e2d\u0e19<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">dependencies { implementation 'com.mysql:mysql-connector-j:8.0.33' \/\/ \u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e40\u0e1b\u0e25\u0e35\u0e48\u0e22\u0e19\u0e40\u0e1b\u0e47\u0e19\u0e40\u0e27\u0e2d\u0e23\u0e4c\u0e0a\u0e31\u0e19\u0e25\u0e48\u0e32\u0e2a\u0e38\u0e14\u0e44\u0e14\u0e49 }<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.sql.Statement;\n\npublic class BasicMysqlConnect {\n\n    public static void main(String[] args) {\n        \/\/ --- 1. \u0e01\u0e33\u0e2b\u0e19\u0e14\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d ---\n        String url = \"jdbc:mysql:\/\/localhost:3306\/your_database\"; \/\/ \u0e41\u0e01\u0e49\u0e44\u0e02 your_database \u0e40\u0e1b\u0e47\u0e19\u0e0a\u0e37\u0e48\u0e2d\u0e10\u0e32\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n        String user = \"your_username\"; \/\/ \u0e41\u0e01\u0e49\u0e44\u0e02\u0e40\u0e1b\u0e47\u0e19 username \u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n        String password = \"your_password\"; \/\/ \u0e41\u0e01\u0e49\u0e44\u0e02\u0e40\u0e1b\u0e47\u0e19 password \u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\n\n        \/\/ --- 2. \u0e43\u0e0a\u0e49 try-with-resources \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e08\u0e31\u0e14\u0e01\u0e32\u0e23\u0e01\u0e32\u0e23\u0e1b\u0e34\u0e14 Connection \u0e2d\u0e31\u0e15\u0e42\u0e19\u0e21\u0e31\u0e15\u0e34 ---\n        try (Connection connection = DriverManager.getConnection(url, user, password)) {\n            System.out.println(\"\u2705 \u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e10\u0e32\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25 MySQL \u0e2a\u0e33\u0e40\u0e23\u0e47\u0e08!\");\n\n            \/\/ --- 3. \u0e2a\u0e23\u0e49\u0e32\u0e07 Statement \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e2a\u0e48\u0e07\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07 SQL ---\n            Statement statement = connection.createStatement();\n            \n            \/\/ --- 4. \u0e2a\u0e23\u0e49\u0e32\u0e07\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07 SQL \u0e17\u0e35\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23 ---\n            String sqlQuery = \"SELECT id, name FROM users\"; \/\/ \u0e2a\u0e21\u0e21\u0e15\u0e34\u0e15\u0e32\u0e23\u0e32\u0e07\u0e0a\u0e37\u0e48\u0e2d users \u0e21\u0e35\u0e04\u0e2d\u0e25\u0e31\u0e21\u0e19\u0e4c id \u0e41\u0e25\u0e30 name\n            \n            \/\/ --- 5. \u0e2a\u0e31\u0e48\u0e07\u0e43\u0e2b\u0e49 SQL \u0e17\u0e33\u0e07\u0e32\u0e19\u0e41\u0e25\u0e30\u0e23\u0e31\u0e1a\u0e1c\u0e25\u0e25\u0e31\u0e1e\u0e18\u0e4c (ResultSet) ---\n            ResultSet resultSet = statement.executeQuery(sqlQuery);\n            System.out.println(\"\ud83d\udd0d \u0e1c\u0e25\u0e25\u0e31\u0e1e\u0e18\u0e4c\u0e08\u0e32\u0e01\u0e15\u0e32\u0e23\u0e32\u0e07 users:\");\n\n            \/\/ --- 6. \u0e27\u0e19\u0e25\u0e39\u0e1b\u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e41\u0e2a\u0e14\u0e07\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e17\u0e35\u0e25\u0e30\u0e41\u0e16\u0e27 ---\n            while (resultSet.next()) {\n                int id = resultSet.getInt(\"id\");\n                String name = resultSet.getString(\"name\");\n                System.out.println(\"ID: \" + id + \", Name: \" + name);\n            }\n\n        } catch (SQLException e) {\n            \/\/ --- 7. \u0e08\u0e31\u0e14\u0e01\u0e32\u0e23\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14\u0e17\u0e35\u0e48\u0e2d\u0e32\u0e08\u0e40\u0e01\u0e34\u0e14\u0e02\u0e36\u0e49\u0e19 ---\n            System.err.println(\"\u274c \u0e40\u0e01\u0e34\u0e14\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14\u0e43\u0e19\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e10\u0e32\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25!\");\n            e.printStackTrace();\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u0e40\u0e14\u0e35\u0e4b\u0e22\u0e27\u0e19\u0e35\u0e49 connect \u0e44\u0e14\u0e49\u0e04\u0e48\u0e2d\u0e19\u0e02\u0e49\u0e32\u0e07\u0e07\u0e48\u0e32\u0e22 \u0e44\u0e21\u0e48\u0e15\u0e49\u0e2d\u0e07 load class \u0e14\u0e49\u0e27\u0e22 Class.forName \u0e2d\u0e30\u0e44\u0e23\u0e2d\u0e35\u0e01\u0e41\u0e25\u0e49\u0e27<\/p>\n\n\n\n<p>String url = &#8220;jdbc:mysql:\/\/localhost:3306\/your_database&#8221;; <br>String user = &#8220;your_username&#8221;;<br>String password = &#8220;your_password&#8221;;<br>Connection connection = DriverManager.getConnection(url, user, password);<br>Statement statement = connection.createStatement();<br>String sqlQuery = &#8220;SELECT id, name FROM users&#8221;;<br>ResultSet resultSet = statement.executeQuery(sqlQuery);<br>while (resultSet.next()) {<br>    int id = resultSet.getInt(&#8220;id&#8221;);<br>    String name = resultSet.getString(&#8220;name&#8221;);<br>    System.out.println(&#8220;ID: &#8221; + id + &#8220;, Name: &#8221; + name);<br>}<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0e43\u0e19 Gradle \u0e43\u0e2b\u0e49 add library \u0e01\u0e48\u0e2d\u0e19 \u0e40\u0e14\u0e35\u0e4b\u0e22\u0e27\u0e19\u0e35\u0e49 connect \u0e44\u0e14\u0e49\u0e04\u0e48\u0e2d\u0e19\u0e02\u0e49\u0e32\u0e07\u0e07\u0e48\u0e32\u0e22 \u0e44\u0e21\u0e48\u0e15\u0e49\u0e2d\u0e07 load class \u0e14\u0e49\u0e27\u0e22 Class.forName \u0e2d\u0e30\u0e44\u0e23\u0e2d\u0e35\u0e01\u0e41\u0e25\u0e49\u0e27&hellip;<\/p>\n","protected":false},"author":1,"featured_media":340,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[],"class_list":["post-327","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-programming"],"_links":{"self":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/comments?post=327"}],"version-history":[{"count":1,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/327\/revisions"}],"predecessor-version":[{"id":328,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/327\/revisions\/328"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media\/340"}],"wp:attachment":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media?parent=327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/categories?post=327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/tags?post=327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}