日期:2014-05-16  浏览次数:20456 次

使用spring jdbc template简化jdbc数据库操作实例代码

文章地址: http://blog.csdn.net/5iasp/article/details/12206793

作者: javaboy2012
Email:yanek@163.com
qq:    1046011462

 

使用spring jdbc template简化jdbc数据库操作实例代码

 

包括如下几个类:

 

1. DAO接口

 

package com.test;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

public interface DAO {
	
	public int getCount(String sql);
	
	public String getResultValue(String sql, String column);

	public List getResult(String sql);

	public void update(String sql);
	public void update(String sql,Object[] params);

	public void delete(String sql);
	
	public JdbcTemplate getJt();

}


2. DAO接口实现类 DAOImpl

package com.test;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

public class DAOImpl implements  DAO{


	protected final Log log = LogFactory.getLog(this.getClass());
	
	private JdbcTemplate jt;


	public int getCount(String sql) {
		int count = 0;
		try {
			count = jt.queryForInt(sql);
		} catch (DataAccessException e) {
			log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
		}
		return count;
	}
	
	public String getResultValue(String sql, String column) {
		String value = "";
		try {
			SqlRowSet s = jt.queryForRowSet(sql);
			while (s.next()){
				value = s.getString(column);
			}
		} catch (DataAccessException e) {
			log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
		}
		return value;
	}

	public List getResult(String sql) {
		List list = null;
		try {
			list = jt.queryForList(sql);
		} catch (DataAccessException e) {
			log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
		}
		return list;
	}

	public void update(String sql) {

		try {
			jt.update(sql);
		} catch (DataAccessException e) {
			log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
		}
	}

	public void delete(String sql) {
		try {
			jt.execute(sql);
		} catch (DataAccessException e) {
			log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
		}
	}



	@Override
	public void update(String sql, Object[] params) {
		// TODO Auto-generated method stub
		try {
			jt.update(sql,params);
		} catch (DataAccessException e) {
			log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
		}
	}

	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}
	
	public JdbcTemplate getJt() {
		return jt;
	}

}


3. UserManager 接口

package com.test;

import java.util.List;
import java.util.Map;



public interface UserManager {
	
	
	
	
	public void addUser(String name);
	
	public void updateUser(String name,int id);
	
	public void deleteUser(int id);
	
	public String getUser(int id);
	
	public User getUserByID(int id);

	
	public List getUsers();
	
	public List<User> getUserList();

	
	public void init();
	

}


4. UserManagerImpl:UserManager 接口实现类

 

package com.test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;



public class UserManagerImpl implements UserManager {
	
	
	private DAO dao;

	/**
	 * @param args
	 */
	public static void main(String[] args) {


	}
	
	public void addUser(String name)
	{
		/*
		String sql="insert into t_test(name) values('"+name+"')";
		dao.update(sql);
		*/
		String sql="insert into t_test(name) values(?)";
		Object[] params = new Object[] {name};
		dao.update(sql, params);
		
	}
	
	public List getUsers()
	{
		String sql = "select * FROM t_test";
		List<Map> lis