Home » Java » java – Data truncation: Truncated incorrect DOUBLE value: on update-Exceptionshub

java – Data truncation: Truncated incorrect DOUBLE value: on update-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

When pushing and update to the localhost keep on receiving this error:

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation:
Truncated incorrect DOUBLE value: ‘Female’

Been trying to solve it, but was not able. Back in php admin gender is defined as varchar it does not look like a double value.

// mySQL JDBC GUI


import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
public class Retrieve

{

//Variables
public static ResultSet rs;
public static String name="", address ="" ,dob="",pps="",salary="",gender="";
public static JButton b1, b2, b3, b4,b5,b6;
public static Connection con = null;

public static void main(String[] args){
    JFrame f=new JFrame();
    JLabel labell = new JLabel("Name: ");
    JLabel labe12 = new JLabel("Address: ");
    JLabel labe13 = new JLabel("Dob: ");
    JLabel labe14 = new JLabel("Pps: ");
    JLabel labe15 = new JLabel("Salary: ");
    JLabel labe16 = new JLabel("Gender: ");
    JTextField textl = new JTextField(20);
    JTextField text2=new JTextField(20);
    JTextField text3=new JTextField(20);
    JTextField text4=new JTextField(20);
    JTextField text5=new JTextField(20);
    JTextField text6=new JTextField(20);
    b1 = new JButton("NEXT");
    b2 = new JButton("PREV");
    b3 = new JButton("ADD");
    b4 = new JButton("CLEAR");
    b5 = new JButton("UPDATE");
    b6 = new JButton("DELETE");
    Connection con = null;

    //Connection and display
    try{
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
        Statement st=con.createStatement();
        rs=st.executeQuery("select * from db");
        if(rs.next()){
            name=rs.getString("name");
            address=rs.getString("address");
            dob=rs.getString("dob");
            pps=rs.getString("pps");
            salary=rs.getString("salary");
            gender=rs.getString("gender");
            textl.setText(name);
            text2.setText(address);
            text3.setText(dob);
            text4.setText(pps);
            text5.setText(salary);
            text6.setText(gender);
        }
    }catch(Exception e){}
    JPanel p=new JPanel();
    p.add(labell);
    p.add(textl);

    p.add(labe12);
    p.add(text2);

    p.add(labe13);
    p.add(text3);

    p.add(labe14);
    p.add(text4);

    p.add(labe15);
    p.add(text5);

    p.add(labe16);
    p.add(text6);

    p.setLayout(new GridLayout(5,3));


    //Next button function
    p.add(b1);
    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if(rs.next()){
                    name=rs.getString("name");
                    address=rs.getString("address");
                    dob=rs.getString("dob");
                    pps=rs.getString("pps");
                    salary=rs.getString("salary");
                    gender=rs.getString("gender");
                    textl.setText(name);
                    text2.setText(address);
                    text3.setText(dob);
                    text4.setText(pps);
                    text5.setText(salary);
                    text6.setText(gender);
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    });


    //Previous button function
    p.add(b2);
    b2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if(rs.previous()){
                    name=rs.getString("name");
                    address=rs.getString("address");
                    dob=rs.getString("dob");
                    pps=rs.getString("pps");
                    salary=rs.getString("salary");
                    gender=rs.getString("gender");
                    textl.setText(name);
                    text2.setText(address);
                    text3.setText(dob);
                    text4.setText(pps);
                    text5.setText(salary);
                    text6.setText(gender);
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    });


    //Clear button function
    p.add(b4);
    b4.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            textl.setText("");
            text2.setText("");
            text3.setText("");
            text4.setText("");
            text5.setText("");
            text6.setText("");
        }
    });


    //Add button function
    p.add(b3);
    b3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Connection finalCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
                try (Statement s = finalCon.createStatement()) {
                    s.executeUpdate(
                            "INSERT INTO db (name, address, dob, pps, salary, gender) VALUES " +
                            "(" +
                                    "'" + textl.getText() + "'," +
                                    "'" + text2.getText() + "'," +
                                    "'" + text3.getText() + "'," +
                                    "'" + text4.getText() + "'," +
                                    "'" + text5.getText() + "'," +
                                    "'" + text6.getText() + "');");
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    });


    //Update button function
    p.add(b5);
    b5.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                Connection finalCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
                PreparedStatement st = finalCon.prepareStatement("UPDATE db SET  name = ?,  address = ?, dob = ?, salary = ?, gender= ? WHERE  pps = ?  ");
                st.setString(1, textl.getText());
                st.setString(2, text2.getText());
                st.setString(3, text3.getText());
                st.setString(4, text4.getText());
                st.setString(5, text5.getText());
                st.setString(6, text6.getText());
                st.executeUpdate();
                rs=st.executeQuery("select * from db");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    });

    //Delete button function
    p.add(b6);
    b6.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Connection finalCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
                try (Statement s = finalCon.createStatement()) {
                    s.executeUpdate(
                            "DELETE FROM db WHERE pps="+pps);
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    });


    f.add(p);
    f.setVisible(true);
    f.pack();
}

}

How to&Answers: