I'm using spring 4.0 (boot), and here is the code:
String sql = "SELECT emp_id,name,role FROM employees";
List<Employee> employees = jdbcTemplate.query(sql,(rs, rowNum)->
new Employee(rs.getInt("emp_id"), rs.getString("name"),
rs.getString("role")));
employees.forEach(employee -> {log.info(employee.toString());
log.info("part a");});
The code looks so simple and straightforward, but the problem is that employees is not returning anything at all in the logs. Doesn't this mean that everything in employees is null?
There should be no problem with the Logger itself because the logging before this code works.
The database gets properly inputted. When I query through mysql client, I can see them there.
What kind of Java mistake am I making (if it is one)?
EDIT: this is the full function which I am performing simple database queries in and from which the above code comes from:
@Override
public void run(String... args) throws Exception {
log.info("Creating tables");
jdbcTemplate.execute("DROP TABLE IF EXISTS employee");
jdbcTemplate.execute("CREATE TABLE employee (emp_id int, name varchar(100), role varchar(100))");
log.info("Inserting Baggins Hopkins");
int rowsAffected = jdbcTemplate.update("INSERT INTO EMPLOYEE(EMP_ID, NAME, ROLE)"
+ " VALUES(1,'Baggins Hopkins','thief')");
log.info("rows affected: "+ Integer.toString(rowsAffected));
int rowsAffected1 = jdbcTemplate.update("INSERT INTO EMPLOYEE(EMP_ID, NAME, ROLE)"
+ " VALUES(2,'Doolous Hopkins','robber')");
log.info("rows affected: "+ Integer.toString(rowsAffected1));
log.info("Querying for employee");
String sql = "SELECT emp_id,name,role FROM employee";
List<Employee> employees = jdbcTemplate.query(sql,(rs, rowNum)->
new Employee(rs.getInt("emp_id"), rs.getString("name"),
rs.getString("role")));
log.info("Part A:");
log.info(String.valueOf(employees.isEmpty()));
// List<Employee> employees = new ArrayList<Employee>();
// final List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
// for(final Map row : rows) {
// Employee employee = new Employee();
// employee.setId((int)row.get("emp_id"));
// employee.setName((String)row.get("name"));
// employee.setRole((String)row.get("role"));
// };
employees.forEach(employee -> {log.info(employee.toString());
log.info("part a");});
}
This is the log that came from when there was an error:
:: Spring Boot :: (v2.1.0.RELEASE)
2018-11-07 22:08:36.599 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : Starting JdbcTest1Application on KitKat with PID 5476 (C:\Users\Nano\Downloads\jdbc-test1\jdbc-test1\target\classes started by Nano in C:\Users\Nano\Downloads\jdbc-test1\jdbc-test1)
2018-11-07 22:08:36.599 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : No active profile set, falling back to default profiles: default
2018-11-07 22:08:37.458 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : Started JdbcTest1Application in 1.156 seconds (JVM running for 2.08)
2018-11-07 22:08:37.474 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : Creating tables
2018-11-07 22:08:37.474 INFO 5476 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-11-07 22:08:37.614 INFO 5476 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-11-07 22:08:38.873 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : Inserting Baggins Hopkins
2018-11-07 22:08:39.042 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : rows affected: 1
2018-11-07 22:08:39.183 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : rows affected: 1
2018-11-07 22:08:39.183 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : Querying for employee
2018-11-07 22:08:39.214 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : Part A:
2018-11-07 22:08:39.214 INFO 5476 --- [ main] c.j.jdbctest1.JdbcTest1Application : true
2018-11-07 22:08:39.230 INFO 5476 --- [ Thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2018-11-07 22:08:39.230 INFO 5476 --- [ Thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Answer by Original Poster:
It was a simple syntax error. As the commentators pointed out, I had been inserting into the wrong table... my syntax in my sql queries were wrong.
Previously, I was inserting into 'employees,' but I should have been inserting into 'employee.'
The queryForList() that returns a List containing Map, however, still returns null. I commented that out in the post above. I don't know what's wrong with that (I haven't tried using List> before). But the issue is solved.