Spring JDBC Querying Table

We can retrieve data from Relational Database Table, using JdbcTemplate's query method.
We can also specify a where clause to get a matching record as required.

System.out.println("Getting data from COSMICOBJECT table");
		
jdbcTemplate.query("SELECT NAME, GALAXYNAME, WEIGHT, NUMBEROFEARTHYEARS FROM COSMICOBJECT WHERE " +  
	"NAME = ?", new Object[]{"Earth"},
	(resultSet, rowNumber) -> new CosmicObject(resultSet.getString("name"),
	resultSet.getString("galaxyname"),
	resultSet.getInt("weight"),
	resultSet.getInt("numberOfEarthYears")))
.forEach(cosmicObject -> System.out.println("CosmicObject Name = "
	+ cosmicObject.getName() + 
	", Galaxy Name = " + 
	cosmicObject.getGalaxy())
	);
In the above example we queried the COSMICOBJECT table for Earth. We then retrieved the object and printed the name of the object along with the Galaxy Name.

Previous: Inserting data into Table | Next: Running Spring JDBC Application

<
>