使用jdbc执行SQL实现登录查询1-带配置文件和工具类

1.jdbc.properties 配置文件


url = jdbc:mysql:///db2
user = root
password = 12345678
driver = com.mysql.cj.jdbc.Driver

2.Utils.JDBCUtils抽取的工具类

package Utils;

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


/**
 * @author Alina
 * @date 2022年02月05日 10:14 下午
 * JDBC的工具类,
 * 1.动态注册驱动
 * 2.释放资源
 * 对应文件6
 */
public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    /***
     *
     * @author Alina
     * @date 2022/2/5 10:53 下午
     * @param null
     * @return null
     * 声明静态代码块,以方便调用类时,代码块的内容就被执行
     */
    static {

        try {
            //使用Properties 类读取配置文件中内容
            Properties pro = new Properties();
            //使用class类的Classload 方法获得绝对地址
           ClassLoader loader = JDBCUtils.class.getClassLoader();
           URL res_url = loader.getResource("jdbc.properties");
           String path = res_url.getPath();
          

            //读取配置文件中内容
            pro.load(new FileReader(path));
            //此处后面的变量记得加双引号
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            Class.forName(pro.getProperty("driver"));

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



    }
    /***
     *
     * @author Alina
     * @date 2022/2/5 10:57 下午
     * @return java.sql.Connection
     * 使用方法获得数据库链接方式
     */

    public static Connection getconnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }




    public static void close(Statement stmt, Connection coon) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        if (coon != null) {
            try {
                coon.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static void close(ResultSet res ,Statement stmt, Connection coon) {
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                    throwables.printStackTrace();
            }

        }

        if (stmt != null) {
            try {
                    stmt.close();
            } catch (SQLException throwables) {
                    throwables.printStackTrace();
            }

        }
        if (coon != null) {
            try {
                    coon.close();
            } catch (SQLException throwables) {
                    throwables.printStackTrace();
            }
        }

    }
}

3.获取的数据库内容

package com.jdsc;


import Utils.JDBCUtils;
import domain.Emp;

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


/**
 * @author Alina
 * @date 2022年02月05日 8:14 下午
 * 将数据库内查询到的数据封装为对象储存
 */
public class jdbcDemo6 {
    public static void main(String[] args) {
        List<Emp> list = new jdbcDemo6().findAll();
        System.out.println(list);

    }
    public List<Emp> findAll()  {
        // 解析class文件 链接驱动
        Connection conn = null;
        Statement stml = null;
        ResultSet res = null;
        List<Emp> list = null;
        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            //链接数据库
//            conn = DriverManager.getConnection(
//                    "jdbc:mysql:///db2",
//                    "root",
//                    "12345678");
            conn = JDBCUtils.getconnection();

            //获取执行sql的对象 Statement
            stml = conn.createStatement();
            //定义Sql语句
            String sql = "select * from db2.emp";
            //获取执行SQL语句后的结果集对象
            res = stml.executeQuery(sql);
            list = new ArrayList<Emp>();
            Emp emp = null;

            //如果当前游标有下一个目标
            while (res.next()){
                //获取指定行数中的列的值
                int id = res.getInt("id");
                String name = res.getString("NAME");
                String gander = res.getString("gender");
                double salary = res.getDouble("salary");
                Date date = res.getDate("join_date");
                int dept_id = res.getInt("dept_id");
                //创建emp对象
                emp = new Emp();
                emp.setId(id);
                emp.setName(name);
                emp.setGender(gander);
                emp.setSalary(salary);
                emp.getDate(date);
                emp.setDept_id(dept_id);
                list.add(emp);
            }
        } catch ( SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(res,stml,conn);

        }



        return list;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值