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

相关推荐
m0_674294642 分钟前
C#怎么使用Channel异步通道 C#如何用BoundedChannel实现有界队列限流异步数据流【进阶】
jvm·数据库·python
m0_748920362 分钟前
HTML函数在系统更新后变卡是硬件老化吗_软硬兼容性排查【方法】
jvm·数据库·python
Xiu Yan3 分钟前
Java 转 C++ 系列:STL容器之list
java·开发语言·数据结构·c++·stl·list·visual studio
weixin_424999363 分钟前
如何正确对对象键名进行字母序排序并存入数组
jvm·数据库·python
搬砖小郭4 分钟前
从0到1的springboot项目搭建到打包
java
maqr_1106 分钟前
如何配置Oracle 19c审计清理_DBMS_AUDIT_MGMT自动清除策略
jvm·数据库·python
小芝麻咿呀7 分钟前
边缘计算网关-EG8200Mini导轨版
java·人工智能·边缘计算
qq_372906939 分钟前
如何用 JavaScript 实现单选式盒子颜色切换(点击高亮,其余复原)
jvm·数据库·python
2401_8971905510 分钟前
怎样使用Navicat高级特权进行还原时解决字符集冲突_企业数据保护
jvm·数据库·python
weixin_5806140014 分钟前
c++文件锁使用方法 c++如何实现多进程文件同步
jvm·数据库·python