Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help i need to add a generic class and a sorting and search feature and Add threads and concurrency to my code this is

need help i need to add a generic class and a sorting and search feature and Add threads and concurrency to my code this is my 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

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions