Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why SAVE button is not saving to MySQL database in Java code

What’s wrong with the code, no errors but still it’s not saving to database, where did it go wrong?

Even if the database is created, the code won’t store the values

    JButton btnSave = new JButton("SAVE");
    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            
            // Get Breed and age entered by user
            String breed = textBreed.getText();
            String breed_age = textAge.getText();
            
            // Convert age into integer
            int age = Integer.parseInt(breed_age);
            
            // Connection
            
            try {
                //open connection
                Class.forName("com.mysql.cj.jdbc.Driver");
                
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost/animal_db", "root", "root");
                
                // Insert data into table
                Statement stm = con.createStatement();
                
                String dog_table = "CREATE TABLE IF NOT EXISTS breedtest" + 
                           "(id INT NOT NULL AUTO_INCREMENT," +
                           "breed VARCHAR(30)," +
                           "age INT," +
                           "PRIMARY KEY (id))";
                
                stm.executeUpdate(dog_table);
                
                String sql = "INSERT INTO breedtest VALUES ('"+textBreed.getText()+"', "+textAge.getText()+")"; 
                
                // Execute Statement
                stm.executeUpdate(sql);
                
                // display message of record inserted
                JOptionPane.showMessageDialog(btnSave, "Record added");
                textBreed.setText("");
                textAge.setText("");
                
                //Close connection
                con.close();
                
                
            }catch(Exception E) {
                
            }
        }
    });

textBreed & textAge are text field from the GUI

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

here is a creen shot of the GUI.
enter image description here

>Solution :

You need to modify your Insert query with the following

String sql = "INSERT INTO breedtest(breed, age) VALUES ('"+textBreed.getText()+"', "+textAge.getText()+")"; 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading