第三次作业
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 vString 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语句bString 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 conif (rs != null) {
// 判断变量 是否为 nulltry {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
key = value
url=jdbc:mysql://127.0.0.1:3306/soft02
user=root
password=root