Ragataj
Ragataj

Reputation: 69

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.

Upvotes: 2

Views: 1123

Answers (1)

Aniket Thakur
Aniket Thakur

Reputation: 69025

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

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

Upvotes: 1

Related Questions