-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test2.java
49 lines (43 loc) · 1.65 KB
/
Test2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) {
// 数据库连接信息
String jdbcUrl = "jdbc:mysql://localhost:3306/your_database_name";
String username = "your_username";
String password = "your_password";
// JDBC连接对象
Connection connection = null;
// 创建SQL语句
String sqlQuery = "SELECT * FROM your_table_name;";
try {
sqlQuery = "UPDATE your_table_name SET a='1'";
// 连接到数据库
connection = DriverManager.getConnection(jdbcUrl, username, password);
// 创建一个Statement对象
Statement statement = connection.createStatement();
// 执行查询
ResultSet resultSet = statement.executeQuery(sqlQuery);
// 遍历结果集并输出数据
while (resultSet.next()) {
int id = resultSet.getInt("id"); // 假设你的表中有一个名为 "id" 的列
String name = resultSet.getString("name"); // 假设你的表中有一个名为 "name" 的列
// 打印查询结果
System.out.println("ID: " + id + ", Name: " + name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭连接
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}