Home > Back-end >  Why SAVE button is not saving to MySQL database in Java code
Why SAVE button is not saving to MySQL database in Java code

Time:06-08

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

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

CodePudding user response:

I have amended the following lines, and it works fine

                    // 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"   "(breed, age)"   "VALUES(?, ?)"; 
                
                PreparedStatement brd = con.prepareStatement(sql);
                
                brd.setString(1, textBreed.getText());
                brd.setString(2, textAge.getText());
                brd.execute();

data got captured in database with the confirmation message. Displayed message

Database table

This line of code is not executing

String sql = "INSERT INTO breedtest" "(breed, age)" "VALUES (" textBreed.getText() ", " textAge.getText() ")";

but this one is.

String sql = "INSERT INTO breedtest"   "(breed, age)"   "VALUES(?, ?)"; 

I understand that in my first code I omitted to name the columns, thanks for flagging this.

Could someone explain me which line of code is associating the "VALUES(?, ?)" to the textfield.

CodePudding user response:

You need to modify your Insert query with the following

String sql = "INSERT INTO breedtest(breed, age) VALUES ('" textBreed.getText() "', " textAge.getText() ")"; 
  • Related