软件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

相关推荐
云原生指北4 分钟前
GitHub Copilot SDK 入门:五分钟构建你的第一个 AI Agent
java
百结2144 小时前
Mysql数据库操作
数据库·mysql·oracle
keep one's resolveY4 小时前
时区问题解决
数据库
Leinwin4 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
qq_417695054 小时前
机器学习与人工智能
jvm·数据库·python
漫随流水4 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
薛定谔的悦4 小时前
MQTT通信协议业务层实现的完整开发流程
java·后端·mqtt·struts
ego.iblacat5 小时前
MySQL 服务基础
数据库·mysql
enjoy嚣士5 小时前
springboot之Exel工具类
java·spring boot·后端·easyexcel·excel工具类
罗超驿5 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist