try { double value1 = Double.parseDouble(total1.getText()); double value2 = Double.parseDouble(cash1.getText()); double value3 = value2 - value1; change1.setText(String.format("%16.2f", value3)); insertTransaction(value1, value2, value3); JOptionPane.showMessageDialog(this, "Transaction Saved"); next1.setEnabled(true); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "Invalid input. Please enter numeric values."); } } private void insertTransaction(double total, double cash, double change) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = DriverManager.getConnection("jdbc:mysql://" + "localhost:3306/project_databse","myuser","xxxx"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentDate = dateFormat.format(new Date()); String sql = "INSERT INTO trans_tbl (trans_total, trans_cash, trans_change, trans_date) VALUES (?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setDouble(1, total); preparedStatement.setDouble(2, cash); preparedStatement.setDouble(3, change); preparedStatement.setString(4, currentDate); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (preparedStatement != null) preparedStatement.close(); if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } }