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

相关推荐
鱼鳞_几秒前
Java学习笔记_Day33(高级流)
java·笔记·学习
王仲肖2 分钟前
PostgreSQL 统计信息 — 完整总结与优化指南
数据库·postgresql
Pkmer3 分钟前
Java古法入门到精通
java
有想法的py工程师3 分钟前
PostgreSQL vs PolarDB:Checkpoint 调优策略深度对比(高频 vs 低频)
大数据·数据库·postgresql
m0_377618236 分钟前
golang如何使用struct嵌套_golang struct结构体嵌套使用方法.txt
jvm·数据库·python
不会写DN6 分钟前
为什么TCP是三次握手?
服务器·网络·网络协议·tcp/ip
2501_913061347 分钟前
网络编程——了解客户端与服务器端之间的交互(1)
java·网络
tumeng07119 分钟前
springboot与springcloud对应版本
java·spring boot·spring cloud
2301_8152795210 分钟前
Redis如何降低快照对CPU的影响_合理分配RDB执行时机避开业务高峰期
jvm·数据库·python