package com.template.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.template.dao.UserDao;
import com.template.model.User;
public class UserDaoImpl implements UserDao {

	private JdbcTemplate jdbcTemplate;  

	       public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
	    this.jdbcTemplate = jdbcTemplate;  
	}  
	public User getUserById(int id) {  

	    String sql = "select id, username from T_USER where id=?";  
	    Object[] args = new Object[]{id};  
	    final User user = new User();  
	    jdbcTemplate.query(sql, args, new RowCallbackHandler(){  
	        public void processRow(ResultSet rs) throws SQLException {  
	        	user.setId(rs.getInt("id"));  
	        	user.setUsername(rs.getString("username"));  
	        }  
	    });  
	    return user;  
	}  

	public void saveUser(User user) {
		 String sql = "insert into T_USER(username, password) values(?,?)";  
		    Object[] args = new Object[]{user.getId(),user.getUsername()};  
		    int[] argTypes = new int[]{Types.VARCHAR, Types.VARCHAR};  
		    jdbcTemplate.update(sql, args, argTypes);  
	}

	Boolean b =false;
	public boolean exitUser(String username, String password) {
		String sql = "select username from T_USER where username=? and password=?";  
	    Object[] args = new Object[]{username,password};  
	    RowCallbackHandler argTypes = new RowCallbackHandler(){  
	        public void processRow(ResultSet rs) throws SQLException {  
	        	if(rs.getString("username").equals("")){
	        		b=false;
	        	}else{
	        		b=true;
	        	}

	        }  
	    };  
	    jdbcTemplate.query(sql, args, argTypes);  
	    System.out.println(b);
		return b;
	}

}


------------------------------------------------------------------------applicationContext
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>


---------------------------------------------------------------junitTest
package com.template.daoImpl;

import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.template.dao.UserDao;


public class UserDaoImplTest {
	private BeanFactory beanFactory =null;
	private UserDao dao =null;

	@Before
	public void setUp() throws Exception {
		beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
		dao = (UserDao) beanFactory.getBean("userDao");
	}

	@After
	public void tearDown() throws Exception {
		dao =null;
		beanFactory =null;
	}

	@Test
	public void testGetUserById() {
		dao.getUserById(1);
	}

	@Test
	public void testSaveUser() {

	}

	@Test
	public void testExitUser() {
		dao.exitUser("admin","123");
	}

}
------------------------------------------------------------------------------

antlr-2.7.6.jar
asm-all-3.2.jar
aspectjrt.jar
aspectjweaver.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-collections-3.2.jar
commons-dbutils-1.3-javadoc.jar
commons-dbutils-1.3-sources.jar
commons-dbutils-1.3.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
db2jcc_license_cu.jar
db2jcc.jar
dom4j-1.6.1.jar
ezmorph-1.0.3.jar
freemarker-2.3.16.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
httpclient-4[1].0.1.jar
httpcore-4[1].0.1.jar
javassist-3.12.0.GA.jar
jsf-api.jar
jsf-impl.jar
json-lib-2.1-jdk15.jar
jstl-1.2.jar
jta-1.1.jar
log4j-1.2.16.jar
mysql-connector-java-5.0.4-bin.jar
ognl-3.0.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.3.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.spring-library-3.0.5.RELEASE.libd
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
oro-2.0.8.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
struts2-core-2.2.1.jar
struts2-spring-plugin-2.2.1.jar
xwork-core-2.2.1.jar