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
相关推荐
西岸行者4 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky4 天前
Django入门笔记
笔记·django
勇气要爆发4 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意4 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发4 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain
qianshanxue114 天前
计算机操作的一些笔记标题
笔记
土拨鼠烧电路4 天前
笔记11:数据中台:不是数据仓库,是业务能力复用的引擎
数据仓库·笔记
土拨鼠烧电路4 天前
笔记14:集成与架构:连接孤岛,构建敏捷响应能力
笔记·架构
烟花落o4 天前
栈和队列的知识点及代码
开发语言·数据结构·笔记·栈和队列·编程学习