Hello! There is the following generalized method:
public static <T> List<T> getAllRecords(String table, String columnLabel) throws SQLException { List<T> list = new ArrayList<T>(); rs = stmt.executeQuery("SELECT * FROM " + table); while(rs.next()) list.add((T)rs.getString(columnLabel)); return list; } Here on this line
list.add((T)rs.getString(columnLabel)); compiler swears
Signals places where an unchecked warning is for example:
void f(HashMap map) { map.put("key", "value"); }Hint: Pass -Xlint: unchecked to javac to get more details.
That is, it (the compiler) means that there is an unchecked conversion. Can you tell what exactly is happening in the mentioned line and how will it be more correct to write the code? As I understand it, a conversion to a type that does not exist, therefore he swears ...
Stringcan be cast to typeTIfTwere a normal type, one could reassure the compiler by adding a check usinginstanceof. In your case, you just need to remove the declarationTand returnList<String>, becauseresultSet.getStringreturns strings and so. Or do you want to write a method that returns a column from a table of the desired type? - zRrrTwhen compiling, and the virtual machine does not have the ability to check it during execution. Therefore, inside a parameterized class, you cannot create objects of typeT, check whether the type of an object isTand perform similar operations. It is possible to cast objects to typeT(compilation errors will not happen) - but it is pointless. In fact, the compiler cannot verify the compatibility of typeTand the type of object passed. The compiler warns you about this - fedotsoldier