软件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
相关推荐
I_LPL1 天前
day34 代码随想录算法训练营 动态规划专题2
java·算法·动态规划·hot100·求职面试
亓才孓1 天前
【MyBatis Exception】Public Key Retrieval is not allowed
java·数据库·spring boot·mybatis
Never_Satisfied1 天前
在c#中,string.replace会替换所有满足条件的子字符串,如何只替换一次
开发语言·c#
J_liaty1 天前
Java设计模式全解析:23种模式的理论与实践指南
java·设计模式
Desirediscipline1 天前
cerr << 是C++中用于输出错误信息的标准用法
java·前端·c++·算法
Demon_Hao1 天前
JAVA快速对接三方支付通道标准模版
java·开发语言
Renhao-Wan1 天前
Java 算法实践(八):贪心算法思路
java·算法·贪心算法
w***71101 天前
常见的 Spring 项目目录结构
java·后端·spring
xyq20241 天前
C# 判断语句详解与应用
开发语言