Skip to content
Advertisement

JDBC, Prepared Statement getInt() returns 0

In my DB every question has a valid questionID (primary Key) and categoryID (foreign Key to category table). The problem is: in the Result Set for every question both IDs are 0 instead of what’s written in the DB. All other arguments are filled out correctly.

private ArrayList<Question> questions = new ArrayList<Question>();
private Connection connie;
private PreparedStatement psShowQuestions;


psShowQuestions= connie.prepareStatement("SELECT * FROM question");        
ResultSet rs = psShowQuestions.executeQuery();

while (rs.next()) {
     questions.add(new Question(rs.getInt("questionID"), rs.getInt("categoryID"), rs.getString("question"), rs.getString("rightAns"), rs.getString("wrong1"), rs.getString("wrong2"), rs.getString("wrong3"), rs.getString("hint")));
}
Collections.shuffle(questions);

Edit 1

Here is the original code (in the post I changed the variables from German to English):

The creation of my SQL table:

CREATE TABLE `frage` (
  `frageID` int(11) NOT NULL,
  `kategorieID` int(11) DEFAULT NULL,
  `frage` varchar(200) NOT NULL,
  `richtig` varchar(200) NOT NULL,
  `falsch1` varchar(200) NOT NULL,
  `falsch2` varchar(200) NOT NULL,
  `falsch3` varchar(200) NOT NULL,
  `hinweis` varchar(200) NOT NULL,
  `anzFalsch` int(11) DEFAULT NULL,
  `anzRichtig` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
private ArrayList<Frage> fragen = new ArrayList<Frage>();
private Connection connie;
private PreparedStatement psGetFragen;

ResultSet rs = psGetFragen.executeQuery();

while (rs.next()) {
     fragen.add(new Frage(rs.getInt("frageID"), rs.getInt("kategorieID"), rs.getString("frage"), rs.getString("richtig"), rs.getString("falsch1"), rs.getString("falsch2"), rs.getString("falsch3"), rs.getString("hinweis")));
}
Collections.shuffle(fragen);

Advertisement

Answer

Most likely, there is a problem with the constructor. You might have not set questionID and categoryID in the constructor and therefore you are getting the default value of int as 0.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement