前言:
为什么要用统一资源配置文件?
答:统一配置可以方便后期代码的维护,比如更改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
queryAlla() { - //2.new一个ArrayList集合 <属性封装类>,
- ArrayList
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
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'}]