JDBS详细步骤

JDBC详细步骤

正常步骤

public class DemoStatement {
    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
        Properties pro = new Properties();
        pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
        // 1.加载驱动
        Class.forName(pro.getProperty("driver"));
        //2.建立连接
        Connection connection = DriverManager.getConnection(
                pro.getProperty("url"),
                pro.getProperty("username"),
                pro.getProperty("password")
        );
        //准备数据
        String sql = "select * from dept";
        //3.创建处理快
        Statement statement = connection.createStatement();
        //4.执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        //5.处理结果集
        while (resultSet.next()){
            int dptno = resultSet.getInt(1);
            String dname = resultSet.getNString(2);
            String loc = resultSet.getNString(3);
            System.out.println(dname+"-->"+dptno+"-->"+loc);
        }
        //6.释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}

封装

能封装的有三个

  1. 加载驱动
  2. 获取连接
  3. 关闭资源
public class Util {
    //1.封装加载驱动
    private static Properties pro = new Properties();
    static {
        try {
            pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //2.封装获取连接
    public static Connection getConnection() throws SQLException {

          Connection  connection = DriverManager.getConnection(
                    pro.getProperty("url"),
                    pro.getProperty("username"),
                    pro.getProperty("password")
            );
        return connection ;
    }
    //3.封装关闭资源
    public static void close(ResultSet resultSet, Statement statement,Connection connection){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static void  close (Statement statement ,Connection connection){
        close(null,statement,connection);
    }

实现用户登录功能

静态处理快实现

此为静态处理块

 public static boolean reg(String username ,String password){
        boolean flag = false;
        //申明
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        //1.加载驱动获得连接
        try {
             connection = Util.getConnection();
             //准备sql
            String sql = "select * from t_user where username ='"+username+"'and password = '"+password+"'";
            //2.创建处理块
             statement = connection.createStatement();
            //3.发送sql
            resultSet = statement.executeQuery(sql);
            //4.处理结果
            if (resultSet.next()){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //5.关闭资源
            Util.close(resultSet,statement,connection);
        }
        return flag;
    }

预处理块实现

public static boolean login(String username ,String password){
        boolean flag = false;
        //申明
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        try {//获取连接
            connection = Util.getConnection();
            //准备sql
            String sql = "select * from t_user where username = ? and password = ? ";
            //创建处理快
            statement = connection.prepareStatement(sql);
            //为占位符赋值
            statement.setObject(1,username);
            statement.setObject(2,password);
            //执行sql
            resultSet = statement.executeQuery();
            //处理结果
             if (resultSet.next()){
                 flag = true;
             }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Util.close(resultSet,statement,connection);
        }
        return  flag;
    }

两者之间区别

  • 预处理快能够防止sql注入
  • 预处理快效率高
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值