软件2班20240513






第三次作业



























java 复制代码
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

key = value

url=jdbc:mysql://127.0.0.1:3306/soft02

user=root

password=root

相关推荐
XDHCOM7 小时前
ORA-32484重复列名错误,ORACLE数据库CYCLE子句故障修复与远程处理方案
数据库·oracle
云烟成雨TD7 小时前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
lwx9148527 小时前
Linux-Shell算术运算
linux·运维·服务器
于慨7 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
swg3213217 小时前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
翻斗包菜7 小时前
PostgreSQL 日常维护完全指南:从基础操作到高级运维
运维·数据库·postgresql
gelald7 小时前
SpringBoot - 自动配置原理
java·spring boot·后端
殷紫川7 小时前
深入理解 AQS:从架构到实现,解锁 Java 并发编程的核心密钥
java
呆瑜nuage7 小时前
MySQL表约束详解:8大核心约束实战指南
数据库·mysql
一轮弯弯的明月7 小时前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得