Spring JDBC之update、query方法

  • 1、项目清单
  • 2、全部代码
    • 2.1、student
    • 2.2、stuDao
    • 2.3、applicationContext.xml
    • 2.4、test
  • 3、结果
  • 4、笔记
  • 5、源码下载

1、项目清单


所用jar包:

2、全部代码

2.1、student

package spring.jdbc.entity;public class student {//属性与数据库的student表的列名一样String Sno;String Sname;String Sex;String Sbrithday;String Sclass;//设置了有参构造方法,系统的默认的无参构造方法被覆盖,需自己手动建立一个无参构造方法以便在其他地方实例化对象public student() {}//设置有参构造方法,以便test中直接给对象赋值public student(String a,String b,String c,String d,String e) {Sno=a;Sname=b;Sex=c;Sbrithday=d;Sclass=e;}public String getSno() {return Sno;}public void setSno(String sno) {Sno = sno;}public String getSname() {return Sname;}public void setSname(String sname) {Sname = sname;}public String getSex() {return Sex;}public void setSex(String sex) {Sex = sex;}public String getSbrithday() {return Sbrithday;}public void setSbrithday(String sbrithday) {Sbrithday = sbrithday;}public String getSclass() {return Sclass;}public void setSclass(String sclass) {Sclass = sclass;}public void tostring() {System.out.println(Sno+" "+Sname+" "+Sex+" "+Sbrithday+" "+Sclass);}
}

2.2、stuDao

package spring.jdbc.dao;import java.util.List;
import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;import com.mysql.cj.result.StringValueFactory;import spring.jdbc.entity.student;
@Repository("stuDao")
public class stuDao {@Autowiredprivate JdbcTemplate jdbcTemplate;public int addstudent(student s) {String sql="insert into student(Sno,Sname,Sex,Sbirthday,Sclass) values(?,?,?,?,?)";//将参数s的键值存到数组对象中Object[] obj=new Object[] {s.getSno(),s.getSname(),s.getSex(),s.getSbrithday(),s.getSclass(),};//执行添加操作,update方法返回的是操作数据库后受影响的行数int num=this.jdbcTemplate.update(sql, obj);return num;}public int updateNamebySno(student s) {String sql="update student set Sname=? where Sno=?";Object[] obj =new Object[] {s.getSname(),s.getSno(),};int num =this.jdbcTemplate.update(sql, obj);return num;}public int deletestudentbySno(String sno) {String sql="delete from student  where Sno=?";int num =this.jdbcTemplate.update(sql, sno);return num;}public List<student> findAllstudent(){String sql="select * from student";RowMapper<student> rowMapper=new BeanPropertyRowMapper<student>(student.class);return this.jdbcTemplate.query(sql, rowMapper);}public String findNameBySno(String sno) {String sql="select Sname from student where Sno=?";String name=this.jdbcTemplate.queryForObject(sql, new Object[] {sno}, String.class);return name;}public int findNumbers() {String sql="select COUNT(*) from student";int total =this.jdbcTemplate.queryForObject(sql, Integer.class);return total;}public List<Map<String, Object>> findScBySno(String sno){String sql="select sc.Sno,Sname,Cname,grade from student,sc,course where student.sno=sc.sno and course.cno=sc.cno"+" and sc.sno=?";return this.jdbcTemplate.queryForList(sql, new Object[] {sno});}}

2.3、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置数据源 --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/spring"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><!-- 配置jdbc模板 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 扫描包,使注解生效 --><context:component-scan base-package="spring.jdbc"></context:component-scan>
</beans>

2.4、test

package spring.jdbc.test;import java.util.List;
import java.util.Map;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;import spring.jdbc.dao.stuDao;
import spring.jdbc.entity.student;public class test {public static void main(String[] args) {ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");student s=new student("027","猪猪侠","女","2001年2月16日","19软件本科3班");student s2=new student("027","猪猪猪侠","女","2001年2月16日","19软件本科3班");stuDao sd=(stuDao) context.getBean("stuDao");
//      System.out.println("添加学生信息受影响行数:"+sd.addstudent(s));
//      System.out.println("修改学生信息受影响行数:"+sd.updateNamebySno(s2));
//      System.out.println("删除学生信息受影响行数:"+sd.deletestudentbySno("001"));//      System.out.println("查询所有学生记录");
//      List<student> list=sd.findAllstudent();
//      for(student st:list) st.tostring();//       System.out.println("查询025学生记录="+sd.findNameBySno("025"));
//      System.out.println("学生人数="+sd.findNumbers());List<Map<String, Object>> list2=sd.findScBySno("005");for(String k:list2.get(0).keySet()) System.out.print(k+" "); System.out.println();for(Map<String, Object> m:list2) {for(String k:m.keySet())  System.out.print(m.get(k)+" ");System.out.println();}}
}

3、结果

4、笔记

  • 增、删、改所使用的都是update()方法,所返回的都是受影响的行数。
  • 对于增、删、改需要更改多个数据的操作,将其封装在一个数组当中再赋值。
  • query方法则是用于处理数据库表的各种查询操作。
  • public List<student> findAllstudent方法中查找的是学生对象,所返回的是多个学生对象,所以返回值是List<student>,query方法执行sql语句并通过参数rowMapper返回一个list数据类型的结果。
  • queryForObject(sql, new Object[] {sno}, String.class);方法中new Object[] {sno}是负责传递实参,String.class是指明该方法的返回类型为String类型。与之对应的有queryForObject(sql, Integer.class)方法,该方法没有实参传递,Integer.class是指该方法返回的是int数据类型。
  • public List<Map<String, Object>> findScBySno(String sno)方法返回的是List<Map<String, Object>>,它是指返回以键值对形式的列表。与之对应使用的queryForList方法返回的也是list类型的数据。

5、源码下载

源代码下载点击此处

Java EE---通过Spring JDBC实现数据库的增、删、改、查相关推荐

  1. 【django】数据库操作-增 删 改

    一.增加数据 增加数据有两种⽅法. 1)save 通过创建模型类对象,执⾏对象的save()⽅法保存到数据库中. f1=FilmInfo(fname='我爱你中国',pub_data='2021-10 ...

  2. Linux技术--mysql数据库增-删-改-查

    # mysql 数据库 ## 数据库的操作 ### 五个单位 * 数据库服务器   Linux或者 windows  * 数据库  * 数据表 * 数据字段 * 数据行 ### 连接数据库 ``` 1 ...

  3. 学生信息管理系统——JAVA 语言版(主页面+增+删+改+查+退)

    学生信息管理系统 前言 一.问题分析 二.学生信息管理系统程序实现思路 三.Student类的创建 程序思路 Student类代码 四.StudentManager类的创建 程序思路 StudentM ...

  4. 简单的php数据库操作类代码(增,删,改,查)

    数据库操纵基本流程为: 1.连接数据库服务器 2.选择数据库 3.执行SQL语句 4.处理结果集 5.打印操作信息 其中用到的相关函数有 •resource mysql_connect ( [stri ...

  5. php数据库可转java数据库,php转java 系列2 Spring boo 链接数据库jdbc

    php转java 系列2 Spring boo 链接数据库jdbc JDBC 首先创建一个新项目,在创建项目时要注意导入依赖, 在项目创建成功后就会看到在 pom.xml 文件中找到,但是如果在创建项 ...

  6. java ee架构_与Java EE和Spring的集成架构

    java ee架构 本周在纽约举行的O'Reilly软件体系结构大会将举行 . 我很高兴与Josh Long一起提供有关如何集成Java EE和Spring的教程. 一段时间以来,我们一直在开玩笑. ...

  7. 与Java EE和Spring的集成架构

    本周在纽约举行的O'Reilly软件体系结构大会将举行 . 我很高兴与Josh Long一起提供了有关如何集成Java EE和Spring的教程. 一段时间以来,我们一直在开玩笑. 某些人想到的对两种 ...

  8. Java系列技术之JDBC操作数据库-钟洪发-专题视频课程

    Java系列技术之JDBC操作数据库-22人已学习 课程介绍         JDBC连接数据库是Java系列技术中数据库知识的核心技术,是学习后续课程JavaWeb入门前需要掌握的基础! 这门课的前 ...

  9. Java SE、Java EE 与 Spring

    JavaSE.JavaEE 与Spring的概念 在Java世界中,很多人都对Java SE.J2EE.Java EE.Spring.EJB等这些术语感到困惑. 什么是Java SE 可以说这是Jav ...

最新文章

  1. 重启网卡服务_Linux下查看不到物理网卡配置
  2. bootstrap的php写在哪里,bootstrap用什么编辑器写
  3. anaconda在ubuntu中安装后没有_你的大数据平台中病毒了!!!记一次HDP安装后中dr.who病毒并修复的过程...
  4. linux通过SSH连接的SSH加密原理(笔记自用)
  5. python怎么编写视觉识别_Python视觉识别--OpenCV色彩空间\图像运算\ROI(四)
  6. 了解ThreadLocal背后的概念
  7. NFS配置及开机自动挂载
  8. Linux操作系统下/etc/hosts文件
  9. /etc/sudoers文件的分析以及sudo的高级用法
  10. struct sockaddr 和 sockaddr_in 的区别
  11. 判断请求是通过点击链接还是直接输入网址
  12. 遇到一个难题:如何从java中调用到C的功能
  13. java程序如何提取数据库json格式_java解析json格式文件,再保存在数据库怎么做?...
  14. Linux中编译mdio命令,Linux 下smi/mdio总线通信
  15. connection linux refuse telnet_解决telnet无法连接 Connection refused
  16. 从零开始再造打爆李世石的AlphaGo:快速构建棋盘和围棋规则
  17. 还有比元宇宙更牛的商业模式吗?
  18. SpringBoot 3.0 来啦!
  19. IFSC的完整形式是什么?
  20. Ngnix+Tomcat配置负载均衡

热门文章

  1. Xcode @property attributes (nonatomic, copy, st...
  2. 虚拟化---简单高效的IT管理模型
  3. Nginx工作原理及优化参数配置
  4. 字节一实习生误删公司所有lite模型,几百人为其善后,有员工处理事故到凌晨三点!...
  5. 你的简历写了 “熟悉” zookeeper ?那这些你会吗?
  6. 深入理解 Java 虚拟机-如何利用 VisualVM 对高并发项目进行性能分析
  7. 用一个创业故事串起操作系统原理(二)
  8. 做了这么多年的研发,其实你一点也不懂购物车的设计思路!
  9. 关于Kafka Spring Boot的教程
  10. 并行化-你的高并发大杀器