From 44e775a1ee9729a04d1d12c3d2ee66338b240c75 Mon Sep 17 00:00:00 2001 From: witt <52407727+witt-bit@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:09:55 +0800 Subject: [PATCH] doc: update docs/java.md (#909) --- docs/java.md | 124 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 118 insertions(+), 6 deletions(-) diff --git a/docs/java.md b/docs/java.md index cae6fa8..239b267 100644 --- a/docs/java.md +++ b/docs/java.md @@ -1344,7 +1344,7 @@ Consumer test = System.out::println; ```java Comparator comparator = Math::max; -int result = comparator.compare(1, 2); +int result = comparator.compare(1, 2); // 返回 2 ``` @@ -1353,7 +1353,7 @@ int result = comparator.compare(1, 2); ```java String str = "HELLO"; -String lowerCase = str::toLowerCase; +String lowerCase = str::toLowerCase; // 返回 "hello" ``` @@ -1362,7 +1362,7 @@ String lowerCase = str::toLowerCase; ```java Supplier supplier = String::new; -String str = supplier.get(); +String str = supplier.get(); // 返回一个空字符串 ``` @@ -1372,7 +1372,7 @@ String str = supplier.get(); Function function = String[]::new; -String[] array = function.apply(5); +String[] array = function.apply(5); // 返回 5 个空字符串的数组 ``` @@ -1382,7 +1382,7 @@ String[] array = function.apply(5); ```java String someStr = "HELLO"; -String lowerCase = someStr::toLowerCase; +String lowerCase = someStr::toLowerCase; // 返回 "hello" ``` @@ -1391,7 +1391,7 @@ String lowerCase = someStr::toLowerCase; ```java SomeClass someObject = new SomeClass(); -int result = someObject::staticMethod; +int result = someObject::staticMethod; // 调用静态方法 ``` @@ -1558,6 +1558,118 @@ int frequency = Collections .frequency(list, 2); // frequency = 2 ``` +操纵数据库 +---- + +### JDBC + +```java +String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC"; +String user = "root"; +String password = "123456"; +String sql = "SELECT 1 as a, '2' as b"; +String preparedSql = "SELECT * FROM t_user WHERE id = ?"; + +Connection conn = null; +Statement sm = null; +ResultSet rs = null; +try { + // 1.注册驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); +} catch (ClassNotFoundException e) { + // 驱动找不到 + throw new RuntimeException(e); +} + +// 2.建立连接 +try (Connection connection = DriverManager.getConnection(url, user, password)) { + + conn = connection; + + // 3.创建Statement对象 + try (Statement statement = connection.createStatement()) { + + sm = statement; + + // 4.执行SQL语句 + try (ResultSet resultSet = statement.executeQuery(sql)) { + + rs = resultSet; + + // 5.处理结果集 + while (resultSet.next()) { + // 按照列名取值 + System.out.println(resultSet.getLong("a")); + // 按照索引取值 + System.out.println(resultSet.getString(2)); + } + } + } + + // 3.创建PreparedStatement对象 + try (PreparedStatement preparedStatement = connection.prepareStatement(preparedSql)) { + + sm = preparedStatement; + + preparedStatement.setLong(1, 1_000L); + // 4.执行SQL语句 + try (ResultSet resultSet = preparedStatement.executeQuery()) { + + rs = resultSet; + + // 5.处理结果集 + while (resultSet.next()) { + System.out.println(resultSet.getLong("id")); + System.out.println(resultSet.getString(2)); + } + } + } +} catch (SQLException e) { + // 数据库异常 + throw new RuntimeException(e); +} finally { + // 6.关闭资源 + // 上面的try块里已经自动关闭,否则(JDK 7以前)按照以下顺序关闭 + // 先打开的后关闭,后打开的先关闭 + if (null != rs) { + try { + rs.close(); + } catch (SQLException ignored) { + } + } + + if (null != sm) { + try { + sm.close(); + } catch (SQLException ignored) { + } + } + + if (null != conn) { + try { + conn.close(); + } catch (SQLException ignored) { + } + } + + // 也可以直接工具类, 注意顺序 + IOUtils.close(rs); + IOUtils.close(sm); + IOUtils.close(conn); +} +``` + +### JDBC注册驱动 + +```java +Class.forName("com.mysql.cj.jdbc.Driver"); + +DriverManager.registerDriver(new org.postgresql.Driver()); + +// 支持多个同时注册 +System.setProperty("jdbc.drivers", "com.mysql.cj.jdbc.Driver:org.postgresql.Driver"); +``` + 另见 ---