Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增java JDBC #909

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 118 additions & 6 deletions docs/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ Consumer<String> test = System.out::println;
```java
Comparator<Integer> comparator = Math::max;

int result = comparator.compare(1, 2);
int result = comparator.compare(1, 2);
// 返回 2
```

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

String lowerCase = str::toLowerCase;
String lowerCase = str::toLowerCase;
// 返回 "hello"
```

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

String str = supplier.get();
String str = supplier.get();
// 返回一个空字符串
```

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


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

String lowerCase = someStr::toLowerCase;
String lowerCase = someStr::toLowerCase;
// 返回 "hello"
```

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

int result = someObject::staticMethod;
int result = someObject::staticMethod;
// 调用静态方法
```

Expand Down Expand Up @@ -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");
```

另见
---

Expand Down