{"id":21613,"date":"2016-12-10T12:56:32","date_gmt":"2016-12-10T12:56:32","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=21613"},"modified":"2024-09-04T07:39:34","modified_gmt":"2024-09-04T07:39:34","slug":"how-to-get-database-table-row-count-in-jdbc","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-get-database-table-row-count-in-jdbc\/","title":{"rendered":"How to Get Database Table Row Count in JDBC?"},"content":{"rendered":"<h1>How to Get Database Table Row Count in JDBC?<\/h1>\n<p>Let&#8217;s see how to get database table row count using JDBC. There are many ways to count the table rows. In this post, we will use the SQL count(*) query to count the table rows. We will use Oracle database and emp table with 5 rows.<\/p>\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<h2>SQL Query<\/h2>\n<p>SQL&gt; SELECT COUNT(*) FROM emp;<\/p>\n<p>COUNT(*)<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n5<\/p>\n<p>SQL&gt; SELECT COUNT(*) AS &#8220;Number of Rows in emp table&#8221; FROM emp;<\/p>\n<p>Number of Rows in emp table<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n5<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-21616\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Number-of-Rows-in-Database-Table.png\" alt=\"Number of Rows in Database Table\" width=\"1920\" height=\"1080\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Number-of-Rows-in-Database-Table.png 1920w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Number-of-Rows-in-Database-Table-300x169.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Number-of-Rows-in-Database-Table-1024x576.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Number-of-Rows-in-Database-Table-768x432.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Number-of-Rows-in-Database-Table-1536x864.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/h2>\n<h2>Sample JDBC Program<\/h2>\n<pre>public class TableRowCount {\r\n\tpublic static void main(String args[]) {\r\n\t\t\/\/ Connection descriptor variables\r\n\t\tString driverName = \"oracle.jdbc.driver.OracleDriver\";\r\n\t\tString hostName = \"localhost\";\r\n\t\tString portNumber = \"1521\";\r\n\t\tString serviceName = \"orclpdb.localdomain\";\r\n\t\tString username = \"scott\";\r\n\t\tString password = \"tiger\";\r\n\t\tString url = \"jdbc:oracle:thin:@\/\/\" + hostName + \":\" + portNumber + \"\/\" + serviceName;\r\n\t\t\/\/ Oracle SQL SELECT Query\r\n\t\tString sqlSelectQuery = \"SELECT COUNT(*) FROM emp\";\r\n\t\tConnection connection = null;\r\n\t\ttry {\r\n\t\t\t\/\/ Load JDBC Driver\r\n\t\t\tClass.forName(driverName);\r\n\r\n\t\t\t\/\/ Connection to the Oracle Database\r\n\t\t\tconnection = DriverManager.getConnection(url, username, password);\r\n\r\n\t\t\tStatement stmt = connection.createStatement();\r\n\t\t\tResultSet rs = stmt.executeQuery(sqlSelectQuery);\r\n\t\t\t\/\/ Print\r\n\t\t\tSystem.out.println(String.format(\"%-20s\", \"Number of Rows in Table\"));\r\n\t\t\twhile (rs.next()) {\r\n\t\t\t\tint rowCount = rs.getInt(1);\r\n\t\t\t\tSystem.out.println(String.format(\"%20s\", rowCount));\r\n\t\t\t}\r\n\t\t\t\/\/ Close Database Connection\r\n\t\t\tstmt.close();\r\n\t\t} catch (Exception exp) {\r\n\t\t\texp.printStackTrace();\r\n\t\t}\r\n\t}\r\n}\r\n\r\n<\/pre>\n<h2><\/h2>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Get Database Table Row Count in JDBC? Let&#8217;s see how to get database table row count using JDBC. There are many ways to count the table rows. In this post, we will use the SQL count(*) query to count the table rows. We will use Oracle database and emp table with 5 rows. [&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-21613","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\/21613","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=21613"}],"version-history":[{"count":11,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21613\/revisions"}],"predecessor-version":[{"id":24116,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21613\/revisions\/24116"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=21613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=21613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=21613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}