简体   繁体   中英

Spring MVC jdbc template

I am using spring MVC and JDBC template.

String SQL = "select * from storeTable"; 

Using the above SQL statement, and print all results on a web page, which works well. However, I have to use the below SQL statement, which should also be seen on web page.

String sql = "select * from storeTable where STORE_NO = ? and Register_NO = ?";

How do I solve this?

Querying for a String:

String lastName = this.jdbcTemplate.queryForObject(
        "select last_name from t_actor where id = ?",
        new Object[]{1212L}, String.class);

Querying and populating a single domain object:

Actor actor = this.jdbcTemplate.queryForObject(
        "select first_name, last_name from t_actor where id = ?",
        new Object[]{1212L},
        new RowMapper<Actor>() {
            public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
                Actor actor = new Actor();
                actor.setFirstName(rs.getString("first_name"));
                actor.setLastName(rs.getString("last_name"));
                return actor;
            }
        });

Querying and populating a number of domain objects:

List<Actor> actors = this.jdbcTemplate.query(
        "select first_name, last_name from t_actor",
        new RowMapper<Actor>() {
            public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
                Actor actor = new Actor();
                actor.setFirstName(rs.getString("first_name"));
                actor.setLastName(rs.getString("last_name"));
                return actor;
            }
        });

For you it will be something like this.

 Object obj = this.jdbcTemplate.queryForObject(
            "select * from storeTable where STORE_NO = ? and Register_NO = ?",
            new Object[]{1212L,46575L}, Returning.class);

queryForObject or query depends what your sql will return. Returning class can be a simple class or a row mapper object like above examples and obj will be the class/List<> that mapper maps the result to. Modify it accordingly

Check out the doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM