Spring配置数据源DAO

总览:

表结构:

目录结构:

1. bean.xml:

<!---->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- 连接MySQL -->
	<!-- 
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost/test</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>root</value>
		</property>
	</bean>
	-->

	<!-- 连接informix -->
	<!--使用连接池-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName">
			<value>com.informix.jdbc.IfxDriver</value>
		</property>
		<property name="url">
			<value>jdbc:informix-sqli://172.18.2.999:1234/abcd:INFORMIXSERVER=efgh</value> <!--注.1-->
		</property>
		<property name="username">
			<value>hello</value>
		</property>
		<property name="password">
			<value>hello</value>
		</property>
	</bean>

	<bean id="connect" class="cn.wangy.dao.UserDAO">
		<property name="ds">
			<ref bean="dataSource" />
		</property>
	</bean>
</beans>

 

2. log4j.properties:

log4j.rootLogger=debug, stdout
log4j.appender.stdout.File=D:\\.log
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout=org.apache.log4j.RollingFileAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n

3.UserBean.java

package cn.wangy.beans;

public class UserBean {
	private String name;
	private String pwd;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}

 

4.UserInter.java:

package cn.wangy.interfaces;

import java.util.List;

import cn.wangy.beans.UserBean;

public interface UserInter {
	List select(String name);
	void insertUser(UserBean user);
}

 

5. UserDAO.java

package cn.wangy.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import cn.wangy.beans.UserBean;
import cn.wangy.interfaces.UserInter;

public class UserDAO implements UserInter {
	private DataSource ds;

	public void setDs(DataSource ds) {
		this.ds = ds;
	}

	public DataSource getDs() {
		return ds;
	}

	public void insertUser(UserBean user) {}

	public List select(String name) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		List users = new ArrayList();
		String sql = "select * from users where name='" + name + "'";
		try {
			conn = ds.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				UserBean uBean = new UserBean();
				uBean.setName(rs.getString("name"));
				uBean.setPwd(rs.getString("pwd"));
				users.add(uBean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return users;
	}
}

 

6. UserTest.java

package cn.wangy.test;

import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import cn.wangy.beans.UserBean;
import cn.wangy.interfaces.UserInter;

public class UserTest {

	public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext("bean.xml");
		UserInter uInter = (UserInter) ac.getBean("connect");
		List list = uInter.select("wangyue");
		Iterator it = list.iterator();
		while(it.hasNext()){
			UserBean uBean = (UserBean)it.next();
			System.out.println("NAME:" + uBean.getName() + "的密码:" + uBean.getPwd());
		}
	}
}

 

#注.1:

在eclipse中 Ctrl+Shift+f为格式化代码 但是在这里如果变成

 

<value>
  jdbc:informix-sqli://172.18.2.999:1234/abcd:INFORMIXSERVER=efgh
</value>

 

会报错:

Caused by: java.sql.SQLException: No suitable driver
 at java.sql.DriverManager.getDriver(DriverManager.java:243)
 at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
 ... 3 more

 

你可能感兴趣的:(java,DAO,spring,数据结构,log4j)