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

相关推荐
IvorySQL4 小时前
PostgreSQL 日报 | JOIN 与外键优化(8 月 9 日)
数据库·postgresql
是未才4 小时前
从输入 URL 到页面返回:DNS、路由、TLS 与 HTTP 完整链路
java·后端·计算机网络
ltl4 小时前
自治数据库十年回顾:Peloton、NoisePage、OtterTune 到云原生 auto-tuning
数据库
不会就选b4 小时前
Linux之信号(二)
linux·运维·服务器
ttod_qzstudio4 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
GlueNa2SiO35 小时前
第十八章 Linux故障排查与恢复
linux·服务器·笔记·学习
萧瑟余晖5 小时前
Java深入解析篇十七之Spring Security
java·开发语言·spring
灯澜忆梦5 小时前
【MySQL10】进阶篇 | 索引_#2性能优化
数据库·sql·mysql·性能优化
寺中人6 小时前
Linux 基础命令入门实战教程:从零掌握常用操作,新手快速上手
linux·运维·服务器·shell·linux 命令·linux 基础教程·linux 入门
小张成长计划..6 小时前
【Linux】18:基础IO
linux·运维·服务器