• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

spring练习1

武飞扬头像
老菜鸟·
帮助1

1、练习网站案例

1、建好相应的java类

  1.  
    package spring;
  2.  
     
  3.  
    public class Player {
  4.  
    public int getId() {
  5.  
    return id;
  6.  
    }
  7.  
     
  8.  
    public void setId(int id) {
  9.  
    this.id = id;
  10.  
    }
  11.  
     
  12.  
    public String getName() {
  13.  
    return name;
  14.  
    }
  15.  
     
  16.  
    public void setName(String name) {
  17.  
    this.name = name;
  18.  
    }
  19.  
     
  20.  
    public String getPosition() {
  21.  
    return position;
  22.  
    }
  23.  
     
  24.  
    public void setPosition(String position) {
  25.  
    this.position = position;
  26.  
    }
  27.  
     
  28.  
    private int id;
  29.  
    private String name;
  30.  
    private String position;
  31.  
    }
  32.  
     

2、准备好xml文件配置

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  5.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  6.  
    xmlns:context="http://www.springframework.org/schema/context"
  7.  
    xsi:schemaLocation="
  8.  
    http://www.springframework.org/schema/beans
  9.  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10.  
    http://www.springframework.org/schema/aop
  11.  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12.  
    http://www.springframework.org/schema/tx
  13.  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14.  
    http://www.springframework.org/schema/context
  15.  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16.  
     
  17.  
    <bean name="" class="spring.Player">
  18.  
    <property name="name" value="niko" />
  19.  
    <property name="position" value="步枪手" />
  20.  
    </bean>
  21.  
     
  22.  
    </beans>

3、准备好测试类

  1.  
    package text;
  2.  
     
  3.  
    import org.springframework.context.ApplicationContext;
  4.  
    import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
     
  6.  
    import spring.Player;
  7.  
     
  8.  
    public class Test {
  9.  
    public static void main(String[] args) {
  10.  
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
  11.  
     
  12.  
    Player c = (Player) context.getBean("s");
  13.  
     
  14.  
    System.out.println(c.getName() " " c.getPosition());
  15.  
    }
  16.  
    }

没问题:

学新通

 1、来个队伍类

  1.  
    public class Team {
  2.  
    private String teamname;
  3.  
    private String game;
  4.  
     
  5.  
    public String getTeamname() {
  6.  
    return teamname;
  7.  
    }
  8.  
     
  9.  
    public void setTeamname(String teamname) {
  10.  
    this.teamname = teamname;
  11.  
    }
  12.  
     
  13.  
    public String getGame() {
  14.  
    return game;
  15.  
    }
  16.  
     
  17.  
    public void setGame(String game) {
  18.  
    this.game = game;
  19.  
    }
  20.  
    }

 2、配置文件作出相应修改,写一个能创建Team类的bean并在创建Player类的bean中调动他

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  5.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  6.  
    xmlns:context="http://www.springframework.org/schema/context"
  7.  
    xsi:schemaLocation="
  8.  
    http://www.springframework.org/schema/beans
  9.  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10.  
    http://www.springframework.org/schema/aop
  11.  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12.  
    http://www.springframework.org/schema/tx
  13.  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14.  
    http://www.springframework.org/schema/context
  15.  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16.  
     
  17.  
    <bean name="s" class="spring.Player">
  18.  
    <property name="name" value="niko" />
  19.  
    <property name="position" value="步枪手" />
  20.  
    <property name="team" ref="t" />
  21.  
    </bean>
  22.  
    <bean name="t" class="spring.Team">
  23.  
    <property name="teamname" value="G2" />
  24.  
    <property name="game" value="csgo" />
  25.  
    </bean>
  26.  
     
  27.  
     
  28.  
    </beans>

3、测试类

  1.  
    package text;
  2.  
     
  3.  
    import org.springframework.context.ApplicationContext;
  4.  
    import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
     
  6.  
    import spring.Player;
  7.  
     
  8.  
    public class Test {
  9.  
    public static void main(String[] args) {
  10.  
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
  11.  
     
  12.  
    Player c = (Player) context.getBean("s");
  13.  
     
  14.  
    System.out.println(c.getName() " " c.getPosition() " " c.getTeam().getTeamname() " " c.getTeam().getGame());
  15.  
    }
  16.  
    }

没问题

学新通

 1、修改配置文件

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  5.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  6.  
    xmlns:context="http://www.springframework.org/schema/context"
  7.  
    xsi:schemaLocation="
  8.  
    http://www.springframework.org/schema/beans
  9.  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10.  
    http://www.springframework.org/schema/aop
  11.  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12.  
    http://www.springframework.org/schema/tx
  13.  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14.  
    http://www.springframework.org/schema/context
  15.  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16.  
     
  17.  
    <context:annotation-config/>
  18.  
    <bean name="s" class="spring.Player">
  19.  
    <property name="name" value="niko" />
  20.  
    <property name="position" value="步枪手" />
  21.  
    </bean>
  22.  
    <bean name="t" class="spring.Team">
  23.  
    <property name="teamname" value="G2" />
  24.  
    <property name="game" value="csgo" />
  25.  
    </bean>
  26.  
     
  27.  
     
  28.  
    </beans>

