Question: hi i need help im new to java im trying to run this code it show up the gui form but when im trying to
hi i need help im new to java im trying to run this code it show up the gui form but when im trying to search a car from my database it shows car not found this my code ; public class AddCarFormJava extends JFrame
private JTextField makeField;
private JTextField modelField;
private JTextField yearField;
private JTextField priceField;
private List vehicleList new ArrayList;
public AddCarFormJava
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
your existing action listener code
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;
;
JButton sortButton new JButtonSort by Price";
sortButton.addActionListenernew ActionListener
public void actionPerformedActionEvent e
Collections.sortvehicleList;
Refresh the display or update a JTable
;
JButton searchButton new JButtonSearch;
searchButton.addActionListenernew ActionListener
public void actionPerformedActionEvent e
String searchMake JOptionPane.showInputDialogEnter make to search:";
Perform linear search for simplicity, consider using a more efficient search algorithm
for Vehicle vehicle : vehicleList
if vehiclegetMakeequalsIgnoreCasesearchMake
JOptionPane.showMessageDialognull "Found: vehicle.toString;
return;
JOptionPane.showMessageDialognull "Car not found.";
;
panel.addmakeLabel;
panel.addmakeField;
panel.addmodelLabel;
panel.addmodelField;
panel.addyearLabel;
panel.addyearField;
panel.addpriceLabel;
panel.addpriceField;
panel.addaddButton;
panel.addsortButton;
panel.addsearchButton;
addpanel;
setVisibletrue;
private Connection getDatabaseConnection
try this is i need to do Add a generic class and a sorting and search feature Add threads and concurrency my database
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
