Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Add JDBC and count method for DAO
Browse files Browse the repository at this point in the history
  • Loading branch information
hardingadonis committed Jan 22, 2024
1 parent 593869f commit fca51be
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/hardingadonis/saledock/dao/IDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public interface IDAO<T> {
public Optional<T> getByID(Integer ID);

public List<T> getAll();

default Long count() {
return 0L;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.hardingadonis.saledock.dao.*;
import io.hardingadonis.saledock.model.*;
import io.hardingadonis.saledock.utils.*;
import java.sql.*;
import java.util.*;
import org.hibernate.*;

Expand Down Expand Up @@ -38,4 +39,27 @@ public List<Customer> getAll() {
return session.createQuery("FROM Customer", Customer.class).getResultList();
}
}

@Override
public Long count() {
Long count = 0L;

try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("SELECT COUNT(*) FROM `customer`");

ResultSet rs = smt.executeQuery();

if (rs.next()) {
count = rs.getLong(1);
}

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}

return count;
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/hardingadonis/saledock/dao/impl/OrderDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.hardingadonis.saledock.dao.*;
import io.hardingadonis.saledock.model.*;
import io.hardingadonis.saledock.utils.*;
import java.sql.*;
import java.util.*;
import org.hibernate.*;

Expand Down Expand Up @@ -38,4 +39,27 @@ public List<Order> getAll() {
return session.createQuery("FROM Order", Order.class).getResultList();
}
}

@Override
public Long count() {
Long count = 0L;

try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("SELECT COUNT(*) FROM `order`");

ResultSet rs = smt.executeQuery();

if (rs.next()) {
count = rs.getLong(1);
}

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}

return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.hardingadonis.saledock.dao.*;
import io.hardingadonis.saledock.model.*;
import io.hardingadonis.saledock.utils.*;
import java.sql.*;
import java.util.*;
import org.hibernate.*;

Expand Down Expand Up @@ -38,4 +39,27 @@ public List<Product> getAll() {
return session.createQuery("FROM Product", Product.class).getResultList();
}
}

@Override
public Long count() {
Long count = 0L;

try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("SELECT COUNT(*) FROM `product`");

ResultSet rs = smt.executeQuery();

if (rs.next()) {
count = rs.getLong(1);
}

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}

return count;
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/hardingadonis/saledock/utils/IDBContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.hardingadonis.saledock.utils;

import java.sql.*;

public interface IDBContext {

public Connection getConnection();

public void closeConnection(Connection connection);
}
5 changes: 5 additions & 0 deletions src/main/java/io/hardingadonis/saledock/utils/Singleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.hardingadonis.saledock.dao.*;
import io.hardingadonis.saledock.dao.impl.*;
import io.hardingadonis.saledock.utils.impl.*;

public class Singleton {

Expand All @@ -14,6 +15,8 @@ public class Singleton {
public static IOrderDAO orderDAO;

public static IProductDAO productDAO;

public static IDBContext dbContext;

static {
categoryDAO = new CategoryDAOImpl();
Expand All @@ -25,5 +28,7 @@ public class Singleton {
orderDAO = new OrderDAOImpl();

productDAO = new ProductDAOImpl();

dbContext = new DBContextMySQLImpl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.hardingadonis.saledock.utils.impl;

import java.io.*;
import java.sql.*;
import java.util.*;

import io.hardingadonis.saledock.utils.*;

public class DBContextMySQLImpl implements IDBContext {

private String dbURL;
private String user;
private String password;

public DBContextMySQLImpl() {
try (InputStream input = DBContextMySQLImpl.class.getClassLoader().getResourceAsStream("config.mysql.properties")) {
Properties prop = new Properties();

if (input == null) {
this.dbURL = "jdbc:mysql://localhost:3306/saledock";
this.user = "root";
this.password = "";
} else {
prop.load(input);

this.dbURL = "jdbc:mysql://" + prop.getProperty("config.jdbc.url") + ":3306/" + prop.getProperty("config.jdbc.database_name");
this.user = prop.getProperty("config.jdbc.user");
this.password = prop.getProperty("config.jdbc.password");
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}

@Override
public Connection getConnection() {
Connection connection = null;

try {
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

connection = DriverManager.getConnection(this.dbURL, this.user, this.password);

System.out.println("Connect MySQL successfully!");
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}

return connection;
}

@Override
public void closeConnection(Connection connection) {
try {
if (connection != null) {
connection.close();

System.out.println("Close MySQL successfully!");
}
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.mysql.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config.jdbc.url=localhost
config.jdbc.database_name=saledock
config.jdbc.user=root
config.jdbc.password=

0 comments on commit fca51be

Please sign in to comment.