Question: hi can someone help i need to add generic class and a sorting and search feature and Add threads and concurrency on my existing code

hi can someone help i need to add generic class and a sorting and search feature and Add threads and concurrency on my existing code this is the code public class AddCarForm extends JFrame {
private JTextField makeField;
private JTextField modelField;
private JTextField yearField;
private JTextField priceField;
public AddCarForm(){
setTitle("Add Car");
setSize(400,200);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,2));
JLabel makeLabel = new JLabel("Make:");
makeField = new JTextField();
JLabel modelLabel = new JLabel("Model:");
modelField = new JTextField();
JLabel yearLabel = new JLabel("Year:");
yearField = new JTextField();
JLabel priceLabel = new JLabel("Price:");
priceField = new JTextField();
JButton addButton = new JButton("Add Car");
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
String make = makeField.getText();
String model = modelField.getText();
int year = Integer.parseInt(yearField.getText());
double price = Double.parseDouble(priceField.getText());
// Assert that the price is non-negative
assert price >=0 : "Price should be non-negative";
// Get the database connection
Connection connection = getDatabaseConnection();
// Insert the new car into the database
if (connection != null){
try {
// database insertion code here
String sql = "INSERT INTO Vehicle (make, model, year, price) VALUES (?,?,?,?)";
try (PreparedStatement statement = connection.prepareStatement(sql)){
statement.setString(1, make);
statement.setString(2, model);
statement.setInt(3, year);
statement.setDouble(4, price);
statement.executeUpdate();
System.out.println("Car added successfully.");
}
} catch (SQLException ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error adding car to the database", "Database Error", JOptionPane.ERROR_MESSAGE);
} finally {
// Close the database connection
try {
connection.close();
} catch (SQLException ex){
ex.printStackTrace();
}
}
}
} catch (NumberFormatException ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Invalid number format for year or price", "Input Error", JOptionPane.ERROR_MESSAGE);
}
}
});
panel.add(makeLabel);
panel.add(makeField);
panel.add(modelLabel);
panel.add(modelField);
panel.add(yearLabel);
panel.add(yearField);
panel.add(priceLabel);
panel.add(priceField);
panel.add(addButton);
add(panel);
setVisible(true);
}
private Connection getDatabaseConnection(){
try {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/carjavasql", "root", "root");
} catch (SQLException e){
e.printStackTrace();
return null;
}
}
public static void main(String[] args){
SwingUtilities.invokeLater(()-> new AddCarForm());
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!