zoukankan      html  css  js  c++  java
  • spring入门之JdbcTemplate 操作crud

    Spring 通过调用 JdbcTemplate来实现对数据库的增删改查,主要用到JdbcTemplate类的4个方法,
    首先,配置数据库信息,创建对象,代码通用:
    //设置数据库信息
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/user");
    dataSource.setUsername("root");
    dataSource.setPassword("123456");
    
    //创建jdbcTemplate对象,设置数据源
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    

      1、修改、删除、插入 jdbcTemplate.update(sql,参数) sql示例:update users set age=? where name=? delete from users where name=? insert into users(name,age) values(?,?)

     例子:String sql = "update users set age=? where name=? ";
        int rows = jdbcTemplate.update(sql ,20,"zhaong");

    2、查询一个统计值的情况,用jdbcTemplate.queryForObject():
    String sql = "select count(*) from users";
      int rows = jdbcTemplate.queryForObject(sql,Integer.class);
    3、查询结果是一个对象object,用jdbcTemplate.queryForObject
    例:String sql = "select * from users where name=? ";
    User user = jdbcTemplate.queryForObject(sql, new MyRowMapper(),"china");
    4、查询结果是一个列表list:jdbcTemplate.queryForObject
      例:String sql = "select * from users";
       List<User> list = jdbcTemplate.query(sql, new MyRowMapper());
     1 package spring.jdbc;
     2 
     3 public class User {
     4     private String name;
     5     private int age;
     6     public String getName() {
     7         return name;
     8     }
     9     @Override
    10     public String toString() {
    11         return "User [name=" + name + ", age=" + age + "]";
    12     }
    13     public void setName(String name) {
    14         this.name = name;
    15     }
    16     public int getAge() {
    17         return age;
    18     }
    19     public void setAge(int age) {
    20         this.age = age;
    21     }
    22     
    23 
    24 }

     JdbcTemplateDemo.java 测试代码

      1 package spring.jdbc;
      2 
      3 import java.sql.ResultSet;
      4 import java.sql.SQLException;
      5 import java.util.List;
      6 
      7 import org.junit.Test;
      8 import org.springframework.jdbc.core.JdbcTemplate;
      9 import org.springframework.jdbc.core.RowMapper;
     10 import org.springframework.jdbc.datasource.DriverManagerDataSource;
     11 
     12 public class JdbcTemplateDemo {
     13     
     14     
     15     public void testObject() {
     16         //设置数据库信息
     17         DriverManagerDataSource dataSource = new DriverManagerDataSource();
     18         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
     19         dataSource.setUrl("jdbc:mysql://localhost:3306/user");
     20         dataSource.setUsername("root");
     21         dataSource.setPassword("123456");
     22         
     23         //创建jdbcTemplate对象,设置数据源
     24         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
     25         
     26         //调用 JDBCteMPLATE 对象时面的方法实现操作
     27         
     28         //创建SQL语句
     29         String sql = "select * from users where name=? ";
     30         //调用方法得到记录
     31         User user = jdbcTemplate.queryForObject(sql, new MyRowMapper(),"china");
     32         System.out.println(user);
     33     }
     34     
     35     public void testList() {
     36         //设置数据库信息
     37         DriverManagerDataSource dataSource = new DriverManagerDataSource();
     38         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
     39         dataSource.setUrl("jdbc:mysql://localhost:3306/user");
     40         dataSource.setUsername("root");
     41         dataSource.setPassword("123456");
     42         
     43         //创建jdbcTemplate对象,设置数据源
     44         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
     45         //创建SQL语句
     46         String sql = "select * from users";
     47         //调用 JDBCteMPLATE 对象时面的方法实现操作
     48         List<User> list = jdbcTemplate.query(sql, new MyRowMapper());
     49         
     50         System.out.println(list);
     51     }    
     52     @Test
     53     public void testCount() {
     54         //设置数据库信息
     55         DriverManagerDataSource dataSource = new DriverManagerDataSource();
     56         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
     57         dataSource.setUrl("jdbc:mysql://localhost:3306/user");
     58         dataSource.setUsername("root");
     59         dataSource.setPassword("123456");
     60         
     61         //创建jdbcTemplate对象,设置数据源
     62         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
     63         
     64         //调用 JDBCteMPLATE 对象时面的方法实现操作
     65         
     66         //创建SQL语句,查询一个统计数值
     67         String sql = "select count(*) from users";
     68         int rows = jdbcTemplate.queryForObject(sql,Integer.class);
     69         System.out.println(rows);
     70     }
     71     
     72     public void update() {
     73         //设置数据库信息
     74         DriverManagerDataSource dataSource = new DriverManagerDataSource();
     75         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
     76         dataSource.setUrl("jdbc:mysql://localhost:3306/user");
     77         dataSource.setUsername("root");
     78         dataSource.setPassword("123456");
     79         
     80         //创建jdbcTemplate对象,设置数据源
     81         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
     82         
     83         //调用 JDBCteMPLATE 对象时面的方法实现操作
     84         
     85         //创建SQL语句
     86         String sql = "update users set age=? where name=? ";
     87         int rows = jdbcTemplate.update(sql ,20,"zhaong");
     88         System.out.println(rows);
     89     }
     90     
     91     public void add() {
     92         //设置数据库信息
     93         DriverManagerDataSource dataSource = new DriverManagerDataSource();
     94         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
     95         dataSource.setUrl("jdbc:mysql://localhost:3306/user");
     96         dataSource.setUsername("root");
     97         dataSource.setPassword("123456");
     98         
     99         //创建jdbcTemplate对象,设置数据源
    100         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    101         
    102         //调用 JDBCteMPLATE 对象时面的方法实现操作
    103         
    104         //创建SQL语句
    105         String sql = "insert into users(name,age) values(?,?)";
    106         int rows = jdbcTemplate.update(sql ,"zhaorng",50);
    107         System.out.println(rows);
    108     }
    109     
    110     
    111     
    112 }
    113 
    114 class MyRowMapper implements RowMapper<User>{
    115     
    116     @Override
    117     public User mapRow(ResultSet rs ,int num) throws SQLException{
    118         //1从结果集里把数据午到
    119         String name = rs.getString("name");
    120         int age = rs.getInt("age");
    121         
    122         //2把得到的数据封装到对象里面
    123         User user = new User();
    124         user.setName(name);
    125         user.setAge(age);
    126         return user;
    127     }
    128     
    129     
    130 }
  • 相关阅读:
    word设置的密码忘了怎么办?
    Navicat Report Viewer 设置 HTTP 的方法
    如何处理Navicat Report Viewer 报表
    excel密码忘记了怎么办
    Beyond Compare文本比较搜索功能详解
    Popular Cows POJ
    Problem B. Harvest of Apples HDU
    网络流模型整理
    The Shortest Statement CodeForces
    Vasya and Multisets CodeForces
  • 原文地址:https://www.cnblogs.com/lrzy/p/8391710.html
Copyright © 2011-2022 走看看