Spring10 : JdbcTemplate

  1. 编码方式

    • pom.xml



      4.0.0

      com.greyson
      jdbctTemplates
      1.0-SNAPSHOT
      jar



      org.springframework
      spring-context
      5.0.7.RELEASE


      org.springframework
      spring-jdbc
      5.0.2.RELEASE


      org.springframework
      spring-tx
      5.0.2.RELEASE


      mysql
      mysql-connector-java
      5.1.6

    • Account

      /**
      * Account domain class
      *
      */
      public class Account implements Serializable {

      private Integer Id;
      private String name;
      private Float money;

      public Integer getId() {
      return Id;
      }

      public void setId(Integer id) {
      Id = id;
      }

      public String getName() {
      return name;
      }

      public void setName(String name) {
      this.name = name;
      }

      public Float getMoney() {
      return money;
      }

      public void setMoney(Float money) {
      this.money = money;
      }

      @Override
      public String toString() {
      return "Account{" +
      "Id=" + Id +
      ", name='" + name + ''' +
      ", money=" + money +
      '}';
      }
      }

    • JdbcTemplateDemo

      /**
      * The Basic Usage of JdbcTemplate
      */
      public class JdbcTemplateDemo {

      public static void main(String[] args) {
      // prepare dataSource
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql://localhost:3306/eesy");
      dataSource.setUsername("root");
      dataSource.setPassword("HotteMYSQL");
      // 1. create the object of JdbcTemplate
      JdbcTemplate jdbcTemplate = new JdbcTemplate();
      jdbcTemplate.setDataSource(dataSource);
      // 2. execute operation
      jdbcTemplate.execute("insert into account(name, money)values('aaa', 1000)");
      }
      }

  2. 配置方式

    添加配置文件ApplicationContext.xml以及修改JdbcTemplateDemo

    • ApplicationContext.xml












    • JdbcTemplateDemo2

      /**
      * The Basic Usage of JdbcTemplate
      */
      public class JdbcTemplateDemo {

      public static void main(String[] args) {
      // prepare dataSource
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql://localhost:3306/eesy");
      dataSource.setUsername("root");
      dataSource.setPassword("HotteMYSQL");
      // 1. create the object of JdbcTemplate
      JdbcTemplate jdbcTemplate = new JdbcTemplate();
      jdbcTemplate.setDataSource(dataSource);
      // 2. execute operation
      jdbcTemplate.execute("insert into account(name, money)values('aaa', 1000)");
      }
      }

posted @ 2020-06-23 13:30  Plorde  阅读(88)  评论(0)    收藏  举报