软件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
相关推荐
用户35218024547514 小时前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
东坡白菜17 小时前
破局全栈:一个前端开发的Java入门实战记录(1)
java·全栈
唐青枫17 小时前
Java Tomcat 实战指南:从 Servlet 容器到 Spring Boot 部署
java
wsaaaqqq18 小时前
roudan:自由选择实体、灵活操作数据、快速写入数据库的 Java 框架
java
plainGeekDev21 小时前
null 判断 → Kotlin 可空类型
android·java·kotlin
糖拌西瓜皮21 小时前
Java开发者视角:深入理解Node.js异步编程模型
java·后端·node.js
plainGeekDev21 小时前
getter/setter → Kotlin 属性
android·java·kotlin
一线大码1 天前
Smart-Doc 的简单使用
java·后端·restful
MacroZheng1 天前
Claude Code官方桌面端正式发布,夯爆了!
java·人工智能·后端