Spring 专题
专题目录
您的位置:java > Spring专题 > Spring+JDBC实例
Spring+JDBC实例
作者:--    发布时间:2019-11-20

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.h3.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.h3.customer.dao;

import com.h3.customer.model.customer;

public interface customerdao 
{
	public void insert(customer customer);
	public customer findbycustomerid(int custid);
}
客户的dao实现,使用 jdbc 发出简单的 insert 和 select sql语句。
package com.h3.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.h3.customer.dao.customerdao;
import com.h3.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.h3.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/h3java" />
		<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.h3.common;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.h3.customer.dao.customerdao;
import com.h3.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, "h3",29);
        customerdao.insert(customer);
    	
        customer customer1 = customerdao.findbycustomerid(1);
        system.out.println(customer1);
        
    }
}

输出结果:

customer [age=29, custid=1, name=h3]

下载源代码 – http://pan.baidu.com/s/1sr2gi

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册