2、为player加上注解

  1.  
    package spring;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Autowired;
  4.  
     
  5.  
    public class Player {
  6.  
    public int getId() {
  7.  
    return id;
  8.  
    }
  9.  
     
  10.  
    public void setId(int id) {
  11.  
    this.id = id;
  12.  
    }
  13.  
     
  14.  
    public String getName() {
  15.  
    return name;
  16.  
    }
  17.  
     
  18.  
    public void setName(String name) {
  19.  
    this.name = name;
  20.  
    }
  21.  
     
  22.  
    public String getPosition() {
  23.  
    return position;
  24.  
    }
  25.  
     
  26.  
    public void setPosition(String position) {
  27.  
    this.position = position;
  28.  
    }
  29.  
     
  30.  
    private int id;
  31.  
    private String name;
  32.  
    private String position;
  33.  
     
  34.  
    public Team getTeam() {
  35.  
    return team;
  36.  
    }
  37.  
     
  38.  
    public void setTeam(Team team) {
  39.  
    this.team = team;
  40.  
    }
  41.  
    @Autowired
  42.  
    private Team team;
  43.  
    }
  44.  
     

 没问题

学新通

 试试对bean的注解:

1、修改xml,注意这里指个包名就可以了,不要指明类名

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  5.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  6.  
    xmlns:context="http://www.springframework.org/schema/context"
  7.  
    xsi:schemaLocation="
  8.  
    http://www.springframework.org/schema/beans
  9.  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10.  
    http://www.springframework.org/schema/aop
  11.  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12.  
    http://www.springframework.org/schema/tx
  13.  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14.  
    http://www.springframework.org/schema/context
  15.  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16.  
     
  17.  
    <context:component-scan base-package="spring"/>
  18.  
     
  19.  
     
  20.  
    </beans>

 2、添加相关注释

  1.  
    package spring;
  2.  
     
  3.  
    import org.springframework.stereotype.Component;
  4.  
     
  5.  
    @Component("T")
  6.  
    public class Team {
  7.  
    private String teamname = "navi";
  8.  
    private String game = "csgo";
  9.  
     
  10.  
    public String getTeamname() {
  11.  
    return teamname;
  12.  
    }
  13.  
     
  14.  
    public void setTeamname(String teamname) {
  15.  
    this.teamname = teamname;
  16.  
    }
  17.  
     
  18.  
    public String getGame() {
  19.  
    return game;
  20.  
    }
  21.  
     
  22.  
    public void setGame(String game) {
  23.  
    this.game = game;
  24.  
    }
  25.  
    }
  26.  
     
  1.  
    package spring;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Autowired;
  4.  
    import org.springframework.stereotype.Component;
  5.  
     
  6.  
    @Component("P")
  7.  
    public class Player {
  8.  
    public int getId() {
  9.  
    return id;
  10.  
    }
  11.  
     
  12.  
    public void setId(int id) {
  13.  
    this.id = id;
  14.  
    }
  15.  
     
  16.  
    public String getName() {
  17.  
    return name;
  18.  
    }
  19.  
     
  20.  
    public void setName(String name) {
  21.  
    this.name = name;
  22.  
    }
  23.  
     
  24.  
    public String getPosition() {
  25.  
    return position;
  26.  
    }
  27.  
     
  28.  
    public void setPosition(String position) {
  29.  
    this.position = position;
  30.  
    }
  31.  
     
  32.  
    private int id;
  33.  
    private String name = "s1mple";
  34.  
    private String position = "狙击手";
  35.  
     
  36.  
    public Team getTeam() {
  37.  
    return team;
  38.  
    }
  39.  
     
  40.  
    public void setTeam(Team team) {
  41.  
    this.team = team;
  42.  
    }
  43.  
    @Autowired
  44.  
    private Team team;
  45.  
    }
  46.  
     

 没毛病:

学新通

2、教材练习 

setter注入之前已经用过,现在试试构造器注入

