JDBCTemplate,SimpleJDBCTemplate简单使用

package org.lei.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.lei.model.User;
import org.lei.util.JDBCUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;


public class JDBCTemplateTest {
	private static JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
	public static void main(String []args){
		//find();
		//find2();
		//findName();
		//String name = getUserName(1);
		//System.out.println(name);
		//update();
		add();
	}
	static void find2(){
		String sql = "select * from userinfo where id = ?";
		Object []args = new Object[]{1};
		User user = (User) jdbcTemplate.queryForObject(sql, args,new RowMapper() {
			
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
					User user = new User();
					user.setId(rs.getInt("id"));
					user.setName(rs.getString("name"));
					return user;
			}
		});
	
	
		System.out.println(user.getName());
	}
	static void findName(){
		System.out.println("***");
		String sql = "select name from userinfo where id = ?";
		Object []args = new Object[]{1};
		String name =  null;
		name =  jdbcTemplate.queryForObject(sql, args,String.class).toString();
		System.out.println("*8"+name+"**");
		System.out.println("&*&");
	}
	
	static String getUserName(int id) {
		String sql = "select name from userinfo where id=?";
		Object []args = new Object[]{1};
		Object name = jdbcTemplate.queryForObject(sql,args, String.class);
		System.out.println(name.toString());
		return (String) name;
	}
	
	static void find(){
		String sql = "select * from userinfo where id = ?";
		Object []args = new Object[]{1};
		User user = (User) jdbcTemplate.queryForObject(sql, args,new BeanPropertyRowMapper(User.class));
		System.out.println(user.getName());
	}
	//修改数据
	static void update(){
		String sql = "update userinfo info set name = ? where id = ?";
		Object []args = new Object[]{"renlei",1};
		int result = -1;
		result = jdbcTemplate.update(sql,args);
		if(result>0){
			System.out.println("修改成功");
		}
	}
	//插入数据
	static void add(){
		String sql = "insert into userinfo values(?,?)";
		Object[]args = new Object[]{2,"zyr"};
		int result = -1;
		result = jdbcTemplate.update(sql,args);
		if(result>0){
			System.out.println("插入成功");
		}
	}
	
	
	
}

SImpleJDBCTemplate

package org.lei.dao.impl;

import org.lei.model.User;
import org.lei.util.JDBCUtil;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class SimpleJdbcTemplateTest {
	static SimpleJdbcTemplate simpleJdbcTemplate  = new SimpleJdbcTemplate(JDBCUtil.getDataSource());
	public static void main(String []args){
		find(1);
	}
	static void find(int id ){
<span style="white-space:pre">		</span>String sql = "select * from userinfo where id = ?";
<span style="white-space:pre">		</span>Object []args = new Object[]{1};
<span style="white-space:pre">		</span>//是可变长的
<span style="white-space:pre">		</span>User user = (User) simpleJdbcTemplate.queryForObject(sql,ParameterizedBeanPropertyRowMapper.newInstance(User.class),id);
<span style="white-space:pre">		</span>System.out.println(user.getName());
<span style="white-space:pre">		</span>}
	
	static void findName(){
		String sql = "select name from userinfo where id = ?";
		Object []args = new Object[]{1};
		String name =  null;
		name =  simpleJdbcTemplate.queryForObject(sql,String.class,args).toString();
		System.out.println(name);
	}
}

public class UserDaoSpringImpl implements UserDao {
	private SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(
			JdbcUtils.getDataSource());

	public void addUser(User user) {
		String sql = "insert into user (name, money, birthday) values (:name, :money, :birthday)";
		SqlParameterSource param = new BeanPropertySqlParameterSource(user);
		KeyHolder keyHolder = new GeneratedKeyHolder();
		this.simpleJdbcTemplate.getNamedParameterJdbcOperations().update(sql,
				param, keyHolder);
		user.setId(keyHolder.getKey().intValue());
	}

	public void delete(User user) {
		String sql = "delete from user where id=?";
		this.simpleJdbcTemplate.update(sql, user.getId());
	}

	public User findUser(String loginName, String password) {
		String sql = "select id, name, money, birthday  from user where name=?";
		return this.simpleJdbcTemplate.queryForObject(sql,
				ParameterizedBeanPropertyRowMapper.newInstance(User.class),
				loginName);
	}

	public User getUser(int userId) {
		String sql = "select id, name, money, birthday  from user where id=?";
		return this.simpleJdbcTemplate.queryForObject(sql,
				ParameterizedBeanPropertyRowMapper.newInstance(User.class),
				userId);
	}

	public void update(User user) {
		String sql = "update user set name=?, birthday=?, money=? where id=? ";
		this.simpleJdbcTemplate.update(sql, user.getName(), user.getBirthday(),
				user.getMoney(), user.getId());

		sql = "update user set name=:name, birthday=:birthday, money=:money where id=:id ";
		this.simpleJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(
				user));
	}

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值