Search code examples
javaswingintegerjtextfield

How to get integer value from JTextField of Swing?


How to get integer value from JTextField of Swing as we get string value via getText() method?

try {
        String sql = "insert into employeeinfo   (username,password,obtainmark) values(?,?,?)";
        pst = conn.prepareStatement(sql);
        pst.setString(1, txt_username.getText());
        pst.setString(2, txt_password.getText());
        pst.setInt(3, txt_obtainmark.getText());
        pst.execute();

        JOptionPane.showMessageDialog(null, "data inserted");
    } catch (Exception e) {

        JOptionPane.showMessageDialog(null, e);
    }

I am not able to insert integer data type value from the JTextField, but able to insert only string or varchar type data.


Solution

  • You can do Integer.parseInt(string) to get Integer value.

    pst.setInt(Interger.parseInt(txt_obtainmark.getText()));