1、给team类加个带参构造方法

  1.  
    package spring;
  2.  
     
  3.  
    import org.springframework.stereotype.Component;
  4.  
     
  5.  
    public class Team {
  6.  
    private String teamname;
  7.  
    private String game;
  8.  
     
  9.  
    public Team(String teamname ,String game){
  10.  
    this.teamname = teamname;
  11.  
    this.game = game;
  12.  
    }
  13.  
     
  14.  
    public String getTeamname() {
  15.  
    return teamname;
  16.  
    }
  17.  
     
  18.  
    public void setTeamname(String teamname) {
  19.  
    this.teamname = teamname;
  20.  
    }
  21.  
     
  22.  
    public String getGame() {
  23.  
    return game;
  24.  
    }
  25.  
     
  26.  
    public void setGame(String game) {
  27.  
    this.game = game;
  28.  
    }
  29.  
    }
  30.  
     

 2、创建相应xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  5.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  6.  
    xmlns:context="http://www.springframework.org/schema/context"
  7.  
    xsi:schemaLocation="
  8.  
    http://www.springframework.org/schema/beans
  9.  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10.  
    http://www.springframework.org/schema/aop
  11.  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12.  
    http://www.springframework.org/schema/tx
  13.  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14.  
    http://www.springframework.org/schema/context
  15.  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16.  
     
  17.  
    <bean name="t" class="spring.Team">
  18.  
    <constructor-arg>
  19.  
    <value>faze</value>
  20.  
    </constructor-arg>
  21.  
    <constructor-arg>
  22.  
    <value>csgo</value>
  23.  
    </constructor-arg>
  24.  
    </bean>
  25.  
     
  26.  
     
  27.  
    </beans>

3、测试类

  1.  
    package text;
  2.  
     
  3.  
    import org.springframework.context.ApplicationContext;
  4.  
    import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
     
  6.  
    import spring.Player;
  7.  
    import spring.Team;
  8.  
     
  9.  
    public class Test {
  10.  
    public static void main(String[] args) {
  11.  
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
  12.  
     
  13.  
    Team c = (Team) context.getBean("t");
  14.  
     
  15.  
    System.out.println(c.getTeamname() " " c.getGame());
  16.  
    }
  17.  
    }

 没毛病

学新通

接下来我们试试教材dao模式操作

 1、类还是用我们之前的Player类

  1.  
    package spring;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Autowired;
  4.  
    import org.springframework.stereotype.Component;
  5.  
     
  6.  
    public class Player {
  7.  
    public int getId() {
  8.  
    return id;
  9.  
    }
  10.  
     
  11.  
    public void setId(int id) {
  12.  
    this.id = id;
  13.  
    }
  14.  
     
  15.  
    public String getName() {
  16.  
    return name;
  17.  
    }
  18.  
     
  19.  
    public void setName(String name) {
  20.  
    this.name = name;
  21.  
    }
  22.  
     
  23.  
    public String getPosition() {
  24.  
    return position;
  25.  
    }
  26.  
     
  27.  
    public void setPosition(String position) {
  28.  
    this.position = position;
  29.  
    }
  30.  
     
  31.  
    private int id;
  32.  
    private String name;
  33.  
    private String position;
  34.  
     
  35.  
    }
  36.  
     

 2、来一个接口,这个接口可以不止抽象定义一种方法,后面可以按照需求定义多个方法,在这里只是尝试一个方法。

  1.  
    package impl;
  2.  
     
  3.  
    import spring.Player;
  4.  
     
  5.  
    public interface Playerdaoimpl {
  6.  
    public void inserplayer(Player p);
  7.  
    }

3、定义一个dao类。这里用到了try。。。finally异常处理方法,这种方式简单来说就是尝试try中的代码,不管是否成功,最终都要执行finally中的代码

  1.  
    package dao;
  2.  
     
  3.  
    import java.sql.Connection;
  4.  
    import java.sql.SQLException;
  5.  
    import java.sql.Statement;
  6.  
     
  7.  
    import javax.sql.DataSource;
  8.  
     
  9.  
    import impl.Playerdaoimpl;
  10.  
    import spring.Player;
  11.  
     
  12.  
    public class Playerdao implements Playerdaoimpl{
  13.  
    private DataSource dataSource;//注入DataSource
  14.  
    public DataSource getDataSource() {
  15.  
    return dataSource;
  16.  
    }
  17.  
    public void setDataSource(DataSource dataSource) {
  18.  
    this.dataSource = dataSource;
  19.  
    }
  20.  
    public void inserplayer(Player p){
  21.  
    String name = p.getName();
  22.  
    String position = p.getPosition();
  23.  
    Connection conn = null;
  24.  
    Statement stmt = null;
  25.  
    try {
  26.  
    conn = dataSource.getConnection();//获取数据库连接
  27.  
    stmt = conn.createStatement();
  28.  
    stmt.execute("insert into player (name,position) "
  29.  
    "values('" name "','" position "')");
  30.  
    } catch (SQLException e) {
  31.  
    e.printStackTrace();
  32.  
    }
  33.  
    finally {
  34.  
    if(stmt != null) {
  35.  
    try {
  36.  
    stmt.close();//关闭Statement对象
  37.  
    }
  38.  
    catch(SQLException e) {
  39.  
    e.printStackTrace();
  40.  
    }
  41.  
    }
  42.  
    if(conn != null) {
  43.  
    try {
  44.  
    conn.close();//关闭数据库连接
  45.  
    }
  46.  
    catch(SQLException e) {
  47.  
    e.printStackTrace();
  48.  
    }
  49.  
    }
  50.  
    }
  51.  
    }
  52.  
    }

