The Jdbctemplate.queryforobject () in spring is used

Source: Internet
Author: User

I wrote a spring program with Javase, and for the whole afternoon, there was a problem with JDBC configuration and access.

1, the old version of the book many use is simplejdbctemplate, looking for a long time, found that the new version of spring does not have this class, replaced by Org.springframework.jdbc.core.JdbcTemplate. function seems to be a simplejdbctemplate superset.

The use of 2,jdbctemplate.queryforobject (), used in the Chinese version of the spring in action Third edition, was Parameterizedrowmapper<>, and found that the new spring was not. On the internet to find a lot of blogs that direct use of jdbctemplate.queryforobject (Sql,user.class) can, my program User is a simple bean, only name and password two members and corresponding setters. Found this way to run the error: Incorrect column count:expected 1, actual 2. I have two variables for user.

After several attempts, the following is found to be normal access

String string= "SELECT * from stutable where name= ' sgzhang3 '";
		User User=jdbctemplate.queryforobject (String, new Rowmapper<user> () {
			@Override public
			user Maprow ( ResultSet arg0, int arg1) throws SQLException {
				user user=new User ();
				User.setname (arg0.getstring ("name"));
				User.setpassword (arg0.getstring ("password"));
				return user
			}
		});

It can be seen that the key to proper access is the need to implement the RowMapper interface.

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.