抽取JDBC工具类:JDBCutils

    目的简化:书写 

    分析 :

      注册驱动也抽取

      抽取一个方法获取连接对象

    需求:不想传递参数(麻烦)还得保证工具类通用性

       解决:配置文件

 jdbc .proprietys

      url = 

      user =

      password=

抽取一个方法释放资源

 

java代码:

  

private static String url;
    private static String user;
    private static String password;
    private static String driver;

    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileReader("src/jdbc.properties"));
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

    public static void close(ResultSet resultSet, Statement statement, Connection connection){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
ublic List<Emp> findAll2(){
        List<Emp> list = null;
        Connection connection = null;
        ResultSet resultSet = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
//            connection = DriverManager.getConnection("jdbc:mysql:///a2", "root", "root");
           connection = JDBCUtils.getConnection();
            String sql = "select * from emp";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            Emp emp = null;
            list = new ArrayList<>();

            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String ename = resultSet.getString("ename");
                int job_id = resultSet.getInt("job_id");
                int mgr = resultSet.getInt("mgr");
                Date joindate = resultSet.getDate("joindate");
                double salary = resultSet.getDouble("salary");
                double bonus = resultSet.getDouble("bonus");
                int dept_id = resultSet.getInt("dept_id");

                emp = new Emp();
                emp.setId(id);
                emp.setEname(ename);
                emp.setJob_id(job_id);
                emp.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBounds(bonus);
                emp.setDept_id(dept_id);

                list.add(emp);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(resultSet,statement,connection);
        }
        return list;
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Jdbc练习_登录案例:

 需求:

  通过键盘录入输入用户名和密码

  判断是否登录成功

 

创建一张表

  

CREATE TABLE user(
id int PRIMARY key auto_increment,
    username VARCHAR(32),
    password VARCHAR(32)
);

INSERT INTO user VALUES (NULL,'zhangsan','123');
INSERT INTO user VALUES (NULL,'lisi','234');

java代码:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("用户名");
        String i = scanner.nextLine();
        System.out.println("密码");
        String i1 = scanner.nextLine();
        boolean login = login(i, i1);
        if (login){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
    }

    public static boolean login(String username,String password){
        if (username == null || password == null){
            return false;
        }
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from user where username = '"+username+"'and password = '"+password+"'";
            statement = conn.createStatement();
           rs = statement.executeQuery(sql);
           return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;

    }

 

posted on 2022-07-27 14:33  淤泥不染  阅读(24)  评论(0编辑  收藏  举报