4、配置文件,注意对应好自己的数据库相关信息

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans
  3.  
    xmlns="http://www.springframework.org/schema/beans"
  4.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6.  
    <!-- 配置数据源 -->
  7.  
    <bean id="dataSource"
  8.  
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  9.  
    <property name="driverClassName">
  10.  
    <value>com.mysql.jdbc.Driver</value>
  11.  
    </property>
  12.  
    <property name="url">
  13.  
    <value>jdbc:mysql://localhost:3306/test
  14.  
    </value>
  15.  
    </property>
  16.  
    <property name="username">
  17.  
    <value>root</value>
  18.  
    </property>
  19.  
    <property name="password">
  20.  
    <value>123456</value>
  21.  
    </property>
  22.  
    </bean>
  23.  
    <bean id="userDAO" class="com.mr.dao.UserDAO">
  24.  
    <property name="dataSource">
  25.  
    <ref local="dataSource"/>
  26.  
    </property>
  27.  
    </bean>
  28.  
    </beans>

 5、创建好相应的数据库

  1.  
    CREATE TABLE player (
  2.  
    id INT NOT NULL AUTO_INCREMENT,
  3.  
    name VARCHAR(50) NOT NULL,
  4.  
    position VARCHAR(50),
  5.  
    PRIMARY KEY (id)
  6.  
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

6、创建测试类

  1.  
    package text;
  2.  
     
  3.  
    import org.springframework.context.ApplicationContext;
  4.  
    import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
     
  6.  
    import dao.Playerdao;
  7.  
    import spring.Player;
  8.  
     
  9.  
    public class Test {
  10.  
    public static void main(String[] args) {
  11.  
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
  12.  
     
  13.  
    Player p = new Player("simple" ,"狙击手");
  14.  
    Playerdao dao = (Playerdao) context.getBean("playerdao");
  15.  
    dao.inserplayer(p);
  16.  
    System.out.println("成功");
  17.  
    }
  18.  
    }

报了个错,是字符编码问题

  1.  
    java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
  2.  
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
  3.  
    at com.mysql.jdbc.Connection.configureClientCharacterSet(Connection.java:2412)
  4.  
    at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:4139)
  5.  
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2789)
  6.  
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
  7.  
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
  8.  
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
  9.  
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
  10.  
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
  11.  
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
  12.  
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
  13.  
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
  14.  
    at dao.Playerdao.inserplayer(Playerdao.java:26)
  15.  
    at text.Test.main(Test.java:15)

 我们修改xml文件中url为

  1.  
    <property name="url">
  2.  
    <value>jdbc:mysql://localhost:3306/how2java?useUnicode=true&amp;characterEncoding=UTF-8</value>
  3.  
    </property>

再次运行,没问题

学新通

 最后用用JdbcTemple:

1、配置xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans
  3.  
    xmlns="http://www.springframework.org/schema/beans"
  4.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6.  
    <!-- 配置数据源 -->
  7.  
    <bean id="dataSource"
  8.  
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  9.  
    <property name="url">
  10.  
    <value>jdbc:mysql://localhost:3306/how2java?useUnicode=true&amp;characterEncoding=UTF-8</value>
  11.  
    </property>
  12.  
    <property name="username">
  13.  
    <value>root</value>
  14.  
    </property>
  15.  
    <property name="password">
  16.  
    <value>123456</value>
  17.  
    </property>
  18.  
    </bean>
  19.  
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  20.  
    <property name="dataSource">
  21.  
    <ref local="dataSource"/>
  22.  
    </property>
  23.  
    </bean>
  24.  
    </beans>
  1.  
    package text;
  2.  
     
  3.  
    import org.springframework.context.ApplicationContext;
  4.  
    import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
    import org.springframework.jdbc.core.JdbcTemplate;
  6.  
     
  7.  
    import dao.Playerdao;
  8.  
    import spring.Player;
  9.  
     
  10.  
    public class Test {
  11.  
    public static void main(String[] args) {
  12.  
    JdbcTemplate jtl = null;
  13.  
    ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");//获取配置文件
  14.  
    jtl =(JdbcTemplate)factory.getBean("jdbcTemplate");
  15.  
    String sql = "insert into player(name,position) values ('niko' ,'步枪手')";
  16.  
    jtl.update(sql);
  17.  
    System.out.println("添加操作执行成功");
  18.  
    }
  19.  
    }

没问题

学新通

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhbaii
系列文章
更多 icon
同类精品
更多 icon
继续加载