
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the return type of a “count” query against MySQL using Java JDBC?
The return type of count is long. The Java statement is as follows
rs.next(); long result= rs.getLong("anyAliasName");
First, create a table with some records in our sample database test3. The query to create a table is as follows
mysql> create table CountDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into CountDemo(Name) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into CountDemo(Name) values('Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into CountDemo(Name) values('Bob'); Query OK, 1 row affected (0.19 sec) mysql> insert into CountDemo(Name) values('David'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from CountDemo;
The following is the output
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | David | +----+-------+ 4 rows in set (0.00 sec)
Here is the Java code return type of a “count” query against MySQL using Java JDBC
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ReturnTypeOfCount { public static void main(String[] args) { Connection con=null; Statement st=null; ResultSet rs=null; try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?useSSL=false", "root","123456"); String yourQuery = "SELECT COUNT(*) AS totalCount FROM CountDemo"; st = con.createStatement(); rs = st.executeQuery(yourQuery); rs.next(); long result= rs.getLong("totalCount"); System.out.println("Total Count="+result); } catch(Exception e) { e.printStackTrace(); } } }
The following is the output
Total Count=4 Here is the snapshot of the output:
- Related Questions & Answers
- What is the difference between “lang” and “type” attributes in a script tag?
- Using “TYPE = InnoDB” in MySQL throws an exception?
- What is the return type of a Constructor in Java?
- In a MySQL schema, what is the meaning of “AUTO_INCREMENT=3”
- What is “SELECT TRUE” in MySQL?
- Which MySQL type is most suitable for “price” column?
- Maintaining order in MySQL “IN” query?
- MySQL query to split the string “Learn With Ease” and return the last word?
- What is the usage of “@” symbol in MySQL stored procedure?
- MySQL query error with a table named “order”?
- MySQL “order by” inside of “group by”? Is it possible?
- Can we use “LIKE concat()” in a MySQL query?
- Can we get records “Jone Deo” or “Deo Jone” with a single MySQL query?
- Using “not equal” in MySQL?
- “Toggle” query in MongoDB?
Advertisements