Question: hi i need help im new to java im trying to run my code that will show the output of my sort price button and

hi i need help im new to java im trying to run my code that will show the output of my sort price button and search button from eclipse that save to my database table vehicle but it will show car not found for example im trying to search KIA but the output show 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(){
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){
//... your existing action listener code ...
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);
}
}
});
JButton sortButton = new JButton("Sort by Price");
sortButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Collections.sort(vehicleList);
// Refresh the display or update a JTable
}
});
JButton searchButton = new JButton("Search");
searchButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String searchMake = JOptionPane.showInputDialog("Enter make to search:");
// Perform linear search (for simplicity, consider using a more efficient search algorithm)
for (Vehicle vehicle : vehicleList){
if (vehicle.getMake().equalsIgnoreCase(searchMake)){
JOptionPane.showMessageDialog(null, "Found: "+ vehicle.toString());
return;
}
}
JOptionPane.showMessageDialog(null, "Car not found.");
}
});
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);
panel.add(sortButton);
panel.add(searchButton);
add(panel);
setVisible(true);
}
private Connection getDatabaseConnection

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!