美文网首页
Spring+JDBC实例

Spring+JDBC实例

作者: 呵呵飘过 | 来源:发表于2017-05-09 14:58 被阅读0次
  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;

  1. Customer模型

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

import java.sql.Timestamp;

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

}

  1. 数据访问对象 (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) {}
        }
    }
}

}

  1. 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>

相关文章

  • Spring+JDBC实例

    Customer 表 在这个例子中,我们使用的是MySQL数据库。CREATE TABLE customer (C...

  • 37 Spring+JDBC实例

    1. Customer 表 2. Customer模型 添加一个客户模型用来存储用户的数据。 3. 数据访问对象 ...

  • 使用Spring+JDBC访问数据库

    JDBC是允许用户在不同数据库之间做选择的一个抽象层。JDBC允许开发者用JAVA写数据库应用程序,而不需要关心底...

  • SQL C语言基本操作

    相关API 打开 实例 关闭 实例 获取错误消息 操作表 实例创建 实例插入 实例修改 实例删除 实例回调查询 非回调

  • Python-数据类型及其操作方法

    数字类型 代码实例: 字符串类型 代码实例: 列表 代码实例: 元组 代码实例 字典: 代码实例 集合 代码实例:

  • HTML基础-03

    HTML 标题 实例 HTML 段落 实例 HTML 链接 实例 HTML 图像 实例

  • Python 类属性、实例属性、类方法、实例方法

    1、实例属性 实例属性,就是赋给由类创建的实例的属性,实例属性属于它所属的实例,不同实例之间的实例属性可以不同。 ...

  • STL算法之常用拷贝和替换

    copy API 实例 replace API 实例 replace_if API 实例 swap API 实例

  • Vue 基础

    Vue 实例 1. Vue实例 2. 实例属性 3. 实例方法/数据 4. 实例方法/事件 5. 实例方法/生命周...

  • 类中的方法

    1.实例方法的调用方式 实例对象.实例方法() 类对象.实例方法(实例对象) 例如: class Student ...

网友评论

      本文标题:Spring+JDBC实例

      本文链接:https://www.haomeiwen.com/subject/ykzbtxtx.html