DBUtils封装

原理

  1. 将建立连接和关闭连接的过程封装在DBUtils工具类里
  2. 常规statement无法进行人机交互 无法键盘交互 引入preparestatement

代码

DBUtils类

package com.tyut.common;

import java.sql.*;

public class DBUtils {


    public static Connection getConnection() throws SQLException { //注意static 否则不能用类名调用
        String url="jdbc:mysql://localhost:3306/test01";
        String username="root";
        String userpwd="123456";

        try{
            Connection conn = DriverManager.getConnection(url,username,userpwd);
            return  conn;
        }catch(SQLException e){
            e.printStackTrace();
        }

        return null;

    }

    /**
     * @author xxx
     * @version 1.0
     * @date 2021.9.30
     * @param rs
     * @param st
     * @param conn
     */
    public static void close(ResultSet rs, Statement st, Connection conn){
        try{
            if(rs!=null)
                rs.close();
            if(st!=null)
                 st.close();
            if(conn!=null)
                 conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

JDBCDemo_Login_PrepareStatement类

package com.tyut.demo_jdbc;

import com.tyut.common.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo_Login_PrepareStatement {
    public static void main(String[] args) throws Exception {
        //常规statement无法进行人机交互 无法键盘交互 引入preparestatement
        /**
         * 创建键盘输入
         */
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username=sc.nextLine();
        System.out.println("请输入密码:");
        String password=sc.nextLine();

        /**
         *  使用工具类JDBCUtils中的方法getConnection获取数据库连接 直接用类名调用
         */
        Connection conn= DBUtils.getConnection();

        String sql="select * from user where username=? and password=?";//注意 是? 底下通过函数setObject()给?传参数

        PreparedStatement pst=null; //创建preparedstatement对象 通过preparedstatement对象执行语句
        ResultSet rs=null; //结果集
        try{
            pst=conn.prepareStatement(sql);
            pst.setObject(1,username);//第一个参数传username
            pst.setObject(2,password);//第二个参数传password
            rs = pst.executeQuery();
            /**
             * 处理查询结果
             */
            if(rs.next()){
                System.out.println("登陆成功");
                System.out.println("用户名:"+rs.getString("username"));
               System.out.println("密码:"+rs.getString("password"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            //关闭结果集ResultSet,Preparedment,Connection
            DBUtils.close(rs,pst,conn);
            //关闭Scanner
            sc.close();
        }


    }
}

结果

在这里插入图片描述

数据库

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值