Answered step by step
Verified Expert Solution
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
setTitleAdd Car";
setSize;
setDefaultCloseOperationJFrameDISPOSEONCLOSE;
JPanel panel new JPanel;
panel.setLayoutnew GridLayout;
JLabel makeLabel new JLabelMake:;
makeField new JTextField;
JLabel modelLabel new JLabelModel:;
modelField new JTextField;
JLabel yearLabel new JLabelYear:;
yearField new JTextField;
JLabel priceLabel new JLabelPrice:;
priceField new JTextField;
JButton addButton new JButtonAdd Car";
addButton.addActionListenernew ActionListener
public void actionPerformedActionEvent e
try
String make makeField.getText;
String model modelField.getText;
int year Integer.parseIntyearFieldgetText;
double price Double.parseDoublepriceFieldgetText;
Assert that the price is nonnegative
assert price : "Price should be nonnegative";
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.prepareStatementsql
statement.setString make;
statement.setString model;
statement.setInt year;
statement.setDouble price;
statement.executeUpdate;
System.out.printlnCar added successfully.";
catch SQLException ex
exprintStackTrace;
JOptionPane.showMessageDialognull "Error adding car to the database", "Database Error", JOptionPane.ERRORMESSAGE;
finally
Close the database connection
try
connection.close;
catch SQLException ex
exprintStackTrace;
catch NumberFormatException ex
exprintStackTrace;
JOptionPane.showMessageDialognull "Invalid number format for year or price", "Input Error", JOptionPane.ERRORMESSAGE;
;
panel.addmakeLabel;
panel.addmakeField;
panel.addmodelLabel;
panel.addmodelField;
panel.addyearLabel;
panel.addyearField;
panel.addpriceLabel;
panel.addpriceField;
panel.addaddButton;
addpanel;
setVisibletrue;
private Connection getDatabaseConnection
try
return DriverManager.getConnectionjdbc:mysql:localhost:carjavasql "root", "root";
catch SQLException e
eprintStackTrace;
return null;
public static void mainString args
SwingUtilities.invokeLater new AddCarForm;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started