软件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
相关推荐
武昌库里写JAVA16 分钟前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
杜子不疼.35 分钟前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
落霞的思绪1 小时前
Java设计模式详细解读
java·开发语言·设计模式
Java小白程序员1 小时前
Spring Framework:Java 开发的基石与 Spring 生态的起点
java·数据库·spring
阿巴~阿巴~1 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list
拂晓银砾2 小时前
Java数据结构-栈
java·数据结构
java1234_小锋2 小时前
一周学会Matplotlib3 Python 数据可视化-绘制自相关图
开发语言·python·信息可视化·matplotlib·matplotlib3
甄超锋2 小时前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
m0_719084112 小时前
sharding-jdbc读写分离配置
java
凢en2 小时前
Perl——qw()函数
开发语言·perl