idea里面mysql数据库统一配置文件和存放到集合中读取出来详细步骤

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介: 统一配置可以方便后期代码的维护,比如更改mysql的密码或者什么就可以直接在资源配置文件里面更改就好,不用到代码里面去更改了。

前言:

为什么要用统一资源配置文件?


答:统一配置可以方便后期代码的维护,比如更改mysql的密码或者什么就可以直接在资源配置文件里面更改就好,不用到代码里面去更改了。


文件位置:要放到src下,是一级文件


文件图片例子:



一、配置文件例子(src一级文件下)

第一行的低版本的mysql就把.cj去掉

第二行最后面那个javafx对应的是自己要连接的数据库

第三第四行对应自己的MySQL数据库账号密码

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/javafx
username=root
password=root756

二、在类里面进行读取配置文件和定义一个给别的类的连接方法 (属于util工具包的)

package com.woody.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DbUtil {
    //1、定义3个静态变量,为了接收和存放读取出来的文件配置信息 
    private static String a ;
    private static String b ;
    private static String c ;
    //2、写一个静态代码块,里面负责读取配置文件和注册驱动
    static {
        //2.1、绑定配置文件
       InputStream in = DbUtil.class.getClassLoader().getResourceAsStream("dp.properties");
       //2.2、创建键值对集合 
        Properties properties = new Properties();
        try {
            //2.3、配置文件放到键值对集合中去读取 
            properties.load(in);
            //2.4、get读取出来的文件再放到全局静态变量里面去赋值给a b c 
            a= properties.getProperty("url");
            b= properties.getProperty("username");
            c= properties.getProperty("password");
            //2.5、注册驱动
            Class.forName(properties.getProperty("driver"));
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
    }
    //3、定义获取数据库的静态连接方法,为了给外界调用
    public static Connection getConnection(){
        Connection connection = null;  //3.1初始化一下
        try {
            connection = DriverManager.getConnection(a,b,c);  //3.2获取数据库的连接,这是主要的!!!!!!!!
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    //4、定义一个关闭资源方法  
    public static void closeAll(ResultSet rs, PreparedStatement ps,Connection conn){
        //判断后关闭资源
        if (rs !=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps !=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn !=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

配置已完成

===============================================


接下来的步骤就是对数据库中的数据进行获取存放到集合里面


三、建立一个属性封装类(属于bean包的),进行封装的属性要与数据库中的数据类型和名字都要对应


这是我个人的数据库中表的内容图片


代码例子:

package com.woody.bean;
/**
 * 这个类用来充当数据库信息和集合的中介。
 * 这是根据数据库的information表数据生成的列名
 * id id
 * name 名字
 * sex 性别
 * sge 年龄
 * birthday 生日
 * education  学历
 * emotion 情感状况
 * height 身高
 * weight 体重
 * constellation 星座
 * hobby 爱好
 * nucleic_acid 核酸时间
 */
public class BeInformation {
    private int id;
    private String name;
    private String sex;
    private int age;
    private String birthday;
    private String education;
    private String emotion;
    private int height;
    private int weight;
    private String constellation;
    private String hobby;
    private String nucleic_acid;
    //快捷键生成的无参构造方法
    public BeInformation() {
    }
    //有参构造方法
    public BeInformation(int id, String name, String sex, int age, String birthday, String education, String emotion, int height, int weight, String constellation, String hobby, String nucleic_acid) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.birthday = birthday;
        this.education = education;
        this.emotion = emotion;
        this.height = height;
        this.weight = weight;
        this.constellation = constellation;
        this.hobby = hobby;
        this.nucleic_acid = nucleic_acid;
    }
    //快捷键生成的set和get方法
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public String getEducation() {
        return education;
    }
    public void setEducation(String education) {
        this.education = education;
    }
    public String getEmotion() {
        return emotion;
    }
    public void setEmotion(String emotion) {
        this.emotion = emotion;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public String getConstellation() {
        return constellation;
    }
    public void setConstellation(String constellation) {
        this.constellation = constellation;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    public String getNucleic_acid() {
        return nucleic_acid;
    }
    public void setNucleic_acid(String nucleic_acid) {
        this.nucleic_acid = nucleic_acid;
    }
//快捷键生成的toString方法
    @Override
    public String toString() {
        return "BeInformation{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", birthday='" + birthday + '\'' +
                ", education='" + education + '\'' +
                ", emotion='" + emotion + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                ", constellation='" + constellation + '\'' +
                ", hobby='" + hobby + '\'' +
                ", nucleic_acid='" + nucleic_acid + '\'' +
                '}';
    }
}

四、建立一个把数据库具体数据放到集合里面的类(属于dao包的), 这需要用到上一个属性封装类充当存放的中介

package com.woody.Test.JDBC;
import com.woody.bean.BeInformation;
import com.woody.util.DbUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * 这个类的作用就是数据库的信息放到属性封装类中,充当中继器再放到集合里面
 * 步骤:
 * 数获取连接数据库方法--->定义sql语句--->connection.prepareStatement发送sql语句到数据库--->ResultSet接收数据库--->ResultSet遍历并一个个获取出来并赋值
 * new实现封装类并set发送上一步赋值的---->再全部放到集合里面--->finally执行关闭集合的方法--->本方法的最后一个括号前return集合
 *
 *测试:方法外建立一个main方法,1.new本类自己-->调用这个定义的方法---->输出
 */
public class BeInformationDao {
      //1.创建一个List集合方法<属性封装类>  方法名
    public List<BeInformation> queryAlla() {
        //2.new一个ArrayList集合 <属性封装类>,
        ArrayList<BeInformation> beInformationList  = new ArrayList<>();
        //3.获取自己写好的连接方法!!!!!!!!!!!!!!!!!!!!!!
        Connection connection = DbUtil.getConnection();
        //4.定义要执行的sql语句
        String sql = "select * from information ";
        //5.发送格式化
        PreparedStatement ps = null;
        //6.接收格式化
        ResultSet rs = null;
        try {
            //7.发送sql语句到数据库(固定的)
            ps= connection.prepareStatement(sql);
            //8.接受数据库的返回信息
            rs=ps.executeQuery();
            //9.遍历接收的数据
            while (rs.next()){
                //10.接受到的用数据类型get方法获取数据库表里面的信息并赋值(括号里面的是数据库表中对应的数据)
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String birthday = rs.getString("birthday");
                String education = rs.getString("education");
                String emotion = rs.getString("emotion");
                int height = rs.getInt("height");
                int weight = rs.getInt("weight");
                String constellation = rs.getString("constellation");
                String hobby = rs.getString("hobby");
                String nucleic_acid = rs.getString("nucleic_acid");
                //11.把上面赋值的再set传给属性封装类里面
                BeInformation beInformation = new BeInformation();//实现自己定义的中继器bean
                beInformation.setId(id);
                beInformation.setName(name);
                beInformation.setSex(sex);
                beInformation.setAge(age);
                beInformation.setBirthday(birthday);
                beInformation.setEducation(education);
                beInformation.setEmotion(emotion);
                beInformation.setHeight(height);
                beInformation.setWeight(weight);
                beInformation.setWeight(weight);
                beInformation.setConstellation(constellation);
                beInformation.setHobby(hobby);
                beInformation.setNucleic_acid(nucleic_acid);
                //12.属性封装最终放到集合里面
                beInformationList.add(beInformation);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //.13调用工具类里面的关闭资源方法(自己写的方法)
            DbUtil.closeAll(rs,ps,connection);
        }
         //14.return第12步的集合
        return beInformationList;
    }
    //输出测试,只是为了测试,可以不用这个main方法
    public static void main(String[] args) {
        BeInformationDao b = new BeInformationDao();
        List<BeInformation> queryAlla = b.queryAlla();
        System.out.println(queryAlla);
    }
}

运行结果:

[BeInformation{id=1, name='洛洛', sex='男生', age=18, birthday='5月27号', education='本科',

emotion='单身', height=188, weight=76, constellation='双子座', hobby='打怪兽', nucleic_acid='2022.11.10'}, BeInformation{id=2, name='晶晶', sex='女生', age=20, birthday='12月12号', education='本科', emotion='单身', height=170, weight=55, constellation='射手座', hobby='看剧', nucleic_acid='2022.11.11'}, BeInformation{id=3, name='赵云', sex='男生', age=24, birthday='11月9号', education='研究生', emotion='单身', height=185, weight=76, constellation='天蝎座', hobby='打野', nucleic_acid='2022.11.12'}]

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
4月前
|
存储 关系型数据库 MySQL
在CentOS 8.x上安装Percona Xtrabackup工具备份MySQL数据步骤。
以上就是在CentOS8.x上通过Perconaxtabbackup工具对Mysql进行高效率、高可靠性、无锁定影响地实现在线快速全量及增加式数据库资料保存与恢复流程。通过以上流程可以有效地将Mysql相关资料按需求完成定期或不定期地保存与灾难恢复需求。
360 10
|
7月前
|
IDE Java 开发工具
【Java基础-环境搭建-创建项目】IntelliJ IDEA创建Java项目的详细步骤
IntelliJ IDEA创建Java项目的图文详细步骤,手把手带你创建Java项目
1197 10
【Java基础-环境搭建-创建项目】IntelliJ IDEA创建Java项目的详细步骤
|
7月前
|
Java 关系型数据库 MySQL
在Linux操作系统上设置JDK、Tomcat、MySQL以及J2EE后端接口的部署步骤
让我们总结一下,给你的Linux操作系统装备上最强的军队,需要先后装备好JDK的弓箭,布置好Tomcat的阵地,再把MySQL的物资原料准备好,最后部署好J2EE攻城车,那就准备好进军吧,你的Linux军团,无人可挡!
162 18
|
8月前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
1881 26
|
8月前
|
缓存 Java Spring
IDEA中配置HTML和Thymeleaf热部署的步骤
以上就是在IntelliJ IDEA中配置HTML和Thymeleaf热部署的步骤。这样的配置可以大大提高你的开发效率,让你更专注于代码的编写,而不是等待应用的重启。希望这个指南对你有所帮助!
525 21
|
9月前
|
Ubuntu 关系型数据库 MySQL
容器技术实践:在Ubuntu上使用Docker安装MySQL的步骤。
通过以上的操作,你已经步入了Docker和MySQL的世界,享受了容器技术给你带来的便利。这个旅程中你可能会遇到各种挑战,但是只要你沿着我们划定的路线行进,你就一定可以达到目的地。这就是Ubuntu、Docker和MySQL的灵魂所在,它们为你开辟了一条通往新探索的道路,带你亲身感受到了技术的力量。欢迎在Ubuntu的广阔大海中探索,用Docker技术引领你的航行,随时准备感受新技术带来的震撼和乐趣。
376 16
|
10月前
|
人工智能 IDE 编译器
idea如何使用AI编程提升效率-在IntelliJ IDEA 中安装 GitHub Copilot 插件的步骤-卓伊凡
idea如何使用AI编程提升效率-在IntelliJ IDEA 中安装 GitHub Copilot 插件的步骤-卓伊凡
2004 15
idea如何使用AI编程提升效率-在IntelliJ IDEA 中安装 GitHub Copilot 插件的步骤-卓伊凡
|
8月前
|
存储 Oracle 关系型数据库
MySQL 8.4 配置SSL组复制(八个步骤)
MySQL 8.4 配置SSL组复制(八个步骤)
517 0
|
12月前
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
11月前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
503 2

热门文章

最新文章

推荐镜像

更多