JDBC Template之基本使用

JDBC Template基本使用

execute 方法

update与batchUpdate方法

query与queryXXX方法

call方法

update方法

对数据进行增删改操作

batchUpdate方法

批量增删改操作

查询简单数据项

获取一个

获取多个

查询复杂对象(封装为Map)

获取一个

获取多个

查询复杂对象(封装为实体对象)

RowMapper接口

获取一个

获取多个

代码演示

import com.jikewenku.sc.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Test {
    private JdbcTemplate jdbcTemplate;
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
    }

    public void testExecute(){
        jdbcTemplate.execute("create table user1(id int,name varchar(20))");
    }

    public void testUpdate(){
        String sql = "insert into student(name,sex) values(?,?)";
        jdbcTemplate.update(sql,new Object[]{"张飞","男"});
    }

    public void testUpdate2(){
        String sql = "update student set sex=? where id=?";
        jdbcTemplate.update(sql,"女",1003);
    }

    public void testBatchUpdate(){
        String[] sqls={
                "insert into student(name,sex) values('关羽','女')",
                "insert into student(name,sex) values('刘备','男')",
                "update student set sex='女' where id=2001"
        };
        jdbcTemplate.batchUpdate(sqls);
    }

    public void testBatchUpdate2(){
        String sql = "insert into selection(student,course) values(?,?)";
        List list = new ArrayList();
        list.add(new Object[]{1005,1001});
        list.add(new Object[]{1005,1003});
        jdbcTemplate.batchUpdate(sql,list);
    }

    public void testQuerySimple1(){
        String sql = "select count(*) from student";
        int count = jdbcTemplate.queryForObject(sql,Integer.class);
        System.out.println(count);
    }

    public void testQuerySimple2(){
        String sql = "select name from student where sex=?";
        List names = jdbcTemplate.queryForList(sql,String.class,"女");
        System.out.println(names);
    }

    public void testQueryMap1(){
        String sql = "select * from student where id = ?";
        Map stu = jdbcTemplate.queryForMap(sql,1003);
        System.out.println(stu);
    }

    public void testQueryMap2(){
        String sql = "select * from student";
        List> stus = jdbcTemplate.queryForList(sql);
        System.out.println(stus);
    }

    public void testQueryEntity1(){
        String sql = "select * from student where id = ?";
        Student stu = jdbcTemplate.queryForObject(sql, new StudentRowMapper(), 1004);
        System.out.println(stu);
    }
    @org.junit.Test
    public void testQueryEntity2(){
        String sql = "select * from student";
        List stus = jdbcTemplate.query(sql,new StudentRowMapper());
        System.out.println(stus);
    }

    private class StudentRowMapper implements RowMapper{
        public Student mapRow(ResultSet resultSet, int i) throws SQLException {
            Student stu = new Student();
            stu.setId(resultSet.getInt("id"));
            stu.setName(resultSet.getString("name"));
            stu.setSex(resultSet.getString("sex"));
            stu.setBorn(resultSet.getDate("born"));
            return stu;
        }
    }
}
本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
极客文库 » JDBC Template之基本使用

Leave a Reply

欢迎加入「极客文库」,成为原创作者从这里开始!

立即加入 了解更多