Cannot find symbol from ResultSet JDBC [duplicate]

2019-09-23 01:18发布

问题:

This question already has an answer here:

  • What does a “Cannot find symbol” compilation error mean? 12 answers

I'm getting a cannot find symbol error and it points to the period right in rs.getChars(1) This code is all within a try{}.

There are three columns in the view table, title, item_id, and name. I'm just trying to query it but get a cannot find symbol on the line inside the start of the while loop.

PreparedStatement pstmt= 
conn.prepareStatement("SELECT * FROM Film_Sound_Track");
ResultSet rs = pstmt.getResultSet();

while(rs.next()){
    char item_id = rs.getChars(1);
}

rs.close();
pstmt.close();

回答1:

java.sql.ResultSet does not have a method getChars. You could use the getString method to retrieve a String and then extract the char from it:

char item_id = rs.getString(1).charAt(0);