当前位置: 首页 > article >正文

Preparedstatement的使用

关于preparedstatement接口

PreparedStatement也是用来执行sql语句的与创建Statement不同的是,需要根据sql语句创建PreparedStatement。还能够通过设置参数,指定相应的值,而不是Statement那样使用字符串拼接。

其实就是Preparedstatement在外面拼接SQL语句的时候可以使用占位符来指定,相较于Statement,Preparedstatement的灵活性更高。

如何使用Preparedstatement接口

在使用此接口前,我们定义了一个DbUtil作为数据库处理类,具体实现如下

 public Class DbUtils{//1.定义工具类需要的对象protected Connection conn=null;protected PreparedStatement ps=null;protected ResultSet rs=null;protected int k=0;//受影响的行数private driverName="com.mysql.cj.jdbc.Driver";private url="jdbc:mysql://localhost:3306";private userName=//输入你的数据库用户名(一般是root);private password=//输入你的数据库密码;//2.加载驱动static{try{Class.forName(driverName);}catch(ClassNotFoundException ex){ex.printStackTrace();}} //3.获得链接protected Connection getConnection(){try{conn=DriverManger.getConnection(url,userName,password);}catch(SQLException ex){ex.printStackTrace();}return conn;}//4.创建通道protected PreparedStatement getPst(String sql){try{getConnection();ps=conn.preparedStatement(sql); }catch(SQLException ex){ex.printStackTrace();}return ps;}//5.给占位符赋值protected void setParams(List list){try{if(list!=null&&list.size()>0){for(int i=0;i<list.size();i++){ps.setObject(i+1,list.get(i));//赋值从参数1开始}}}catch(SQLException ex){ex.printStackTrace();} }//6.增删改调取的方法protect int update(String sql,List params){try{getPst(sql);setParams(params);k=ps.executeUpdate();}catch(SQLException ex){ex.printStackTrace();}return k;} //7.查询的时候调取的方法protected ResultSet query(String sql,List params){try{getPst(sql);setParams(params);rs=ps.executeQuery();return rs;}catch(SQLException ex){ex.printStackTrace();}return null;}//8.关闭资源protected void closeAll(){try{if(rs!=null){rs.close();}if(ps!=null){ps.close();}if(conn!=null){conn.close();} }catch(SQLException ex){ex.printStackTrace();}}}

接下来我们就可以使用PreparedStatment进行SQL语句的预处理 

         · 下列代码在values后面使用了‘?’占位符

         · 并且将参数courseName拼接到SQL语句中并执行操作

	public void addCourse(String courseName){String sql = "insert into t_course(course_name) values(?)";  //该语句为每个 IN 参数保留一个问号(“?”)作为占位符Connection conn = DbUtil.getConnection();//连接数据库PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);pstmt.setString(1, courseName); //给占位符赋值pstmt.executeUpdate();			//执行//省略trycatch、close处理(根据提示加上即可)}

相较于Statment

Statment是需要在sql语句里面进行String对象的硬编码拼接,逻辑会麻烦许多 

    public void addCourse(courseName){//拼接StringString sql="insert into t1(id) values ("+courseName+")"; Connection conn = DbUtil.getConnection();//连接数据库Statement st = conn.createStatement();st.executeUpdate(sql);//省略trycatch、close处理(根据提示加上即可)}

至此我们会发现相较于statement硬编码的SQL语句,PreparedStament简直是太灵活了! 

http://www.lryc.cn/news/2414547.html

相关文章:

  • 达梦数据库分区表
  • 皮尔森、斯皮尔曼,肯德尔相关系数及其python实现
  • adb工具包的安装和使用
  • 如何在windows上搭建DZ(Discuz!)论坛?
  • Jqgrid教程(简单上手)
  • C# 正则表达式完全指南:基础、进阶与实战示例
  • C语言 | 指针详解
  • Linux命令之pkill命令
  • 必收藏:最全SQL讲解!
  • WSUS服务的部署
  • CTex下载地址和方法
  • Tomcat启动成功但是无法访问http://localhost:8080/
  • gcc编译器及C语言基础
  • ISE工程建立(含IP核建立)和chipscope实验
  • ARM学习-ARM指令集详解
  • 数据结构typedef、L和*L、Elemtype *elem和Elemtype elem[maxsize]
  • Quartus-II13.1三种方式实现D触发器及时序仿真
  • Python详细介绍及使用(基础篇)
  • openfire学习之安装
  • Linux下 itoa,atoi 函数的实现
  • 三维重建(7)--运动恢复结构SfM系统解析
  • 无线渗透----扫描附近WiFi(windows系统)
  • location.href用法总结(转)
  • vue3如何实现使用SortableJs插件进行表格内的数据项拖拽排序
  • 深度学习之目标检测(九)--YOLOv3 SPP理论介绍
  • JenKins 自动化打包上传到服务器的fir 工具
  • CSS基础:插入CSS样式的3种方法
  • 7.1 函数的基本概念和定义
  • linux 环境变量设置(临时 + 永久)
  • BootStrapTable 分页