I have an application that is designed to manage the flights for an airline. I'm trying to store the flight info in a mysql database. I'm able to establish a connection to the database and send the info from my GUI to the appropriate method to add flight, delete flight, and search for a flight. I have tried a number of things and I must be doing something wrong because it is not working. I was getting a null pointer exception which I have corrected via the updated code below in the 2nd code block. However the records on the database are still not updating. Any help would be appreciated.
Thanks
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.color.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.border.*;
import java.sql.*;
import javax.swing.*;
public class FlightManager extends JFrame implements ActionListener{
// JDBC driver name and database url
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/flightmanager";
// jtextfield components
private JTextField fn = new JTextField(15);
private JTextField oc = new JTextField(15);
private JTextField dc = new JTextField(15);
private JTextField dt = new JTextField(15);
// labels telling user what each jtextfield is for
private JLabel fnl = new JLabel("Flight Number: ");
private JLabel ocl = new JLabel("City of Origin: ");
private JLabel dcl = new JLabel("Destination City: ");
private JLabel dtl = new JLabel("Departure Time: ");
// variables that hold data added to jtextfield
private int flightNum;
private String originCity;
private String destinationCity;
private String departure;
// jbutton components
private JButton add;
private JButton remove;
private JButton search;
private JButton exit;
private DatabaseUpdate du = new DatabaseUpdate();
public static void main(String[] args) throws SQLException {
try {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
Class.forName(DRIVER);
connection = DriverManager.getConnection(DATABASE_URL, "root", "lilly");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
FlightManager fm = new FlightManager();
Container pc = new Container();
pc.setLayout(new BoxLayout(pc, BoxLayout.Y_AXIS));
JPanel blank = new JPanel();
pc.add(blank);
pc.add(fm.getFlightInputPanel());
pc.add(fm.getOriginPanel());
pc.add(fm.getDestinationPanel());
pc.add(fm.getDepartureTimePanel());
JFrame f = new JFrame();
f.setTitle("Flight Manager");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(pc, "First");
f.add(fm.getButtonPanel(), "Last"); // adds button panel to JFrame
f.setSize(500, 200);
f.setLocation(200,200);
f.setVisible(true);
}
private JPanel getFlightInputPanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel fnp = new JPanel();
fnp.add(fnl);
fnp.add(fn);
return fnp;
}
private JPanel getOriginPanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel ocp = new JPanel();
ocp.add(ocl);
ocp.add(oc);
return ocp;
}
private JPanel getDestinationPanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel dcp = new JPanel();
dcp.add(dcl);
dcp.add(dc);
return dcp;
}
private JPanel getDepartureTimePanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel dtp = new JPanel();
dtp.add(dtl);
dtp.add(dt);
return dtp;
}
private JPanel getButtonPanel(){
FlowLayout buttonLayout = new FlowLayout(FlowLayout.CENTER);
JPanel buttonPanel = new JPanel();
add = new JButton("Add Flight");
add.addActionListener(this);
remove = new JButton("Delete Flight");
remove.addActionListener(this);
search = new JButton("Search For Flight");
search.addActionListener(this);
exit = new JButton("Exit");
exit.addActionListener(this);
buttonPanel.add(add);
buttonPanel.add(remove);
buttonPanel.add(search);
buttonPanel.add(exit);
return buttonPanel;
}
// processes actions for when buttons are pushed
public void actionPerformed(ActionEvent e) {
Object ac = e.getSource();
// action to take if add flight button is pushed
if(ac == add){
flightNum = Integer.parseInt(fn.getText());
originCity = oc.getText();
destinationCity = dc.getText();
departure = dt.getText();
du.addFlight(flightNum, originCity, destinationCity, departure);
// clears out current contents of JTextFields
fn.setText("");
oc.setText("");
dc.setText("");
dt.setText("");
}
// action to take if search for flight is pushed
if(ac == search){
originCity = oc.getText();
destinationCity = dc.getText();
du.searchFlight(originCity, destinationCity);
oc.setText("");
dc.setText("");
}
if(ac == remove){
flightNum = Integer.parseInt(fn.getText());
originCity = oc.getText();
destinationCity = dc.getText();
departure = dt.getText();
du.removeFlight(flightNum, originCity, destinationCity, departure);
}
// action to take if exit button is pushed
if(ac == exit){
System.exit(0);
}
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.color.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.border.*;
import java.sql.*;
import javax.swing.*;
public class DatabaseUpdate {
// JDBC driver name and database url
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/flightmanager";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
public DatabaseUpdate(){
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(DATABASE_URL, "root", "lilly");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
public void addFlight(int flightNum, String originCity, String destinationCity, String departure) {
int flight = flightNum;
String origin = originCity;
String destination = destinationCity;
String departTime = departure;
try{
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
connection.setAutoCommit(false);
statement.addBatch("INSERT INTO flightinfo(flightnumber, origincity, destinationcity, departuretime) VALUES(flight, 'origincity', 'destinationcity', 'departuretime')");
int[] updateCounts = statement.executeBatch();
connection.commit();
resultSet = statement.executeQuery("SELECT * FROM flightinfo");
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
public void removeFlight(int flightNum, String originCity, String destinationCity, String departure) {
int flight = flightNum;
String origin = originCity;
String destination = destinationCity;
String departTime = departure;
try{
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
connection.setAutoCommit(false);
statement.addBatch("DELETE FROM flightinfo WHERE FlightNumber = 'flight'");
int[] updateCounts = statement.executeBatch();
connection.commit();
// String sql = "DELETE FROM flightinfo WHERE FlightNumber = flight";
// int delete = statement.executeUpdate(sql);
// if(delete == 1){
System.out.println("Row is deleted.");
// }
// else{
// System.out.println("Row is not deleted.");
// }
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
void searchFlight(String originCity, String destinationCity) {
String origin = originCity;
String destination = destinationCity;
try{
}catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
}