Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

Explain the lower-of-cost-or-market approach to valuing inventory.

Answered: 1 week ago

Question

Compare levels of resolution in conflict outcomes?

Answered: 1 week ago

Question

Strategies for Managing Conflict Conflict Outcomes?

Answered: 1 week ago