Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java GUI inserting to a DB

  • 17-10-2017 03:37PM
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi can someone please help me with this. When the user clicks the add button the package details add too the arrayList but not my DB.
     //Add package
            String time = new SimpleDateFormat("DD.MM.YYYY.HH.mm.ss").format(new java.util.Date());
            String name = nameInputTF.getText();
            String address = addressInputTF.getText();
            String city = cityInput.getText();
            String county = (String) countyCombo.getSelectedItem();
            String contact = contactInput.getText();
            String description = descriptionInput.getText();
            double price = Double.parseDouble(priceInput.getText());
            boolean prepaid = true || false;
            id++;
    
            //Adding packages to the list
            packages.add(new Package(id, time, name, address, city, county, contact, description, price, prepaid));
    
            //Method to display packages in the text JList
            displayList();
    
            try {
    
                String sql = "INSERT INTO packtable VALUES (?,?,?,?,?,?,?)";
    
                statement = conn.prepareStatement(sql);
    
                statement.setString(1, name);
                statement.setString(2, address);
                statement.setString(3, contact);
                statement.setDouble(4, price);
                statement.setInt(5, id);
                statement.setString(6, county);
                statement.setString(7, city);
    
                statement.executeUpdate();
    
            } catch (SQLException ex) {
                Logger.getLogger(A2ZTransit.class.getName()).log(Level.SEVERE, null, ex);
            }
           }
    

    And below is my connection class
    public class PackageJDBC {
    
        String user = "root";
        String password = "";
        String dbURL = "jdbc:mysql://localhost:3306/packages";
    
        Connection conn;
        PreparedStatement stmt;
        ResultSet rs;
    
        public PackageJDBC() {
    
        }
    
        public void connection() {
            try {
                conn = DriverManager.getConnection(dbURL, user, password);
            } catch (SQLException ex) {
                Logger.getLogger(PackageJDBC.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            JOptionPane.showMessageDialog(null, "Connected to the database!");
        }
    }
    
    

    My database is made in netbeans with each entry as char except id which is int, contact which is varchar and price which is float. Help would be massively appreciated.


Advertisement