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
相关推荐
Z21-X10 小时前
云原生运维学习笔记:ECShop全链路容器化部署与Docker底层资源隔离深度剖析
运维·笔记·学习·云原生
晓梦林14 小时前
BUUCTF-菜刀流量-MISC-学习笔记
笔记·学习
David猪大卫1 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
星恒随风1 天前
MySQL 从零入门(三):表的约束详解
linux·服务器·数据库·笔记·mysql
蒸蒸yyyyzwd1 天前
从后端面经学到的 3 个知识
笔记·学习
17岁的勇气1 天前
Unity Shader unity文档学习笔记(二十二):shader变体
笔记·学习·unity
坤坤子吖1 天前
Linux网络编程:自定义应用层协议与 JSON 序列化
linux·服务器·网络·c++·笔记·json
路灯上の马陈2 天前
Java 内部类 学习笔记
笔记·学习
希和20272 天前
51 单片机入门笔记:江科大课程代码逐行讲解(按键控制 LED 流水灯)
笔记·单片机·mongodb
前进的李工2 天前
Agent 学习笔记 12:Agent 的工具应该怎么设计
人工智能·笔记·学习·agent·工具调用·skill·工具设计