JDBC中的PreparedStatement对象

2024-04-28 09:58

本文主要是介绍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);}}
    }
    

这篇关于JDBC中的PreparedStatement对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/942952

相关文章

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

Spring中管理bean对象的方式(专业级说明)

《Spring中管理bean对象的方式(专业级说明)》在Spring框架中,Bean的管理是核心功能,主要通过IoC(控制反转)容器实现,下面给大家介绍Spring中管理bean对象的方式,感兴趣的朋... 目录1.Bean的声明与注册1.1 基于XML配置1.2 基于注解(主流方式)1.3 基于Java

C++/类与对象/默认成员函数@构造函数的用法

《C++/类与对象/默认成员函数@构造函数的用法》:本文主要介绍C++/类与对象/默认成员函数@构造函数的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录名词概念默认成员函数构造函数概念函数特征显示构造函数隐式构造函数总结名词概念默认构造函数:不用传参就可以

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

golang 对象池sync.Pool的实现

《golang对象池sync.Pool的实现》:本文主要介绍golang对象池sync.Pool的实现,用于缓存和复用临时对象,以减少内存分配和垃圾回收的压力,下面就来介绍一下,感兴趣的可以了解... 目录sync.Pool的用法原理sync.Pool 的使用示例sync.Pool 的使用场景注意sync.

SpringBoot项目中Redis存储Session对象序列化处理

《SpringBoot项目中Redis存储Session对象序列化处理》在SpringBoot项目中使用Redis存储Session时,对象的序列化和反序列化是关键步骤,下面我们就来讲讲如何在Spri... 目录一、为什么需要序列化处理二、Spring Boot 集成 Redis 存储 Session2.1