Reputation: 31
I want to display the new id from database in a jTextField but it dosen't.. this is my code
public void actionPerformed(ActionEvent e) {
try {
DB con=new DB();
con.connecte();
String req = "SELECT Max(num) FROM condidat";
ResultSet rs = stmt.executeQuery(req);
int num = rs.getInt("num");
int nvNum=num+1;
txt_num.setText(valueOf(nvNum));
}
catch (ClassNotFoundException ex) {
Logger.getLogger(Ajouter.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException ex) {
Logger.getLogger(Ajouter.class.getName()).log(Level.SEVERE, null, ex);
}
}
//To change body of generated methods, choose Tools | Templates.
}
Upvotes: 0
Views: 2261
Reputation: 1
change your integer value to a string because jTextField display only text this way: txt_num.setText(Integer.toString(nvNum));
Upvotes: 0
Reputation: 19821
You don't call ResultSet.next()
and the column in the ResultSet
will not be called num
but Max(num)
, if you want a better name, change your query/code this way using AS
in your query:
String req = "SELECT Max(num) AS maxnum FROM condidat";
ResultSet rs = stmt.executeQuery(req);
if (rs.next()) {
int num = rs.getInt("maxnum");
...
}
Remember that the ResultSet starts pointing before the first record, so next() must always be called at least one time (and this can also be useful to verify that there is actual data, it will return false
if there are no more records). After that if, the ResultSet
is ready to be used and it points to the first record.
Edit:
Since it looks like the actionPerformed
is not even called, did you register this class as a listener to some JComponent
, for example a button?
JButton b = new Button("Click me");
b.addActionListener(this);
If you click this button, and all this code is in the same class, the actionPerformed
will be called.
Upvotes: 2