I am working on a personal project. I am creating a punch clock program so the part that I need help with, is where the user inputs the uid in the jTextField. So, I need to retrieve the uid(#######) and then search for that uid in the database with a sql statement. So this is what I have so far. the problem is that there is a red line under jTextField1.getText and when and when I run it, says =
"setInt(int,int) in java.sql.PreparedStatement cannot be applied to (int,java.lang.String) pstmt.setInt(1, jTextField1.getText());"
Any help is appreciated. Thank you.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM Students WHERE STUDENTID = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, jTextField1.getText()); // Retrieve uid from jTextField
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String first = rs.getString(2);
String last = rs.getString(3);
String hours = rs.getString(6);
fName.setText(first);
lName.setText(last);
tHours.setText(hours);
}
}
catch(SQLException err){
JOptionPane.showMessageDialog(Student.this, err.getMessage());
}
}
As you can see from the documentation, the
setInt()method takes in both parameters asint.So,You need to change this:
to:
because the
getText()method returns a String.