Sunteți pe pagina 1din 6

package com.xyz.crms.controller.

manager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import com.xyz.crms.model.Car;
public class CarManager
{
private void writeCar(Car car, PreparedStatement ps) throws SQLException
{
//Set the attributes of the SQL statement
ps.setString(1, car.getPlateNo());
ps.setString(2, car.getModel());
ps.setDouble(3, car.getPrice());
ps.setString(4, Character .toString(car.getStatus()));
//Only applicable when updating
if(car.getCarID() !=0)
ps.setInt(5, car.getCarID());
}
public int addCar(Car car) throws ClassNotFoundException, SQLException /
/Has possibilities to occur
{
//Load database driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Create database connection from a database URL by using driver
manager class
Connection connection = DriverManager.getConnection("jdbc:derby:
//localhost:1527/dbCRMS","irfan","123");
//Create an SQL statement to be sent to database using existing
connection
PreparedStatement ps = connection.prepareStatement("INSERT INTO
Car(PlateNo,Model,Price,Status)VALUES(?,?,?,?)", PreparedStatement.RETURN_GENERA
TED_KEYS);
//Set the attributes of the SQL statement
ps.setString(1, car.getPlateNo());
ps.setString(2, car.getModel());
ps.setDouble(3, car.getPrice());
ps.setString(4, Character .toString(car.getStatus()));
//Send the SQL statement to the databases and get the status
int status = ps.executeUpdate();
//Get the generated IID if the status is not fail
if(status!=0)
{
//Get the result set containing the generated IDs
ResultSet rs=ps.getGeneratedKeys();
//Assign the generated ID as object ID if available
if(rs.next())
car.setCarID(rs.getInt(1));
}
//Close the database connection
connection.close();
//Return the execution status
return status;
}
public int updateCar(Car car) throws ClassNotFoundException, SQLExceptio
n //Has possibilities to occur
{
//Load database driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Create database connection from a database URL by using driver
manager class
Connection connection = DriverManager.getConnection("jdbc:derby:
//localhost:1527/dbCRMS","irfan","123");
//Create an SQL statement to be sent to database using existing
connection
PreparedStatement ps = connection.prepareStatement("UPDATE Car S
ET PlateNo =?,Model=?,Price=?,Status=? WHERE CarID=?", PreparedStatement.RETURN_
GENERATED_KEYS);
//Set he attributes of the SQL statement
writeCar(car,ps);
//Send the SQL statement to the databases and get the status
int status = ps.executeUpdate();
//Close the database connection
connection.close();
//Return the execution status
return status;
}
public int deleteCar(Car car) throws ClassNotFoundException, SQLExceptio
n //Has possibilities to occur
{
//Load database driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Create database connection from a database URL by using driver
manager class
Connection connection = DriverManager.getConnection("jdbc:derby:
//localhost:1527/dbCRMS","irfan","123");
//Create an SQL statement to be sent to database using existing
connection
PreparedStatement ps = connection.prepareStatement("DELETE FROM
Car WHERE CarID=?", PreparedStatement.RETURN_GENERATED_KEYS);
//Set the attributes of the SQL statement
ps.setInt(1, car.getCarID());
//Send the SQL statement to the databases and get the status
int status = ps.executeUpdate();
//Close the database connection
connection.close();
//Return the execution status
return status;
}
private ArrayList<Car> searchCars(PreparedStatement ps) throws SQLExcept
ion
{
//Create the list to contain the results
ArrayList<Car> cars = new ArrayList<>();
//Send the SQL statement to the databases and get the status
ResultSet rs = ps.executeQuery();
//Iterate the result returned
while (rs.next())
{
Car car = new Car();
car.setCarID(rs.getInt("CarID"));
car.setPlateNo(rs.getString("PlateNo"));
car.setModel(rs.getString("Model"));
car.setPrice(rs.getDouble("Price"));
car.setStatus(rs.getString("Status").charAt(0));
cars.add(car);
}
//Return the list
return cars;
}
public ArrayList<Car> searchCars(String keyword, int type) throws ClassN
otFoundException, SQLException //Has possibilities to occur
{
//Create the list to contain the results
ArrayList<Car> cars = new ArrayList<>();
//Load database driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Create database connection from a database URL by using driver
manager class
Connection connection = DriverManager.getConnection("jdbc:derby:
//localhost:1527/dbCRMS","irfan","123");
//Create an SQL statement to be sent to database using existing
connection
PreparedStatement ps = null;
//Option 1
if(type==0)
ps = connection.prepareStatement(" SELECT * FROM Car WHE
RE UPPER(PlateNo) LIKE ? ", PreparedStatement.RETURN_GENERATED_KEYS);
else
ps = connection.prepareStatement(" SELECT * FROM Car WHE
RE UPPER(Model) LIKE ? ", PreparedStatement.RETURN_GENERATED_KEYS);
//Option 2
ps = connection.prepareStatement(type == 0 ? " SELECT * FROM Car
WHERE UPPER(PlateNo) LIKE ? " : " SELECT * FROM Car WHERE UPPER(Model) LIKE ?",
PreparedStatement.RETURN_GENERATED_KEYS);
//Option 3
ps = connection.prepareStatement("SELECT * FROM Car WHERE UPPER(
"+ (type == 0 ? "PlateNo" : "Model") + ") LIKE ?", PreparedStatement.RETURN_GENE
RATED_KEYS);
//Set the attributes of the SQL statement
ps.setString(1, "%" + keyword.toUpperCase() + "%");
//Send the SQL statement to the databases and get the status
ResultSet rs = ps.executeQuery();
//Iterate the result returned
while (rs.next())
{
Car car = new Car();
car.setCarID(rs.getInt("CarID"));
car.setPlateNo(rs.getString("PlateNo"));
car.setModel(rs.getString("Model"));
car.setPrice(rs.getDouble("Price"));
car.setStatus(rs.getString("Status").charAt(0));
cars.add(car);
}
//Close the database connection
connection.close();
//Return the execution status
return cars;
}
public ArrayList<Car> searchCars(double price, int type) throws ClassNot
FoundException, SQLException //Has possibilities to occur
{
//Create the list to contain the results
ArrayList<Car> cars = new ArrayList<>();
//Load database driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Create database connection from a database URL by using driver
manager class
Connection connection = DriverManager.getConnection("jdbc:derby:
//localhost:1527/dbCRMS","irfan","123");
//Create an SQL statement to be sent to database using existing
connection
PreparedStatement ps = null;
//Option 1
if(type==0)
ps = connection.prepareStatement(" SELECT * FROM Car WHE
RE Price < ? ", PreparedStatement.RETURN_GENERATED_KEYS);
else if(type==1)
ps = connection.prepareStatement(" SELECT * FROM Car WHE
RE Price = ? ", PreparedStatement.RETURN_GENERATED_KEYS);
else if(type==2)
ps = connection.prepareStatement(" SELECT * FROM Car WHE
RE Price >= ? ", PreparedStatement.RETURN_GENERATED_KEYS);
//Option 2
String condition = "";
if(type==0)
condition = "<=";
else if(type==1)
condition = "=";
else if(type==2)
condition = ">=";
ps=connection.prepareStatement("SELECT * FROM Car WHERE Price "
+ condition + " ?", PreparedStatement.RETURN_GENERATED_KEYS);

//Set the attributes of the SQL statement


ps.setDouble(1, price);
//Send the SQL statement to the databases and get the status
ResultSet rs = ps.executeQuery();
//Iterate the result returned
while (rs.next())
{
Car car = new Car();
car.setCarID(rs.getInt("CarID"));
car.setPlateNo(rs.getString("PlateNo"));
car.setModel(rs.getString("Model"));
car.setPrice(rs.getDouble("Price"));
car.setStatus(rs.getString("Status").charAt(0));
cars.add(car);
}
//Close the database connection
connection.close();
//Return the execution status
return cars;
}
public ArrayList<Car> searchCars(Date start) throws ClassNotFoundExcepti
on, SQLException //Has possibilities to occur
{
//Create the list to contain the results
ArrayList<Car> cars = new ArrayList<>();
//Load database driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Create database connection from a database URL by using driver
manager class
Connection connection = DriverManager.getConnection("jdbc:derby:
//localhost:1527/dbCRMS","irfan","123");
//Create an SQL statement to be sent to database using existing
connection
PreparedStatement ps = connection.prepareStatement("SELECT * FRO
M Car WHERE CarID NOT IN(SELECT CarID FROM Rental WHERE ? BETWEEN Start AND {fn
TIMESTAMPADD(SQL_TSI_MINUTE,Duration * 60 -1,Start})AND Status = 'A'", PreparedS
tatement.RETURN_GENERATED_KEYS);
//Set the attributes of the SQL statement
ps.setTimestamp(1, new Timestamp(start.getTime()));
//Send the SQL statement to the databases and get the status
ResultSet rs = ps.executeQuery();
//Iterate the result returned
while (rs.next())
{
Car car = new Car();
car.setCarID(rs.getInt("CarID"));
car.setPlateNo(rs.getString("PlateNo"));
car.setModel(rs.getString("Model"));
car.setPrice(rs.getDouble("Price"));
car.setStatus(rs.getString("Status").charAt(0));
cars.add(car);
}
//Close the database connection
connection.close();
//Return the execution status
return cars;
}
}

S-ar putea să vă placă și