Can someone help me public class Booking { protected static final String URL = \
ID: 3822372 • Letter: C
Question
Can someone help me
public class Booking {
protected static final String URL = "jdbc:derby://localhost:1527/FlightSchedulerDBsfs5208";
private static final String USERNAME = "java";
private static final String PASSWORD = "java";
private String flight;
private Date date;
private WaitList waitlist = new WaitList();
private int seatsBooked;
/**
*
*/
protected static Connection connection; // manages connection
private PreparedStatement selectAllPeople;
private PreparedStatement selectPeopleByLastName;
private PreparedStatement insertNewPerson;
private PreparedStatement insertBookings;
private PreparedStatement insertBooking;
private PreparedStatement getFlightSeats;
public Booking() {
super();
try {
if(!isTableExisting()) {
System.out.println("here");
createTable();
System.out.println("there");
}
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
getFlightSeats = connection.prepareStatement("select count(flight) from Bookings where flight = ? and date = ?");
selectAllPeople = connection.prepareStatement("SELECT * FROM Bookings where flight = ? and date = ?");
selectPeopleByLastName = connection.prepareStatement("SELECT * FROM Flights WHERE Name = ?");
insertNewPerson = connection.prepareStatement("INSERT INTO Flights " +"(flightId,Name,numSeats) " + "VALUES (?, ?, ?)");
insertBookings = connection.prepareStatement("INSERT INTO Bookings " +"(flightId, dayId, customerId) " + "VALUES (?, ?,?)");
insertBooking = connection.prepareStatement("INSERT INTO Bookings " +"(Name,Flight,Date) " + "VALUES (?,?,?)");
}
catch (SQLException sqlException) {
System.out.println("hello there");
sqlException.printStackTrace();
System.exit(1);
}
}
private static boolean isTableExisting()
{
try {
PreparedStatement s = connection.prepareStatement(
"SELECT * FROM Bookings");
s.execute();
return true;
} catch (SQLException ex) {
return false;
}
}
private void createTable() throws SQLException
{
PreparedStatement create = connection.prepareStatement(
"CREATE TABLE Bookings("
+ "bookingId INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1), "
+ "name VARCHAR(20), "
+ "flight VARCHAR(20) "
+ "date DATE "
+ "PRIMARY KEY(bookingId))");
create.execute();
}
Explanation / Answer
You are trying to use connection object before instantiating it. Thats why you are getting null pointer exception.I have highlighted that part. You are trying to use this connection object in isTableExisting() and createTable() methods before instantiating it.
public class Booking {
protected static final String URL = "jdbc:derby://localhost:1527/FlightSchedulerDBsfs5208";
private static final String USERNAME = "java";
private static final String PASSWORD = "java";
private String flight;
private Date date;
private WaitList waitlist = new WaitList();
private int seatsBooked;
/**
*
*/
protected static Connection connection; // manages connection
private PreparedStatement selectAllPeople;
private PreparedStatement selectPeopleByLastName;
private PreparedStatement insertNewPerson;
private PreparedStatement insertBookings;
private PreparedStatement insertBooking;
private PreparedStatement getFlightSeats;
public Booking() {
super();
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
if(!isTableExisting()) {
System.out.println("here");
createTable();
System.out.println("there");
}
getFlightSeats = connection.prepareStatement("select count(flight) from Bookings where flight = ? and date = ?");
selectAllPeople = connection.prepareStatement("SELECT * FROM Bookings where flight = ? and date = ?");
selectPeopleByLastName = connection.prepareStatement("SELECT * FROM Flights WHERE Name = ?");
insertNewPerson = connection.prepareStatement("INSERT INTO Flights " +"(flightId,Name,numSeats) " + "VALUES (?, ?, ?)");
insertBookings = connection.prepareStatement("INSERT INTO Bookings " +"(flightId, dayId, customerId) " + "VALUES (?, ?,?)");
insertBooking = connection.prepareStatement("INSERT INTO Bookings " +"(Name,Flight,Date) " + "VALUES (?,?,?)");
}
catch (SQLException sqlException) {
System.out.println("hello there");
sqlException.printStackTrace();
System.exit(1);
}
}
private static boolean isTableExisting()
{
try {
PreparedStatement s = connection.prepareStatement(
"SELECT * FROM Bookings");
s.execute();
return true;
} catch (SQLException ex) {
return false;
}
}
private void createTable() throws SQLException
{
PreparedStatement create = connection.prepareStatement(
"CREATE TABLE Bookings("
+ "bookingId INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1), "
+ "name VARCHAR(20), "
+ "flight VARCHAR(20) "
+ "date DATE "
+ "PRIMARY KEY(bookingId))");
create.execute();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.