配置文件简化jdbc连接mysql

emPerson
package cn.Domain;

public class emPerson {
    private int id;
    private String name;
    private int age;
    private String gender;

    public emPerson() {
    }

    public emPerson(int id, String name, int age, String gender) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "emPerson{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                '}';
    }
}

Demo07Jdbc 
package cn.Dong;

import cn.Domain.emPerson;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Demo07Jdbc {
    /**
     * 查询emPerson所有对象
     * @return
     */
    public List findAll() throws SQLException {
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        Listlist=null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC", "root", "");

            //3.定义sql
            String sql="select * from stu";
            //4.获取执行sql的对象
             stmt = conn.createStatement();
            //5.执行sql
            rs = stmt.executeQuery(sql);
            //6.遍历结果集,封装对象
            emPerson emp=null;
            list=new ArrayList<>();
            while (rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                int age=rs.getInt("age");
                String gender=rs.getString("gender");

                emp=new emPerson();
                emp.setAge(age);
                emp.setGender(gender);
                emp.setId(id);
                emp.setName(name);
                //装载集合
                list.add(emp);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt.close();
            conn.close();
        }

        return list;
    }

    public static void main(String[] args) throws SQLException {
        Listlist=new Demo07Jdbc().findAll();
        System.out.println(list);

    }
}

-version 2.0
jdbc.propreties                           //配置文件
url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC
user=root
password=
driver=com.mysql.cj.jdbc.Driver
JDBCUtils                                 //封装方法
package cn.utils;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {

    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    /**
     * 文件的读取,只需要读取义词即可拿到资源,可使用静态代码块
     */
    static {

        try {
            //读取资源文件,获取值
            //1.创建Properties集合类
            Properties pro=new Properties();
            //获取src路径下的文件的方式---》ClassLoader类加载器
            ClassLoader classLoader=JDBCUtils.class.getClassLoader();
            URL resource = classLoader.getResource("jdbc.properties");
            String path = resource.getPath();
            System.out.println(path);

            //2.加载文件
            pro.load(new FileReader(path));
            //3.获取数据,赋值
            url=pro.getProperty("url");
            user=pro.getProperty("user");
            password=pro.getProperty("password");
            driver=pro.getProperty("driver");
            //4.注册驱动
            Class.forName(driver);


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return  连接对象
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    /**
     * 释放资源
     * @param stmt
     * @param conn
     */
    public static void close(Statement stmt,Connection conn){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
    public static void close(ResultSet rs,Statement stmt, Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

第一次使用配置文件
配置文件就是不想传参,或者参数太多,参数容易变,的简便方法

你可能感兴趣的:(java)