JDBC中的PreparedStatement对象

  • 提取工具类

    public class JdbcUtils {
    
        private static String driver = null;
        private static String url = null;
        private static String username = null;
        private static String password = null;
    
        static{
            try{
                InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
                Properties properties = new Properties();
                properties.load(in);
    
                driver = properties.getProperty("driver");
                url = properties.getProperty("url");
                username = properties.getProperty("username");
                password = properties.getProperty("password");
    
                //驱动只用加载一次
                Class.forName(driver);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //获得连接
        public static Connection getConnection() throws SQLException {
            return DriverManager.getConnection(url,username,password);
        }
    
        //释放连接资源
        public static void release(Connection conn, PreparedStatement ps, ResultSet rs){
            if(rs != null){
                try {
                    rs.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }
    
  • insert添加

    public class TestInsert {
        public static void main(String[] args) {
            Connection conn = null;
            PreparedStatement ps = null;
    
            try {
                conn = JdbcUtils.getConnection();
    
                //使用?占位符代替参数
                String sql = "insert into users(`id`,`name`,`password`,`email`,`birthday`) values(?,?,?,?,?)";
                ps = conn.prepareStatement(sql);
    
                //给参数手动赋值
                ps.setInt(1,5);
                ps.setString(2,"zhaoliu");
                ps.setString(3,"123456");
                ps.setString(4,"123456@qq.com");
                ps.setDate(5,new java.sql.Date(new Date().getTime()));
    
                //执行
                int i = ps.executeUpdate();
                if(i>0){
                    System.out.println("插入成功");
                }
    
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JdbcUtils.release(conn,ps,null);
            }
        }
    }
    
  • delete删除

    public class TestDelete {
        public static void main(String[] args) {
            Connection conn = null;
            PreparedStatement ps = null;
            try {
                conn = JdbcUtils.getConnection();
                String sql = "delete from users where id = ?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1,5);
                int i = ps.executeUpdate();
                if(i>0){
                    System.out.println("删除成功");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JdbcUtils.release(conn,ps,null);
            }
        }
    }
    
  • update修改

    public class TestUpdate {
        public static void main(String[] args) {
            Connection conn = null;
            PreparedStatement ps = null;
            try {
                conn = JdbcUtils.getConnection();
                String sql = "update users set name = ? where id = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1,"张三");
                ps.setInt(2,1);
                int i = ps.executeUpdate();
                if(i>0){
                    System.out.println("修改成功");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JdbcUtils.release(conn,ps,null);
            }
        }
    }
    
  • select查询

    public class TestSelect {
        public static void main(String[] args) {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
    
            try {
                conn = JdbcUtils.getConnection();
                String sql = "select `name`,`password` from users where `id` = ?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1,1);
                rs = ps.executeQuery();
                while (rs.next()){
                    System.out.println("name=" + rs.getString("name"));
                    System.out.println("password=" +rs.getString("password"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JdbcUtils.release(conn,ps,rs);
            }
    
        }
    }
    
  • 防止SQL注入

    • PreparedStatement 防止SQL注入的本质,把传递进来的参数当做字符,假设其中存在转义字符,比如’'就会直接忽略
    public class SQL {
        public static void main(String[] args) {
    
            //正常登录
            login("张三","123456");
            //SQL注入没用
            //login("'' or 1=1","123456");
        }
    
        //登录业务
        public static void login(String username,String password){
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                conn = JdbcUtils.getConnection();
                String sql = "select * from users where `name` = ? and `password` = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1,username);
                ps.setString(2,password);
                rs = ps.executeQuery();
                while (rs.next()){
                    System.out.println("name="+rs.getString("name"));
                    System.out.println("password="+rs.getString("password"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JdbcUtils.release(conn,ps,rs);
            }
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Remote_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值