使用 Java JDBC 对 MySQL 进行“count”查询,其返回类型是什么?
Count 的返回类型是 long。Java 语句如下
rs.next();
long result= rs.getLong("anyAliasName");首先,在示例数据库 test3 中创建一个包含一些记录的表。创建表的查询如下
mysql> create table CountDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.60 sec)
使用 insert 命令在表中插入一些记录。
查询如下
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)使用 select 语句显示表中的所有记录。
查询如下
mysql> select *from CountDemo;
以下为输出
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | David | +----+-------+ 4 rows in set (0.00 sec)
以下是使用 Java JDBC 对 MySQL 进行“count”查询的 Java 代码返回类型
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://: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();
}
}
}以下为输出
Total Count=4 Here is the snapshot of the output:

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP