JDBC uses Spring for addition, deletion, modification, and query operations-greatly simplifying the program

Source: Internet
Author: User

Package cn.com. spring; import org. springframework. jdbc. core. namedparam. beanPropertySqlParameterSource; import org. springframework. jdbc. core. namedparam. sqlParameterSource; import org. springframework. jdbc. core. simple. parameterizedBeanPropertyRowMapper; import org. springframework. jdbc. core. simple. parameterizedRowMapper; import org. springframework. jdbc. core. simple. simpleJdbcTemplate; import org. springframewo Rk. jdbc. support. generatedKeyHolder; import org. springframework. jdbc. support. keyHolder; import cn.com. JDBC. jdbcUtils; import cn.com. dao. userDao; import cn.com. jadbc. domain. user; public class UserDaoSpringImpl implements UserDao {private SimpleJdbcTemplate simplejdbcTemplate = new SimpleJdbcTemplate (JdbcUtils. getDataSource (); @ Overridepublic void addUser (User user) {// TODO Auto-generated method stubString 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 () ;}@ Overridepublic 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) ;}@ Overridepublic User getUser (int userId) {String SQL = "select id, name, money, birthday from user where id =? "; Return this. simplejdbcTemplate. queryForObject (SQL, ParameterizedBeanPropertyRowMapper. newInstance (User. class), loginName) ;}@ Overridepublic void update (User user) {String SQL = "update user set name = ?, Birthday = ?, Money =? Where id =? "; This. simplejdbcTemplate. update (SQL, user. getName (), user. getId (), user. getBirthday (), user. getMoney (); // Method 2: String SQL = "update user set name =: name, birthday =: birthday, money =: money where id =: id"; this. simplejdbcTemplate. update (SQL, new BeanPropertySqlParameterSource (user) ;}@ Overridepublic void delete (User user) {// TODO Auto-generated method stubString SQL = "delete from user where id =? "; This. simplejdbcTemplate. update (SQL, user. getId () ;}} package cn.com. dao; import cn.com. jadbc. domain. user; public interface UserDao {public void addUser (User user User); public User findUser (String loginName, String password); public User getUser (int userId ); public void update (User user); public void delete (User user );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.