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"));
			}
		},"修改");
	}

猜你喜欢

转载自201407105131.iteye.com/blog/2205095