When you try to run a method, an error occurs:

java.sql.SQLException: Column 'Rock' not found. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1093) at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488) at DAO.MusicTypeDAO.getAll(MusicTypeDAO.java:108) at DAO.Main.main(Main.java:18) 

Method code:

 public List<MusicType> getAll() { String requestMysql = "SELECT * FROM music_type;"; List<MusicType> musicTypes = new ArrayList(); PreparedStatement ps; try { ConnectionUtils.getConnection().setAutoCommit(false); ps = ConnectionUtils.getConnection().prepareStatement(requestMysql); ResultSet rs = ps.executeQuery(); while (rs.next()) { MusicType musicType = new MusicType(rs.getInt("id"), rs.getString(rs.getString("type_name"))); musicTypes.add(musicType); } ConnectionUtils.getConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } return musicTypes; } 

Db:

 CREATE database if not exists `users_and_music` ; USE users_and_music; CREATE TABLE address ( id INT PRIMARY KEY AUTO_INCREMENT, country VARCHAR(256), street VARCHAR(256), zip INT ); CREATE TABLE role ( id INT PRIMARY KEY AUTO_INCREMENT, role_name VARCHAR(256) ); CREATE TABLE music_type ( id INT PRIMARY KEY AUTO_INCREMENT, type_name VARCHAR(256) ); CREATE TABLE user ( id INT NOT NULL AUTO_INCREMENT, role_id INT NOT NULL, firstName VARCHAR(15) DEFAULT NULL, lastName VARCHAR(15) DEFAULT NULL, login VARCHAR(15) DEFAULT NULL, userpassword VARCHAR(20) DEFAULT NULL, age SMALLINT(3) DEFAULT NULL, PRIMARY KEY (`id`), FOREIGN KEY (role_id) REFERENCES role (id) ); CREATE TABLE user_music_type ( user_id INT NOT NULL, music_type_id INT NOT NULL, PRIMARY KEY(user_id,music_type_id ), FOREIGN KEY (user_id) REFERENCES user (id), FOREIGN KEY (music_type_id) REFERENCES music_type (id) ); 

    1 answer 1

    Judging by the stack-tracing, an exception is generated in this line:

     MusicType musicType = new MusicType(rs.getInt("id"), rs.getString(rs.getString("type_name"))); 

    Namely because of this:

     rs.getString(rs.getString("type_name")) 

    Here you get the value of the type_name field, and then you try to get the value of the field with the name returned by rs.getString("type_name") , which obviously does not exist.

    In general, try replacing this line with this one:

     MusicType musicType = new MusicType(rs.getInt("id"), rs.getString("type_name"));