1. Customer 表

在这个例子中,我们使用的是MySQL数据库。
CREATE TABLE `customer` (
`CUST_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) NOT NULL,
`AGE` int(10) unsigned NOT NULL,
PRIMARY KEY (`CUST_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2. Customer模型

添加一个客户模型用来存储用户的数据。
package com.yiibai.customer.model;

import java.sql.Timestamp;

public class Customer
{
int custId;
String name;
int age;
//getter and setter methods }

4. 数据访问对象 (DAO) 模式

Customer Dao 接口.

package com.yiibai.customer.dao;

import com.yiibai.customer.model.Customer;

public interface CustomerDAO
{
public void insert(Customer customer);
public Customer findByCustomerId(int custId);
}
客户的DAO实现,使用 JDBC 发出简单的 insert 和 select SQL语句。
package com.yiibai.customer.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.yiibai.customer.dao.CustomerDAO;
import com.yiibai.customer.model.Customer; public class JdbcCustomerDAO implements CustomerDAO
{
private DataSource dataSource; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} public void insert(Customer customer){ String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
Connection conn = null; try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, customer.getCustId());
ps.setString(2, customer.getName());
ps.setInt(3, customer.getAge());
ps.executeUpdate();
ps.close(); } catch (SQLException e) {
throw new RuntimeException(e); } finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
} public Customer findByCustomerId(int custId){ String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?"; Connection conn = null; try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, custId);
Customer customer = null;
ResultSet rs = ps.executeQuery();
if (rs.next()) {
customer = new Customer(
rs.getInt("CUST_ID"),
rs.getString("NAME"),
rs.getInt("Age")
);
}
rs.close();
ps.close();
return customer;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
}

5. Spring bean配置

创建 customerDAO 和数据源在 Spring bean 配置文件中。

File : Spring-Customer.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean> </beans>

File : Spring-Datasource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yiibaijava" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean> </beans>

File : Spring-Module.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="database/Spring-Datasource.xml" />
<import resource="customer/Spring-Customer.xml" /> </beans>
6.项目结构
本实例完整目录结构。
7.运行它
package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiibai.customer.dao.CustomerDAO;
import com.yiibai.customer.model.Customer; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml"); CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");
Customer customer = new Customer(1, "yiibai",29);
customerDAO.insert(customer); Customer customer1 = customerDAO.findByCustomerId(1);
System.out.println(customer1); }
}

输出结果:

Customer [age=29, custId=1, name=yiibai]
 
下载源代码 – http://pan.baidu.com/s/1SR2GI

Spring+JDBC实例的更多相关文章

  1. 一个spring jdbc实例

    一.使用示例 (1)springJdbcContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  2. Spring JDBC 框架使用JdbcTemplate 类的一个实例

    JDBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQ ...

  3. spring jdbc 查询结果返回对象、对象列表

    首先,需要了解spring jdbc查询时,有三种回调方式来处理查询的结果集.可以参考 使用spring的JdbcTemplate进行查询的三种回调方式的比较,写得还不错. 1.返回对象(queryF ...

  4. Spring JDBC

    转载:博客主页:http://blog.csdn.NET/chszs 一.概述 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1)core即核心包,它包含了JDBC的核心功能.此包内 ...

  5. Spring学习进阶(四) Spring JDBC

    Spring JDBC是Spring所提供的持久层技术.主要目的是降低使用JDBC API的门槛,以一种更直接,更简洁的方式使用JDBC API.在Spring JDBC里用户仅需要做哪些比不可少的事 ...

  6. Spring JDBC常用方法详细示例

    Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...

  7. Spring笔记——Spring+JDBC组合开发

      使用Spring+JDBC集成步骤如下:   1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...

  8. Spring JDBC 示例

    在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常, ...

  9. Spring04-SpringEL&Spring JDBC数据访问

    一. SpringEL入门 Spring动态语言(简称SpEL) 是一个支持运行时查询和操作对象图的强大的动态语言,语法类似于EL表达式,具有诸如显示方法和基本字符串模板函数等特性. 1. 准备工作 ...

随机推荐

  1. 关于Hazard Pointers的话题

    关于Hazard Pointers的话题, 起源于这个文章: 实现无锁的栈与队列(4) http://www.cnblogs.com/catch/p/3176636.html 其实他的系列文章(3)之 ...

  2. springboot + swagger2 生成api文档

    直接贴代码: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...

  3. cgic实现输入文件名,打开文件的功能

    a.c文件 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg. ...

  4. intellij idea 远程tomcat 调试

    由于在服务器上远程调试风险较高,所以万不得已的情况下 不要这样做.可以本地调试好  再上传到服务器上. 1.关闭防火墙 启动Firewalld,及开机自启. # systemctl start fir ...

  5. csu 1769(数学)

    1769: 想打架吗?算我一个!所有人,都过来!(3) Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 262  Solved: 76[Submit][S ...

  6. JAVA 抽象类、接口

    一.抽象类 1.语法定义 在类前面使用 abstract 修饰的类就是抽象类 2.应用场景: (1)在某些情况下,某个父类只是知道其子类应该包含怎样的方法,但无法准确知道这些子类如何实现这些方法. ( ...

  7. jquery on方法(事件委托)

    jquery绑定事件处理函数的方法有好几个,比如:bind(),on(),delegate(),live(). 其中delegate和live都是用on实现的,效果也类似,live好像在1.7版本中已 ...

  8. Could not apply the stored configuration for monitors

    在用户目录下$user.home/.config/monitors.xml,要解决上面的问题,最简单的办法就是删除这个monitors.xml文件,重启一下电脑

  9. SQL 如何查找一个表里,每个班级各个学科的最高分?

    SQL 如何查找一个表里,每个班级各个学科的最高分? 学生表:STUDENT(S#,SNAME,SAGE,SSEX,CLASSNO) 班级表:CLASS(CLASSNO,CLASSNAME) 课程表: ...

  10. cpp笔记(长期更新)

    c++0x:c++11标准成为正式标准之前的草案临时名字 多态:接口的多种不同的实现方式即为多态,即子类类型的指针赋值给父类类型的指针 动态内存(堆)的管理是通过一对运算符来完成的: new:在动态内 ...