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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 统一配置可以方便后期代码的维护,比如更改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'}]

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
SQL 数据库连接 数据库
你不知道ADo.Net中操作数据库的步骤【超详细整理】
你不知道ADo.Net中操作数据库的步骤【超详细整理】
16 0
|
1月前
|
SQL 数据库 OceanBase
OceanBase数据库的主备库参照的配置文件
【2月更文挑战第27天】OceanBase数据库的主备库参照的配置文件
32 4
|
1月前
|
Ubuntu 关系型数据库 MySQL
使用Ubuntu和Windows电脑实现Mysql主从同步(详细操作步骤)
使用Ubuntu和Windows电脑实现Mysql主从同步(详细操作步骤)
30 2
|
1月前
|
SQL 安全 数据管理
在阿里云数据管理DMS(Data Management Service)中,您可以按照以下步骤来创建和管理数据库
【2月更文挑战第33天】在阿里云数据管理DMS(Data Management Service)中,您可以按照以下步骤来创建和管理数据库
37 7
|
1月前
|
Shell Linux 数据库
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
29 1
|
1月前
|
监控 Shell Linux
【Shell 命令集合 磁盘管理 】Linux 检查和创建磁盘配额数据库 quotacheck命令使用教程
【Shell 命令集合 磁盘管理 】Linux 检查和创建磁盘配额数据库 quotacheck命令使用教程
32 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 文件管理】Linux 更新locate命令所使用的数据库 updatedb命令解析
【Shell 命令集合 文件管理】Linux 更新locate命令所使用的数据库 updatedb命令解析
155 0
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
224 0
|
1月前
|
关系型数据库 MySQL 数据库
使用 Docker 搭建一个“一主一从”的 MySQL 读写分离集群(超详细步骤
使用 Docker 搭建一个“一主一从”的 MySQL 读写分离集群(超详细步骤
64 0
|
1月前
|
关系型数据库 MySQL Linux
【Linux】在Linux上安装MySQL数据库的步骤
【Linux】在Linux上安装MySQL数据库的步骤
127 0