-
Notifications
You must be signed in to change notification settings - Fork 2
/
CountRecord.java
70 lines (54 loc) 路 1.91 KB
/
CountRecord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.jayden.study.query;
import com.jayden.study.utils.JdbcUtils;
import java.sql.*;
/**
* Count Record
*
* @author jayden-lee
*/
public class CountRecord {
private static final String url = "jdbc:mysql://localhost:13909/mysql";
private static final String user = "user";
private static final String password = "password";
private static Connection connection;
public static void main(String[] args) {
try {
connection = JdbcUtils.getConnection(url, user, password);
int totalRows = getTotalRows();
System.out.println("Total Rows : " + totalRows);
int totalRows2 = getTotalRows2();
System.out.println("Total Rows : " + totalRows2);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeConnection(connection);
}
}
private static int getTotalRows() throws SQLException {
String sql = "SELECT * FROM sakila.address";
int totalRows;
try (Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY)) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
resultSet.last();
totalRows = resultSet.getRow();
} catch (SQLException e) {
throw e;
}
}
return totalRows;
}
private static int getTotalRows2() throws SQLException {
String sql = "SELECT count(*) FROM sakila.address";
int totalRows = 0;
try (PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
totalRows = resultSet.getInt(1);
}
} catch (SQLException e) {
throw e;
}
return totalRows;
}
}