@@ -1344,7 +1344,7 @@ Consumer<String> test = System.out::println;
1344
1344
``` java
1345
1345
Comparator<Integer > comparator = Math :: max;
1346
1346
1347
- int result = comparator. compare(1 , 2 );
1347
+ int result = comparator. compare(1 , 2 );
1348
1348
// 返回 2
1349
1349
```
1350
1350
@@ -1353,7 +1353,7 @@ int result = comparator.compare(1, 2);
1353
1353
``` java
1354
1354
String str = " HELLO" ;
1355
1355
1356
- String lowerCase = str:: toLowerCase;
1356
+ String lowerCase = str:: toLowerCase;
1357
1357
// 返回 "hello"
1358
1358
```
1359
1359
@@ -1362,7 +1362,7 @@ String lowerCase = str::toLowerCase;
1362
1362
``` java
1363
1363
Supplier<String > supplier = String :: new ;
1364
1364
1365
- String str = supplier. get();
1365
+ String str = supplier. get();
1366
1366
// 返回一个空字符串
1367
1367
```
1368
1368
@@ -1372,7 +1372,7 @@ String str = supplier.get();
1372
1372
Function<Integer , String []> function = String []:: new ;
1373
1373
1374
1374
1375
- String [] array = function. apply(5 );
1375
+ String [] array = function. apply(5 );
1376
1376
// 返回 5 个空字符串的数组
1377
1377
```
1378
1378
<!-- rehype:className=wrap-text-->
@@ -1382,7 +1382,7 @@ String[] array = function.apply(5);
1382
1382
``` java
1383
1383
String someStr = " HELLO" ;
1384
1384
1385
- String lowerCase = someStr:: toLowerCase;
1385
+ String lowerCase = someStr:: toLowerCase;
1386
1386
// 返回 "hello"
1387
1387
```
1388
1388
@@ -1391,7 +1391,7 @@ String lowerCase = someStr::toLowerCase;
1391
1391
``` java
1392
1392
SomeClass someObject = new SomeClass ();
1393
1393
1394
- int result = someObject:: staticMethod;
1394
+ int result = someObject:: staticMethod;
1395
1395
// 调用静态方法
1396
1396
```
1397
1397
@@ -1558,6 +1558,118 @@ int frequency = Collections
1558
1558
.frequency(list, 2 ); // frequency = 2
1559
1559
```
1560
1560
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
+
1561
1673
另见
1562
1674
---
1563
1675
0 commit comments