Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Items table databse class added #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added RentItAll/build/classes/Item.class
Binary file not shown.
Binary file added RentItAll/build/classes/ItemDBC.class
Binary file not shown.
102 changes: 102 additions & 0 deletions RentItAll/src/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

public class Item {
protected int id;
protected String name;
protected String category;
protected int price;
protected int duration;

public Item() {

}

public Item(int id) {
this.id = id;
}

public Item(int id, String name, String category, int price, int duration) {
this.name = name;
this.category = category;
this.price = price;
this.duration = duration;
this.id = id;
}


public Item(String name, String category, int price, int duration) {
this.name = name;
this.category = category;
this.price = price;
this.duration = duration;
}

public int getID() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public int getDuration() {
return duration;
}

public void setDuration(int duration) {
this.duration = duration;
}
}






























144 changes: 144 additions & 0 deletions RentItAll/src/ItemDBC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.ArrayList;
import java.sql.Statement;
import java.sql.ResultSet;

public class ItemDBC {
private String jdbcURL;
private String jdbcUsername;
private String jdbcPassword;
private Connection jdbcConnection;

public ItemDBC(String jdbcURL, String jdbcUsername, String jdbcPassword) {
this.jdbcURL = jdbcURL;
this.jdbcUsername = jdbcUsername;
this.jdbcPassword = jdbcPassword;
}

protected void connect() throws SQLException {
if (jdbcConnection == null || jdbcConnection.isClosed()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
jdbcConnection = DriverManager.getConnection(
jdbcURL, jdbcUsername, jdbcPassword);
}
}

protected void disconnect() throws SQLException {
if (jdbcConnection != null && !jdbcConnection.isClosed()) {
jdbcConnection.close();
}
}

public boolean insertItem(Item item) throws SQLException {
String sql = "INSERT INTO ITEMS (ID, Name, Category, PricePerDay, Duration) VALUES (?, ?, ?, ?, ?)";
connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, item.getID());
statement.setString(2, item.getName());
statement.setString(3, item.getCategory());
statement.setInt(4, item.getPrice());
statement.setInt(5, item.getDuration());

boolean rowInserted = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowInserted;
}

public List<Item> listAllItems() throws SQLException {
List<Item> listItem = new ArrayList<>();

String sql = "SELECT * FROM ITEMS";

connect();

Statement statement = jdbcConnection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {
int id = resultSet.getInt("ID");
String name = resultSet.getString("Name");
String category = resultSet.getString("Category");
int price = resultSet.getInt("PricePerDay");
int duration = resultSet.getInt("Duration");

Item item = new Item(id, name, category, price, duration);
listItem.add(item);
}

resultSet.close();
statement.close();

disconnect();

return listItem;
}

public boolean deleteItem(Item item) throws SQLException {
String sql = "DELETE FROM ITEMS where ID = ?";

connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, item.getID());

boolean rowDeleted = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowDeleted;
}

public boolean updateItem(Item item) throws SQLException {
String sql = "UPDATE ITEMS SET Name = ?, Category = ?, PricePerDay = ?, Duration = ?";
sql += " WHERE ID = ?";
connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, item.getName());
statement.setString(2, item.getCategory());
statement.setInt(3, item.getPrice());
statement.setInt(4, item.getDuration());
statement.setInt(5, item.getID());

boolean rowUpdated = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowUpdated;
}

public Item getItem(int id) throws SQLException {
Item item = null;
String sql = "SELECT * FROM ITEMS WHERE ID = ?";

connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, id);

ResultSet resultSet = statement.executeQuery();

if (resultSet.next()) {
String name = resultSet.getString("Name");
String category = resultSet.getString("Category");
int price = resultSet.getInt("PricePerDay");
int duration = resultSet.getInt("Duration");

item = new Item(id, name, category, price, duration);
}

resultSet.close();
statement.close();

return item;
}

}