JDBCTemplate的执行语句(增删改查)

package demo;

import java.util.List;
import java.util.Map;
import javax.swing.text.html.HTMLDocument.Iterator;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;
import util.student;

public class JDBCDemo1 {
public static void main(String[] args) {
JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
//修改一条数据
int count=template.update(“update score set chinese=60 where id=?”,1);
System.out.println(count+“修改数据成功”);
//添加一条记录
count =count +template.update(“insert into score values(5,‘pinky’,88,98,76)”);
System.out.println(count+“添加数据成功”);
//删除上一步添加的记录
count =count +template.update(“delete from score where id=?”,5);
System.out.println(count+“删除数据成功”);
//查询id为1的记录,将其封装为Map集合
Map map1=template.queryForMap(“select * from score where id =1”);
System.out.println(map1);
//查询所有记录,将其封装为List
List<Map<String, Object>> list1 = template.queryForList(“select * from score”);
for (Map<String, Object> map : list1) {
System.out.println(map);
}
//查询所有记录,将其封装为student对象的List集合
List list = template.query(“select * from score”,new BeanPropertyRowMapper(student.class));
for (student student : list) {
System.out.println(student);
}
//查询总记录数
Long long1 = template.queryForObject(“select count(id) from score”, Long.class);
System.out.println(long1);
}
}
在这里插入图片描述
在这里插入图片描述

发布了100 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/lena7/article/details/99003782