package C3p0_Demo;
import Jdbc_Utils.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
public class PingCe {
//1.创建jdbctemplate对象
JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
@Test//修改
public void xiugai(){
//2.SQL语句
String sql = "update emp set salary=10000 where id=?";
//执行sql语句
int i = jt.update(sql, 1005);
System.out.println(i);
}
@Test//添加
public void add(){
String sql = "insert into emp (id,salary,dept_id) values (?,?,?)";
int i = jt.update(sql, 999, 8888, 38);
System.out.println(i);
}
@Test//删除
public void delete(){
String sql = "delete from emp where id=?";
int i = jt.update(sql, 999);
System.out.println(i);
}
@Test//查询所有
public void chaxun(){
String sql = "select * from emp";
List<Map<String, Object>> i = jt.queryForList(sql);
System.out.println(i);
}
@Test//查询
public void cha(){
String sql = "select * from emp";
List<Emp> i = jt.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
// System.out.println(i);
for (Emp emp:i){
System.out.println(emp);
}
}
@Test//Map
public void map(){
String sql = "select * from emp where id=? ";
Map<String, Object> map = jt.queryForMap(sql, 1001);
System.out.println(map);
}
@Test
public void zongshu(){
String sql = "select count(id) from emp";
Long all = jt.queryForObject(sql, Long.class);
System.out.println(all);
}
}