软件3班20240513






java.util.PropertyResourceBundle@4554617c






java 复制代码
package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest01 {
    public static void main(String[] args) throws SQLException {
//        获取属性配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("com/yanyu/db");//ctrl   alt   v
//        System.out.println(bundle);//   CTRL  SHIFT   F10  java.util.PropertyResourceBundle@4554617c
        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);//  alt   enter
            con = DriverManager.getConnection(url, user, password);
//            System.out.println(connection);
            con.setAutoCommit(false);
            st = con.createStatement();
//            关闭十五自动提交
            String sql = "insert into t_user values(2,'yanyu2')";
            boolean execute = st.execute(sql);
//            System.out.println(execute?"数据插入成功":"数据插入失败");


            con.commit();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            if (con != null) {
                con.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }
        }
//        异常     方法未重写(继承 父类   接口)



    }
}
java 复制代码
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/soft03
#https:www.baidu.com
user=root
password=root
相关推荐
皮皮林5516 分钟前
拒绝写重复代码,试试这套开源的 SpringBoot 组件,效率翻倍~
java·spring boot
顺风尿一寸4 小时前
从 Java NIO poll 到 Linux 内核 poll:一次系统调用的完整旅程
java
程途知微4 小时前
JVM运行时数据区各区域作用与溢出原理
java
华仔啊6 小时前
为啥不用 MP 的 saveOrUpdateBatch?MySQL 一条 SQL 批量增改才是最优解
java·后端
xiaoye20188 小时前
Lettuce连接模型、命令执行、Pipeline 浅析
java
beata12 小时前
Java基础-18:Java开发中的常用设计模式:深入解析与实战应用
java·后端
Seven9712 小时前
剑指offer-81、⼆叉搜索树的最近公共祖先
java
雨中飘荡的记忆1 天前
保证金系统入门到实战
java·后端
Nyarlathotep01131 天前
Java内存模型
java