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.