1.tidb 查询数据
在 Java 语言当中,可以通过声明一个 Author 类来定义如何存放作者的基础信息,根据数据的类型和取值范围从
Java 语言当中选择合适的数据类型来存放对应的数据,例如:
使用 Int 类型变量存放 int 类型的数据。
使用 Long 类型变量存放 bigint 类型的数据。
使用 Short 类型变量存放 tinyint 类型的数据。
使用 String 类型变量存放 varchar 类型的数据。
public class Author {
private Long id;
private String name;
private Short gender;
private Short birthYear;
private Short deathYear;
public Author() {}
// Skip the getters and setters.
}
public class AuthorDAO {
// Omit initialization of instance variables.
public List<Author> getAuthors() throws SQLException {
List<Author> authors = new ArrayList<>();
try (Connection conn = ds.getConnection()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name FROM authors");
while (rs.next()) {
Author author = new Author();
author.setId(rs.getLong("id"));
author.setName(rs.getString("name"));
authors.add(author);
}
}
return authors;
}
}
在获得数据库连接之后,你可以通过 conn.createStatement() 语句创建一个 Statement 实例对象。
然后调用 stmt.executeQuery("query_sql") 方法向 TiDB 发起一个数据库查询请求。
数据库返回的查询结果将会存放到 ResultSet 当中,通过遍历 ResultSet 对象可以将返回结果映射到此前准备的 Author 类对象当中。
对结果进行筛选
查询得到的结果非常多,但是并不都是你想要的?可以通过 WHERE 语句对查询的结果进行过滤,从而找到想要查询的部分。
例如,想要查找众多作家当中找出在 1998 年出生的作家:
public List<Author> getAuthorsByBirthYear(Short birthYear) throws SQLException {
List<Author> authors = new ArrayList<>();
try (Connection conn = ds.getConnection()) {
PreparedStatement stmt = conn.prepareStatement("""
SELECT * FROM authors WHERE birth_year = ?;
""");
stmt.setShort(1, birthYear);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Author author = new Author();
author.setId(rs.getLong("id"));
author.setName(rs.getString("name"));
authors.add(author);
}
}
return authors;
}
#对结果进行排序
使用 ORDER BY 语句可以让查询结果按照期望的方式进行排序
public List<Author> getAuthorsSortByBirthYear() throws SQLException {
List<Author> authors = new ArrayList<>();
try (Connection conn = ds.getConnection()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("""
SELECT id, name, birth_year
FROM authors
ORDER BY birth_year DESC;
""");
while (rs.next()) {
Author author = new Author();
author.setId(rs.getLong("id"));
author.setName(rs.getString("name"));
author.setBirthYear(rs.getShort("birth_year"));
authors.add(author);
}
}
return authors;
}
#限制查询结果数量
#如果希望 TiDB 只返回部分结果,可以使用 LIMIT 语句限制查询结果返回的记录数。
public List<Author> getAuthorsWithLimit(Integer limit) throws SQLException {
List<Author> authors = new ArrayList<>();
try (Connection conn = ds.getConnection()) {
PreparedStatement stmt = conn.prepareStatement("""
SELECT id, name, birth_year
FROM authors
ORDER BY birth_year DESC
LIMIT ?;
""");
stmt.setInt(1, limit);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Author author = new Author();
author.setId(rs.getLong("id"));
author.setName(rs.getString("name"));
author.setBirthYear(rs.getShort("birth_year"));
authors.add(author);
}
}
return authors;
}
#聚合查询
如果你想要关注数据整体的情况,而不是部分数据,你可以通过使用 GROUP BY 语句配合聚合函数。
public class AuthorCount {
private Short birthYear;
private Integer authorCount;
public AuthorCount() {}
// Skip the getters and setters.
}
public List<AuthorCount> getAuthorCountsByBirthYear() throws SQLException {
List<AuthorCount> authorCounts = new ArrayList<>();
try (Connection conn = ds.getConnection()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("""
SELECT birth_year, COUNT(DISTINCT id) AS author_count
FROM authors
GROUP BY birth_year
ORDER BY author_count DESC;
""");
while (rs.next()) {
AuthorCount authorCount = new AuthorCount();
authorCount.setBirthYear(rs.getShort("birth_year"));
authorCount.setAuthorCount(rs.getInt("author_count"));
authorCounts.add(authorCount);
}
}
return authorCount;
}