第三次作业
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest01 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");// ctrl alt v
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
Statement st = null;
ResultSet rs = null;
// 注册驱动
try {
Class.forName(driver);//ctrl p
// 获取链接对象
con = DriverManager.getConnection(url, user, password);
// alt enter
// ctrl 单机
// 异常(√) 方法未重写(错)
// System.out.println(con);com.mysql.cj.jdbc.ConnectionImpl@4ae82894
// 关闭自动 提交事务
con.setAutoCommit(false);
// 操作对象
st = con.createStatement();
// SQL语句b
String sql = "insert into t_user values(1,'yanyu')";
// 执行SQL语句
st.execute(sql);
// 提交 事务
con.commit();
} catch (ClassNotFoundException e) {
// 回滚事务
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
// 关流 操作
// rs st con
if (rs != null) {
// 判断变量 是否为 null
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/soft02
user=root
password=root