JdbcTemplate以对象的方式操作数据库
来源:互联网 发布:网络骗局大全微信 编辑:程序博客网 时间:2024/06/01 07:23
1.创建接口和实现类
//PersonDao接口package com.entity;public interface PersonDao {public Person find(int id);}//PersonDao实现类PersonDaoImplpackage com.entity;import java.sql.*;import javax.sql.DataSource;public class PersonDaoImpl implements PersonDao {private DataSource dataSource;/** * 查找方法 */public Person find(int id) {Connection con=null;PreparedStatement ps=null;ResultSet rs=null;try{con=dataSource.getConnection();ps=con.prepareStatement("select * from person where id=?");ps.setInt(1, id);rs=ps.executeQuery();Person p=new Person();if(rs.next()){p.setId(rs.getInt("id"));p.setName(rs.getString("name"));p.setPassword(rs.getString("password"));}return p;}catch(SQLException e){e.printStackTrace();}finally{try {if(rs!=null){rs.close();} if(ps!=null){ps.close();}if(con!=null){con.close();}}catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return null;}/** * Spring配置文件通过查找setDataSource方法,将数据源注入当前Bean中 * @param dataSource */public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}public DataSource getDataSource() {return dataSource;}}//编写测试类package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.entity.Hello;import com.entity.IHello;import com.entity.IHello2;import com.entity.IHelloHandler;import com.entity.Person;import com.entity.PersonDao;public class test1 {/** * @param args */public static void main(String[] args) {try {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");PersonDao personDao=(PersonDao) context.getBean("personDao");Person p=personDao.find(1);System.out.println("姓名:"+p.getName()+",密码:"+p.getPassword());} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
2.修改配置文件
<!-- 配置DataSource --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property><property name="url"><value>jdbc:oracle:thin:@192.168.1.32:1521:orcl</value></property><property name="username"><value>usera</value></property><property name="password"><value>usera</value></property></bean><bean id="personDao" class="com.entity.PersonDaoImpl"><property name="dataSource" ref="dataSource"></property></bean>
3.在数据库建立相应的表
4.运行结果
0 0
- JdbcTemplate以对象的方式操作数据库
- 【Spring学习笔记】五、使用JDBCTemplate以对象方式操作数据库
- Spring中配置JdbcTemplate的三种方式要使用Jdbctemplate 对象来完成jdbc 操作。
- jdbcTemplate操作数据库的DDL语句
- 使用Spring 内置的JdbcTemplate操作数据库
- jdbcTemplate 对数据库的一些操作
- Spring基于jdbcTemplate对数据库的操作
- spring jdbctemplate 实现数据库的查询操作
- 个人试做了C#关于以面向对象的方式操作数据库的插件 希望多给点建议................
- mybatis集成spring操作数据库(以注解的方式)
- spring4 -- JdbcTemplate数据库操作
- jdbcTemplate操作数据库
- SpringBoot连接数据库JDBCTemplate方式
- Spring 使用JdbcTemPlate的方式实现与数据库的连接
- Spring使用JdbcTemplate操作数据库
- Spring使用JdbcTemplate操作数据库
- Spring JdbcTemplate实现数据库操作
- 十八、使用JdbcTemplate操作数据库
- 吗,好,的,国府田
- hadoop学习(六)WordCount示例深度学习MapReduce过程(1)
- LeetCode之Rotate List
- prim算法
- 子网掩码相关的概念定义
- JdbcTemplate以对象的方式操作数据库
- 日期工具类(DateUtil)
- 黑马程序员--(Map与集合框架工具类的知识总结)
- Django foreignKey reverse lookup
- fork与vfork的区别
- 从floyd算法学习动态规划
- MySQL数据库索引的4大类型以及相关的索引创建
- ORA-14039: partitioning columns must form a subset of key columns of a UNIQUE index SYS
- 在WINDOWS系统创建了一个新账户后,原来的Administrator账户不见了,怎样恢复?