JDBC笔记

文章目录

准备MySQL

数据的建立和建表






idea 建工程和模块


设置属性配置文件



编写JDBC代码












URL的设置

JDBC 代码







java 复制代码
package com.yanyu;

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

public class JDBCTest01 {
    //    psvm
    public static void main(String[] args) {
//    ctrl  alt  l
//        RB
        ResourceBundle bundle = ResourceBundle.getBundle("com/yanyu/db");

//          Ctrl    alt    v
        //    进入 源代码   ctrl   单机
//           ctrl   P  提示  形参类型
//        System.out.println(bundle);//java.util.PropertyResourceBundle@677327b6
//        System.out.println(bundle);
//        读取配置文件
        String driver = bundle.getString("driver");//  ctrl   alt   v
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        String url = bundle.getString("url");
//        System.out.println(driver);//  复制   当前行
//        System.out.println(url);//  复制   当前行
//        System.out.println(user);//  复制   当前行
//        System.out.println(password);//  复制   当前行
//        JDBC  代码
//           放大作用域
//        Connec   声明变了  并初始化    为 null
        Connection con = null;//  连接对象
        Statement st = null;//  操作对象
        ResultSet rs = null;//   结果对象
//          1.  注册驱动
//        Cl
        try {
            Class.forName(driver);//  mysql  connector  驱动名字  ,告诉Java连接哪个数据库
            //        alt   +   enter
//            1.  获取 连接  对象
//            DM
            con = DriverManager.getConnection(url,user,password);
//            System.out.println(con);//com.mysql.cj.jdbc.ConnectionImpl@52525845
//            获取  操作对象
            st = con.createStatement();
//           写MySQL  语句
            String sql = "insert into t_user values(1,'小明','123')";//  注意 SQL  用  ''
//              用操作对象 去 执行 SQL语句
            st.execute(sql);





        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
//            关闭流  rs    st   con
            
            
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            
            
        }






    }
}

配置文件

properties 复制代码
#驱动信息:告诉Java  去 连接  哪种数据库
#用户名
#密码
#URL   注释  ctrl  /
driver=com.mysql.cj.jdbc.Driver
#driver=com.mysql.jdbc.Driver     新版本驱动 多了   cj

user=root
password=123456
#url=jdbc:mysql://localhost:3306/    数据库名字
url=jdbc:mysql://localhost:3306/yanyu
相关推荐
hgfsjk5 分钟前
野莓采集图片修改指南:从单张处理到批量优化
大数据·经验分享·笔记·其他
咖啡忍者20 分钟前
【SAP BASIS】Section 11: SAP System Landscape
笔记
嵌入式DZC28 分钟前
嵌入式开发笔记随记
笔记
懿路向前33 分钟前
【唤醒实战笔记】2026-09-29 | 智能体提示词迭代史(v1 → v8)
linux·笔记·算法
2601_957883841 小时前
2026年9月:雷神专业维修服务保障
笔记
71-32 小时前
MySQL密码重置
数据库·笔记·学习·mysql
咖啡忍者2 小时前
【SAP BASIS】Section 13: Managing Transport Request
笔记
bllovepigpig3 小时前
操作系统个人学习笔记(四):K8s基础
笔记·学习·kubernetes
有范先生3 小时前
懒人代餐即食米饭:常温全能型,让一顿热饭不受厨房和冰箱限制
笔记
高山有多高4 小时前
【Linux笔记】Linux基本指令
linux·运维·笔记