JDBC中如何使用不同的行方法获取表中的行数?
Java支持许多数据库,对于每个数据库,我们需要将它们各自的 jar 文件放置在构建路径中以进行 JDBC 连接。对于不同的数据库,导入不同的jar文件进行下面给出的连接,或者应该为特定的数据库添加它们的构建路径。
- 数据库类型
- SQL
- MySQL:mysql-connector-java-8.0.22
- PostgreSQL
- 甲骨文:ojdbc14.jar
- 微软 SQL 服务器
- 无SQL
- MongoDB:mongo-java-driver-3.12.7
- 大表
- Redis
- 进步
- 卡桑德拉
- 沙发数据库
- 乌鸦数据库
- SQL
说明: SQL 和 Oracle 数据库主要用于说明。这里考虑了 SQL 数据库。这里 Table_Name 是表名。在这里它将获取所有列并计算行数。
输入:表中现有数据如下图所示
- 使用的 SQL 服务器:sqljdbc4.jar
- 使用的 SQL 表
CREATE TABLE `studentsdetails` (
`id` int(6) unsigned NOT NULL,
`Name` varchar(50) NOT NULL,
`caste` varchar(10) NOT NULL,
`NeetMarks` int(11) NOT NULL,
`gender` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
方法:
创建查询的效率较低的方法
select count(*) from Table_Name;
创建查询的更有效方法
select count(1) from Table_Name;
此查询将采用第一列并计算行数。大多数情况下,主键是第一列,它足够理想,因为主键始终是唯一的并且不为空。Example 1 Example 2 It will give only one row as output containing a number of rows. Hence, ‘resultset’ will be kept as next itself. select * will bring the whole resultset and the cursor is forced to move last and finally ‘resultset.getRow()’ method will give the number of rows. It is more efficient It is comparatively less efficient
示例 1:通过从 'studentsdetails' 中选择 count(1) 来获取 JDBC 中表中的行数将提供结果为 5。
Java
/* Java Program to use different row methods
to get no of rows in a table in JDBC */
// Step 1: Importing database libraries
import java.sql.*;
// Only main class- GFG is shown
// connection class object is used
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Initially connection is assigned Null valued
Connection con = null;
ResultSet res = null;
// Try block to check exceptions
try {
/* Step 2: Load and register drivers or
relevant jars in build path of project */
// Here- 'mysql-connector-java-8.0.22'
// is used usinf Class.forNmae() method
Class.forName("com.mysql.cj.jdbc.Driver");
/* Step 3: Establish a connection using
DriverManager method */
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?serverTimezone=UTC",
"root", "");
// Try block to check exceptions
try {
Statement st = con.createStatement();
/* This query will take first column and
count the rows. As mostly, Primary key is
the first column, it is ideal enough as
Primary key is always unique and not
null */
/* Step 4: Create a statement */
/* Alias name is used as NumberOfRows
for COUNT(1) Moving the cursor to the
last row */
res = st.executeQuery(
"SELECT COUNT(1) as NumberOfRows FROM "
+ "studentsdetails");
/* Step 5: Execute the query */
res.next();
/* Step 6: Process the results */
System.out.println(
"MySQL Table - studentsdetails contains "
+ res.getInt("NumberOfRows") + " rows");
}
// Catch block to handle exceptions
catch (SQLException s) {
// Message to be displayed if SQLException
// occurs
System.out.println(
"SQL statement is not executed!");
}
}
catch (Exception e) {
/* Displaying line where exception occured using
method returning line number in code */
e.printStackTrace();
}
finally {
// Step 7: Closing the connection
res = null;
con = null;
}
}
}
Java
/* Step 1: Importing Database libraries */
import java.sql.*;
/* Only main class-GFG is shown
Connection class of JDBC is not shown.
Object of connection class is used */
public class GFG {
// Main driver method
public static void main(String[] args)
{
/* Objects are assigned null
before any execution */
// Connection class objects
Connection con = null;
ResultSet res = null;
// Try block to check exceptions
try {
/* Step 2: Load and register drivers
or relevant jars in build path of project */
// Driver used- 'mysql-connector-java-8.0.22'
// Loading and register drivers
// using Class.forname() method
Class.forName("com.mysql.cj.jdbc.Driver");
/* Step 3: Create a connection */
// using DriverManager
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?serverTimezone=UTC",
"root", "");
// Display message when connection
// is successfully established
System.out.println(
"Connection is established");
// Try block to check exceptions
try {
/* In order to avoid Result set type is
TYPE_FORWARD_ONLY */
Statement st = con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
/* Query takes first column and
count the rows. As mostly, Primary key is
the first column, it is ideal enough as
Primary key is always unique & not null.
*/
/* Step 4: Creating the statement */
res = st.executeQuery("SELECT * FROM "
+ "studentsdetails");
/* Step 5: Execute the statements */
// Moving the cursor to the last row
res.last();
/* Step 6: Process the results */
System.out.println(
"MySQL Table - studentsdetails contains "
+ res.getRow() + " rows");
}
// Catch block to handle exceptions
catch (SQLException s) {
// Exception handled if it is SQL based
System.out.println(
"SQL statement is not executed!"
+ s.getMessage());
}
}
catch (Exception e) {
// Exception handled here if it is generic
// program based
e.printStackTrace();
}
finally {
// Step 7: Closing the connection
res = null;
con = null;
}
}
}
输出:
示例2:在JDBC中获取表中的行数
Java
/* Step 1: Importing Database libraries */
import java.sql.*;
/* Only main class-GFG is shown
Connection class of JDBC is not shown.
Object of connection class is used */
public class GFG {
// Main driver method
public static void main(String[] args)
{
/* Objects are assigned null
before any execution */
// Connection class objects
Connection con = null;
ResultSet res = null;
// Try block to check exceptions
try {
/* Step 2: Load and register drivers
or relevant jars in build path of project */
// Driver used- 'mysql-connector-java-8.0.22'
// Loading and register drivers
// using Class.forname() method
Class.forName("com.mysql.cj.jdbc.Driver");
/* Step 3: Create a connection */
// using DriverManager
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?serverTimezone=UTC",
"root", "");
// Display message when connection
// is successfully established
System.out.println(
"Connection is established");
// Try block to check exceptions
try {
/* In order to avoid Result set type is
TYPE_FORWARD_ONLY */
Statement st = con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
/* Query takes first column and
count the rows. As mostly, Primary key is
the first column, it is ideal enough as
Primary key is always unique & not null.
*/
/* Step 4: Creating the statement */
res = st.executeQuery("SELECT * FROM "
+ "studentsdetails");
/* Step 5: Execute the statements */
// Moving the cursor to the last row
res.last();
/* Step 6: Process the results */
System.out.println(
"MySQL Table - studentsdetails contains "
+ res.getRow() + " rows");
}
// Catch block to handle exceptions
catch (SQLException s) {
// Exception handled if it is SQL based
System.out.println(
"SQL statement is not executed!"
+ s.getMessage());
}
}
catch (Exception e) {
// Exception handled here if it is generic
// program based
e.printStackTrace();
}
finally {
// Step 7: Closing the connection
res = null;
con = null;
}
}
}
输出: