Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java GUI inserting to a DB

Options
  • 17-10-2017 3: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