Skip to content

Commit 44e775a

Browse files
authored
doc: update docs/java.md (#909)
1 parent 933b195 commit 44e775a

File tree

1 file changed

+118
-6
lines changed

1 file changed

+118
-6
lines changed

docs/java.md

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ Consumer<String> test = System.out::println;
13441344
```java
13451345
Comparator<Integer> comparator = Math::max;
13461346

1347-
int result = comparator.compare(1, 2);
1347+
int result = comparator.compare(1, 2);
13481348
// 返回 2
13491349
```
13501350

@@ -1353,7 +1353,7 @@ int result = comparator.compare(1, 2);
13531353
```java
13541354
String str = "HELLO";
13551355

1356-
String lowerCase = str::toLowerCase;
1356+
String lowerCase = str::toLowerCase;
13571357
// 返回 "hello"
13581358
```
13591359

@@ -1362,7 +1362,7 @@ String lowerCase = str::toLowerCase;
13621362
```java
13631363
Supplier<String> supplier = String::new;
13641364

1365-
String str = supplier.get();
1365+
String str = supplier.get();
13661366
// 返回一个空字符串
13671367
```
13681368

@@ -1372,7 +1372,7 @@ String str = supplier.get();
13721372
Function<Integer, String[]> function = String[]::new;
13731373

13741374

1375-
String[] array = function.apply(5);
1375+
String[] array = function.apply(5);
13761376
// 返回 5 个空字符串的数组
13771377
```
13781378
<!--rehype:className=wrap-text-->
@@ -1382,7 +1382,7 @@ String[] array = function.apply(5);
13821382
```java
13831383
String someStr = "HELLO";
13841384

1385-
String lowerCase = someStr::toLowerCase;
1385+
String lowerCase = someStr::toLowerCase;
13861386
// 返回 "hello"
13871387
```
13881388

@@ -1391,7 +1391,7 @@ String lowerCase = someStr::toLowerCase;
13911391
```java
13921392
SomeClass someObject = new SomeClass();
13931393

1394-
int result = someObject::staticMethod;
1394+
int result = someObject::staticMethod;
13951395
// 调用静态方法
13961396
```
13971397

@@ -1558,6 +1558,118 @@ int frequency = Collections
15581558
.frequency(list, 2); // frequency = 2
15591559
```
15601560

1561+
操纵数据库
1562+
----
1563+
1564+
### JDBC
1565+
1566+
```java
1567+
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
1568+
String user = "root";
1569+
String password = "123456";
1570+
String sql = "SELECT 1 as a, '2' as b";
1571+
String preparedSql = "SELECT * FROM t_user WHERE id = ?";
1572+
1573+
Connection conn = null;
1574+
Statement sm = null;
1575+
ResultSet rs = null;
1576+
try {
1577+
// 1.注册驱动
1578+
Class.forName("com.mysql.cj.jdbc.Driver");
1579+
} catch (ClassNotFoundException e) {
1580+
// 驱动找不到
1581+
throw new RuntimeException(e);
1582+
}
1583+
1584+
// 2.建立连接
1585+
try (Connection connection = DriverManager.getConnection(url, user, password)) {
1586+
1587+
conn = connection;
1588+
1589+
// 3.创建Statement对象
1590+
try (Statement statement = connection.createStatement()) {
1591+
1592+
sm = statement;
1593+
1594+
// 4.执行SQL语句
1595+
try (ResultSet resultSet = statement.executeQuery(sql)) {
1596+
1597+
rs = resultSet;
1598+
1599+
// 5.处理结果集
1600+
while (resultSet.next()) {
1601+
// 按照列名取值
1602+
System.out.println(resultSet.getLong("a"));
1603+
// 按照索引取值
1604+
System.out.println(resultSet.getString(2));
1605+
}
1606+
}
1607+
}
1608+
1609+
// 3.创建PreparedStatement对象
1610+
try (PreparedStatement preparedStatement = connection.prepareStatement(preparedSql)) {
1611+
1612+
sm = preparedStatement;
1613+
1614+
preparedStatement.setLong(1, 1_000L);
1615+
// 4.执行SQL语句
1616+
try (ResultSet resultSet = preparedStatement.executeQuery()) {
1617+
1618+
rs = resultSet;
1619+
1620+
// 5.处理结果集
1621+
while (resultSet.next()) {
1622+
System.out.println(resultSet.getLong("id"));
1623+
System.out.println(resultSet.getString(2));
1624+
}
1625+
}
1626+
}
1627+
} catch (SQLException e) {
1628+
// 数据库异常
1629+
throw new RuntimeException(e);
1630+
} finally {
1631+
// 6.关闭资源
1632+
// 上面的try块里已经自动关闭,否则(JDK 7以前)按照以下顺序关闭
1633+
// 先打开的后关闭,后打开的先关闭
1634+
if (null != rs) {
1635+
try {
1636+
rs.close();
1637+
} catch (SQLException ignored) {
1638+
}
1639+
}
1640+
1641+
if (null != sm) {
1642+
try {
1643+
sm.close();
1644+
} catch (SQLException ignored) {
1645+
}
1646+
}
1647+
1648+
if (null != conn) {
1649+
try {
1650+
conn.close();
1651+
} catch (SQLException ignored) {
1652+
}
1653+
}
1654+
1655+
// 也可以直接工具类, 注意顺序
1656+
IOUtils.close(rs);
1657+
IOUtils.close(sm);
1658+
IOUtils.close(conn);
1659+
}
1660+
```
1661+
1662+
### JDBC注册驱动
1663+
1664+
```java
1665+
Class.forName("com.mysql.cj.jdbc.Driver");
1666+
1667+
DriverManager.registerDriver(new org.postgresql.Driver());
1668+
1669+
// 支持多个同时注册
1670+
System.setProperty("jdbc.drivers", "com.mysql.cj.jdbc.Driver:org.postgresql.Driver");
1671+
```
1672+
15611673
另见
15621674
---
15631675

0 commit comments

Comments
 (0)