jdbcTemplcate 操作数据库的几种方式

一.增加:

@Test
	public void handle5() throws SQLException{
		final String sql="insert into mw_person(name,age)values(?,?)";//常量的效率要远远高于变量
		jdbcTemplate.update(new PreparedStatementCreator() {
			
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement statement=con.prepareStatement(sql);
				statement.setString(1, "zhansaj");
				statement.setInt(2, 23);
				return statement;
			}
		});
	}

 

@Test
	public void handle6() throws SQLException{
		final String sql="insert into mw_person(name,age)values(?,?)";//常量的效率要远远高于变量
		jdbcTemplate.update(sql,"hsdj",34);
			
	}
 
@Test
	public void handle7() throws SQLException{
		final String sql="insert into mw_person(name,age)values(?,?)";//常量的效率要远远高于变量
		jdbcTemplate.update(sql,new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				//preparedStatementSetter 就是专门给sql语句(insert,update赋值)
				ps.setString(1, "默翁");
				ps.setInt(2, 72);
			}
		});	
	}
 
@Test
	public void handle8() throws SQLException{
		final String sql="insert into mw_person(name,age)values(?,?)";//常量的效率要远远高于变量
		jdbcTemplate.update(sql,new Object[]{"mark",23},new int[]{Types.VARCHAR,Types.INTEGER});
	}
 
	@Test
	public void handle9() throws SQLException{
		//在新增的时候有时需要返回主键
		final String sql="insert into mw_person(name,age)values(?,?)";//常量的效率要远远高于变量
		KeyHolder keyHolder=new GeneratedKeyHolder();
		jdbcTemplate.update(new PreparedStatementCreator() {
			
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement statement=con.prepareStatement(sql);
				statement.setString(1,"hjhsdj");
				statement.setInt(2, 14);
				return statement;
			}
		},keyHolder);
		int key=keyHolder.getKey().intValue();
		System.out.println("我添加的数据返回的主键是:"+key);
	}
 二.修改;

 

 

@Test
	public void handle10() throws SQLException{
		final String sql="update mw_person set name=?,age=? where name=?";//常量的效率要远远高于变量
		jdbcTemplate.update(sql,"xiaoer",10,"默翁");
	}
 三.删除

 

 

	@Test
	public void handle11() throws SQLException{
		final String sql="delete from mw_person where name=?";//常量的效率要远远高于变量
		jdbcTemplate.update(sql,"xiaoer");
	}
 四.查询

 

@Test
	public void handle12() throws SQLException{
		final String sql="select * from mw_person where name=?";//常量的效率要远远高于变量
		jdbcTemplate.query(new PreparedStatementCreator() {
			
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement statement=con.prepareStatement(sql);
				statement.setString(1, "修改");
				return statement;
			}
		},new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				System.out.println(rs.getString("name")+"========"+rs.getInt("age"));
			}
		});
	}

 

@Test
	public void handle13() throws SQLException{
		final String sql="select * from mw_person";//常量的效率要远远高于变量
		jdbcTemplate.query(sql,new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				System.out.println(rs.getString("name")+"========"+rs.getInt("age"));
			}
		});
	}

 

@Test
	public void handle14() throws SQLException{
		final String sql="select * from mw_person where name=?";//常量的效率要远远高于变量
		jdbcTemplate.query(sql,new Object[]{"修改"},new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				System.out.println(rs.getString("name")+"========"+rs.getInt("age"));
			}
		});
	}

 

@Test
	public void handle15() throws SQLException{
		final String sql="select * from mw_person where name=?";//常量的效率要远远高于变量
		jdbcTemplate.query(sql,new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, "修改");
			}
		},new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				System.out.println(rs.getString("name")+"========"+rs.getInt("age"));
			}
		});
	}

 

@Test
	public void handle16() throws SQLException{
		final String sql="select * from mw_person where name=?";//常量的效率要远远高于变量
		jdbcTemplate.query(sql,new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				System.out.println(rs.getString("name")+"========"+rs.getInt("age"));
			}
		},"修改");
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值