jdbc 就是使用java语言操作关系型数据库的一套api
jdbc本质:
jdbc的好处
<dependency>
<groupId>com.mysqlgroupId>
<artifactId>mysql-connector-jartifactId>
<version>8.0.33version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>RELEASEversion>
<scope>compilescope>
dependency>
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "root1234";
String className = "com.mysql.cj.jdbc.Driver";
// 1. 注册驱动
try {
// 这个可以不用写,mysql驱动里面配置了
// 会自动加载jar包中META-INF/services/java.sql.Driver文件中的驱动类
// Class.forName(className);
// 2. 获取连接
Connection conn = DriverManager.getConnection(url, username, password);
// 3. 定义sql语句
String sql = "update account set money = 2000 where id = 1";
// 4. 获取执行sql的对象 statement
Statement statement = conn.createStatement();
// 5. 执行sql语句
int count = statement.executeUpdate(sql);
// 6. 处理结果
System.out.println("count: " + count);
// 7. 释放资源
statement.close();
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}