必须先插入工具包
代码
public static void main(String[] args) {
DataSource ds = JdbcHelper.getDs();
System.out.println(ds);
JdbcTemplate jdbcTemplate=new JdbcTemplate(ds);
System.out.println(jdbcTemplate);
//新增
String sql1="insert into biao values(null,?,?,?)";
int i=jdbcTemplate.update(sql1,"6",6,"6");
System.out.println(i);
//删除
String sql2="delete from biao where id=?";
int a=jdbcTemplate.update(sql2,"2");
System.out.println(a);
//修改
String sql3="update biao set name=? where id=2";
int b=jdbcTemplate.update(sql3,"小军");
System.out.println(b);
//查询单个数据
String sql4="select *from biao where id=?";
Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql4,3);
System.out.println(stringObjectMap);
//查询多个数据
String sql5="select * from biao where id>?";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql5, 5);
System.out.println(maps);
String sql6="select id(3) from biao where age>?";
String s = jdbcTemplate.queryForObject(sql6, String.class, 20);
System.out.println(s);
}
!!!查询!!!
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/0e9185bf852740c584658d608c3101af.png
查询
public static void main(String[] args) {
DataSource ds = JdbcHelper.getDs();
System.out.println(ds);
JdbcTemplate jdbcTemplate=new JdbcTemplate(ds);
System.out.println(jdbcTemplate);
String sql="select * from biao where id>?";
BeanPropertyRowMapper<Test2> rowMapper=new BeanPropertyRowMapper<>(Test2.class);
List<Test2> query=jdbcTemplate.query(sql,rowMapper,5);
System.out.println(query);
for(Test2 bean:query){
System.out.println(bean);
int id = bean.getId();
System.out.println(id);
}
}