• JdbcTemplate的使用


    要使用jdbcTemplate操作数据库,要先配置datasource跟jdbcTemplate。

    <!--使用bean的方式配置dataSource,注意导入的包是import javax.sql.DataSource;
    为NewSpringDAO包里的Book2DAO的实例配置属性值-->
    <bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
    <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
    </property>
    <property name="url">
    <value>jdbc:sqlserver://localhost:1433;DatabaseName=Book</value>
    </property>
    <property name="username">
    <value>sa</value>
    </property>
    <property name="password">
    <value>123456</value>
    </property>
    </bean>

    <!--使用JdbcTemplate操作数据库。为它配置bean,然后确保数据库有dataSource2这个bean,
    即保存加载数据库信息的bean。然后具体使用可以参照NewSpringDAO包里的jdbcTemplateMain类-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
    <ref bean="dataSource2"></ref>
    </property>
    </bean>



    public class jdbcTemplateMain {
    public static void main(String[]args){
    ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");
    JdbcTemplate jdbcTemplate=(JdbcTemplate)ctx.getBean("jdbcTemplate");
    /** 批量加入操作
    String sql="insert into Bookimformation values(?,?,?,?,?)";
    List<Object[]>batchArgs=new ArrayList<>();
    batchArgs.add(new Object[]{100088,"yzz",70,"yzz",9000});
    batchArgs.add(new Object[]{100077,"yzz",70,"yzz",9000});
    batchArgs.add(new Object[]{100066,"yzz",70,"yzz",9000});
    batchArgs.add(new Object[]{100055,"yzz",70,"yzz",9000});
    jdbcTemplate.batchUpdate(sql,batchArgs);
    **/

    /**插入一个对象**/
    String sql="insert into Bookimformation values(100033,'zzyyxl','yxlzzy',300,900)";
    jdbcTemplate.update(sql);

    /***
    * 修改一个对象
    String sql="UPDATE Bookimformation SET name =? WHERE id=?";
    jdbcTemplate.update(sql,"zzx",100003);
    * ***/

    /***
    * 从数据库中获取一条记录
    String sql="select * from Bookimformation where id =?";
    RowMapper<Book2> rowMapper=new BeanPropertyRowMapper<>(Book2.class);
    Book2 book2=jdbcTemplate.queryForObject(sql,rowMapper,100099);
    System.out.println(book2);
    * ***/

    /** 查询所有对象
    * String sql="select * from Bookimformation";
    RowMapper<Book2> rowMapper=new BeanPropertyRowMapper<>(Book2.class);
    List<Book2> book2=jdbcTemplate.query(sql, rowMapper);
    Iterator iterator=book2.iterator();
    while (iterator.hasNext())
    System.out.println(iterator.next());**/

    /**
    * 查询行操作
    * String sql="select count(*) from Bookimformation";
    long count=jdbcTemplate.queryForObject(sql,long.class);
    System.out.print(count+" ");
    **/

    System.out.println("success to do it!");
    }
    }
     
  • 相关阅读:
    MFC编程习惯
    光源的频闪与常亮
    相机的选择——卷帘快门(Rolling Shutter)与全局快门(Global Shutter)
    Halcon标定(以40*40标定板为例)
    DALSA线阵相机使用教程(以16K为例)、保存配置文件ccf细节
    打包exe(使用Inno Setup)
    机器视觉项目总结——镜头
    Qt(MinGW版本)utf8终端中文乱码解决方案
    Qt(MinGW版本)安装
    (C#)Windows Shell 外壳编程系列7
  • 原文地址:https://www.cnblogs.com/41ZZY/p/5330354.html
Copyright © 2020-2023  润新知