Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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(){
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(){
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

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions