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


    前言必读

    读者手册(必读)_云边的快乐猫的博客-CSDN博客

    前言:

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

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

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

    文件图片例子:

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

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

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

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

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

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

    1. package com.woody.util;
    2. import java.io.IOException;
    3. import java.io.InputStream;
    4. import java.sql.*;
    5. import java.util.Properties;
    6. public class DbUtil {
    7. //1、定义3个静态变量,为了接收和存放读取出来的文件配置信息
    8. private static String a ;
    9. private static String b ;
    10. private static String c ;
    11. //2、写一个静态代码块,里面负责读取配置文件和注册驱动
    12. static {
    13. //2.1、绑定配置文件
    14. InputStream in = DbUtil.class.getClassLoader().getResourceAsStream("dp.properties");
    15. //2.2、创建键值对集合
    16. Properties properties = new Properties();
    17. try {
    18. //2.3、配置文件放到键值对集合中去读取
    19. properties.load(in);
    20. //2.4、get读取出来的文件再放到全局静态变量里面去赋值给a b c
    21. a= properties.getProperty("url");
    22. b= properties.getProperty("username");
    23. c= properties.getProperty("password");
    24. //2.5、注册驱动
    25. Class.forName(properties.getProperty("driver"));
    26. } catch (ClassNotFoundException | IOException e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. //3、定义获取数据库的静态连接方法,为了给外界调用
    31. public static Connection getConnection(){
    32. Connection connection = null; //3.1初始化一下
    33. try {
    34. connection = DriverManager.getConnection(a,b,c); //3.2获取数据库的连接,这是主要的!!!!!!!!
    35. } catch (SQLException e) {
    36. e.printStackTrace();
    37. }
    38. return connection;
    39. }
    40. //4、定义一个关闭资源方法
    41. public static void closeAll(ResultSet rs, PreparedStatement ps,Connection conn){
    42. //判断后关闭资源
    43. if (rs !=null){
    44. try {
    45. rs.close();
    46. } catch (SQLException e) {
    47. e.printStackTrace();
    48. }
    49. }
    50. if (ps !=null){
    51. try {
    52. ps.close();
    53. } catch (SQLException e) {
    54. e.printStackTrace();
    55. }
    56. }
    57. if (conn !=null){
    58. try {
    59. conn.close();
    60. } catch (SQLException e) {
    61. e.printStackTrace();
    62. }
    63. }
    64. }
    65. }

    配置已完成 

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

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

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

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

     代码例子:

    1. package com.woody.bean;
    2. /**
    3. * 这个类用来充当数据库信息和集合的中介。
    4. * 这是根据数据库的information表数据生成的列名
    5. * id id
    6. * name 名字
    7. * sex 性别
    8. * sge 年龄
    9. * birthday 生日
    10. * education 学历
    11. * emotion 情感状况
    12. * height 身高
    13. * weight 体重
    14. * constellation 星座
    15. * hobby 爱好
    16. * nucleic_acid 核酸时间
    17. */
    18. public class BeInformation {
    19. private int id;
    20. private String name;
    21. private String sex;
    22. private int age;
    23. private String birthday;
    24. private String education;
    25. private String emotion;
    26. private int height;
    27. private int weight;
    28. private String constellation;
    29. private String hobby;
    30. private String nucleic_acid;
    31. //快捷键生成的无参构造方法
    32. public BeInformation() {
    33. }
    34. //有参构造方法
    35. 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) {
    36. this.id = id;
    37. this.name = name;
    38. this.sex = sex;
    39. this.age = age;
    40. this.birthday = birthday;
    41. this.education = education;
    42. this.emotion = emotion;
    43. this.height = height;
    44. this.weight = weight;
    45. this.constellation = constellation;
    46. this.hobby = hobby;
    47. this.nucleic_acid = nucleic_acid;
    48. }
    49. //快捷键生成的set和get方法
    50. public int getId() {
    51. return id;
    52. }
    53. public void setId(int id) {
    54. this.id = id;
    55. }
    56. public String getName() {
    57. return name;
    58. }
    59. public void setName(String name) {
    60. this.name = name;
    61. }
    62. public String getSex() {
    63. return sex;
    64. }
    65. public void setSex(String sex) {
    66. this.sex = sex;
    67. }
    68. public int getAge() {
    69. return age;
    70. }
    71. public void setAge(int age) {
    72. this.age = age;
    73. }
    74. public String getBirthday() {
    75. return birthday;
    76. }
    77. public void setBirthday(String birthday) {
    78. this.birthday = birthday;
    79. }
    80. public String getEducation() {
    81. return education;
    82. }
    83. public void setEducation(String education) {
    84. this.education = education;
    85. }
    86. public String getEmotion() {
    87. return emotion;
    88. }
    89. public void setEmotion(String emotion) {
    90. this.emotion = emotion;
    91. }
    92. public int getHeight() {
    93. return height;
    94. }
    95. public void setHeight(int height) {
    96. this.height = height;
    97. }
    98. public int getWeight() {
    99. return weight;
    100. }
    101. public void setWeight(int weight) {
    102. this.weight = weight;
    103. }
    104. public String getConstellation() {
    105. return constellation;
    106. }
    107. public void setConstellation(String constellation) {
    108. this.constellation = constellation;
    109. }
    110. public String getHobby() {
    111. return hobby;
    112. }
    113. public void setHobby(String hobby) {
    114. this.hobby = hobby;
    115. }
    116. public String getNucleic_acid() {
    117. return nucleic_acid;
    118. }
    119. public void setNucleic_acid(String nucleic_acid) {
    120. this.nucleic_acid = nucleic_acid;
    121. }
    122. //快捷键生成的toString方法
    123. @Override
    124. public String toString() {
    125. return "BeInformation{" +
    126. "id=" + id +
    127. ", name='" + name + '\'' +
    128. ", sex='" + sex + '\'' +
    129. ", age=" + age +
    130. ", birthday='" + birthday + '\'' +
    131. ", education='" + education + '\'' +
    132. ", emotion='" + emotion + '\'' +
    133. ", height=" + height +
    134. ", weight=" + weight +
    135. ", constellation='" + constellation + '\'' +
    136. ", hobby='" + hobby + '\'' +
    137. ", nucleic_acid='" + nucleic_acid + '\'' +
    138. '}';
    139. }
    140. }

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

    1. package com.woody.Test.JDBC;
    2. import com.woody.bean.BeInformation;
    3. import com.woody.util.DbUtil;
    4. import java.sql.Connection;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8. import java.util.ArrayList;
    9. import java.util.List;
    10. /**
    11. * 这个类的作用就是数据库的信息放到属性封装类中,充当中继器再放到集合里面
    12. * 步骤:
    13. * 数获取连接数据库方法--->定义sql语句--->connection.prepareStatement发送sql语句到数据库--->ResultSet接收数据库--->ResultSet遍历并一个个获取出来并赋值
    14. * new实现封装类并set发送上一步赋值的---->再全部放到集合里面--->finally执行关闭集合的方法--->本方法的最后一个括号前return集合
    15. *
    16. *测试:方法外建立一个main方法,1.new本类自己-->调用这个定义的方法---->输出
    17. */
    18. public class BeInformationDao {
    19. //1.创建一个List集合方法<属性封装类> 方法名
    20. public List queryAlla() {
    21. //2.new一个ArrayList集合 <属性封装类>,
    22. ArrayList beInformationList = new ArrayList<>();
    23. //3.获取自己写好的连接方法!!!!!!!!!!!!!!!!!!!!!!
    24. Connection connection = DbUtil.getConnection();
    25. //4.定义要执行的sql语句
    26. String sql = "select * from information ";
    27. //5.发送格式化
    28. PreparedStatement ps = null;
    29. //6.接收格式化
    30. ResultSet rs = null;
    31. try {
    32. //7.发送sql语句到数据库(固定的)
    33. ps= connection.prepareStatement(sql);
    34. //8.接受数据库的返回信息
    35. rs=ps.executeQuery();
    36. //9.遍历接收的数据
    37. while (rs.next()){
    38. //10.接受到的用数据类型get方法获取数据库表里面的信息并赋值(括号里面的是数据库表中对应的数据)
    39. int id = rs.getInt("id");
    40. String name = rs.getString("name");
    41. String sex = rs.getString("sex");
    42. int age = rs.getInt("age");
    43. String birthday = rs.getString("birthday");
    44. String education = rs.getString("education");
    45. String emotion = rs.getString("emotion");
    46. int height = rs.getInt("height");
    47. int weight = rs.getInt("weight");
    48. String constellation = rs.getString("constellation");
    49. String hobby = rs.getString("hobby");
    50. String nucleic_acid = rs.getString("nucleic_acid");
    51. //11.把上面赋值的再set传给属性封装类里面
    52. BeInformation beInformation = new BeInformation();//实现自己定义的中继器bean
    53. beInformation.setId(id);
    54. beInformation.setName(name);
    55. beInformation.setSex(sex);
    56. beInformation.setAge(age);
    57. beInformation.setBirthday(birthday);
    58. beInformation.setEducation(education);
    59. beInformation.setEmotion(emotion);
    60. beInformation.setHeight(height);
    61. beInformation.setWeight(weight);
    62. beInformation.setWeight(weight);
    63. beInformation.setConstellation(constellation);
    64. beInformation.setHobby(hobby);
    65. beInformation.setNucleic_acid(nucleic_acid);
    66. //12.属性封装最终放到集合里面
    67. beInformationList.add(beInformation);
    68. }
    69. } catch (SQLException e) {
    70. throw new RuntimeException(e);
    71. }finally {
    72. //.13调用工具类里面的关闭资源方法(自己写的方法)
    73. DbUtil.closeAll(rs,ps,connection);
    74. }
    75. //14.return第12步的集合
    76. return beInformationList;
    77. }
    78. //输出测试,只是为了测试,可以不用这个main方法
    79. public static void main(String[] args) {
    80. BeInformationDao b = new BeInformationDao();
    81. List queryAlla = b.queryAlla();
    82. System.out.println(queryAlla);
    83. }
    84. }

    运行结果:

    [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'}]

  • 相关阅读:
    冷热电气多能互补的微能源网鲁棒优化调度(Matlab代码实现)
    CSS色域、色彩空间、CSS Color 4新标准
    chrome调试秘籍,让你的开发速度飞起来
    如何在Linux上使用git远程上传至gitee托管(add-commit-push指令详解)
    WinUI(WASDK)项目实践——优雅的开发上位机应用(新)
    Linux 系统下 CMake 示 例
    谷粒商城10——分布式缓存Redis 分布式锁Redisson SpringCache自定义缓存配置
    javaWeb基于SSM框架开发的社区医疗数据管理系统【项目源码+数据库脚本+报告】
    ubuntu20.04 安装TensorRT,解决依赖问题
    阿里巴巴API接口解析,实现获得商品详情
  • 原文地址:https://blog.csdn.net/m0_52861000/article/details/127778889