@@ -11,7 +11,7 @@
import models.Users;

/**
*
*
* @author Skyler Layne on Feb 8, 2016
*
* @version 0.1.0
@@ -25,7 +25,7 @@ public PoDataAccessObject() {

/**
* Get the Po Bean from the passed result set.
*
*
* @param rs
* - the result of the query.
* @return - a list of the Po Beans that are found from the query.
@@ -50,7 +50,6 @@ public synchronized Pos get(ResultSet rs) {

rs.close();
this.getStmt().close();
close();
return pos;

} catch (SQLException e) {
@@ -62,7 +61,7 @@ public synchronized Pos get(ResultSet rs) {

/**
* Get all the po Beans in the objects.
*
*
* @return - A list of all the po beans.
*/
public Pos findAll() {
@@ -79,7 +78,7 @@ public Pos findAll() {

/**
* Get the po beans with the registered id.
*
*
* @param id
* - the id to look for.
* @return - a list of po beans with that id.
@@ -119,7 +118,6 @@ public Pos findByUserId(String id) {

rs.close();
this.getStmt().close();
close();
return pos;

} catch (SQLException e) {
@@ -131,7 +129,7 @@ public Pos findByUserId(String id) {

/**
* Insert a po into the database.
*
*
* @param po
* - the po to be inserted.
* @return - if the po was inserted or not.
@@ -151,7 +149,6 @@ public boolean insert(PoBean po) {
pstmt.setInt(6, po.getAddress().getId());
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -161,7 +158,7 @@ public boolean insert(PoBean po) {

/**
* Update a po into the database.
*
*
* @param po
* - the po to be updated.
* @return - if the po was updated or not.
@@ -180,7 +177,6 @@ public boolean update(PoBean po) {
pstmt.setInt(5, po.getAddress().getId());
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -190,7 +186,7 @@ public boolean update(PoBean po) {

/**
* Delete a po into the database.
*
*
* @param po
* - the po to be deleted.
* @return - if the po was deleted or not.
@@ -203,7 +199,6 @@ public boolean delete(PoBean po) {
pstmt = this.getCon().prepareStatement(insert);
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -49,7 +49,6 @@ private PoItems get(ResultSet rs) {

rs.close();
this.getStmt().close();
close();
return poItems;

} catch (SQLException e) {
@@ -94,7 +93,7 @@ public PoItems findById(String id) {

/**
* Insert poitem into the database.
*
*
* @param poitem
* - item to be inserted.
* @return - if the insert worked or not.
@@ -110,7 +109,6 @@ public boolean insert(PoItemBean poitem) {
pstmt.setDouble(3, poitem.getPrice());
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -120,7 +118,7 @@ public boolean insert(PoItemBean poitem) {

/**
* Update poitem into the database.
*
*
* @param poitem
* - item to be iupdated.
* @return - if the update worked or not.
@@ -136,7 +134,6 @@ public boolean update(PoItemBean poitem) {
pstmt.setDouble(2, poitem.getPrice());
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -146,7 +143,7 @@ public boolean update(PoItemBean poitem) {

/**
* Delete poitem into the database.
*
*
* @param poitem
* - item to be deleted.
* @return - if the delete worked or not.
@@ -159,7 +156,6 @@ public boolean delete(PoItemBean poitem) {
pstmt = this.getCon().prepareStatement(insert);
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -0,0 +1,29 @@
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

class SingletonConnection {
private static Connection con = null;

public static Connection getConnection() {
try {
if (SingletonConnection.con == null || SingletonConnection.con.isClosed()) {
String host = "mysql";
String user = "bookstore_user";
String pass = "4413";
String db = "ecommerce";
String url = "jdbc:mysql://" + host + "/" + db + "?useSSL=false";

Class.forName("com.mysql.jdbc.Driver").newInstance();
SingletonConnection.con = DriverManager.getConnection(url, user, pass);
}
} catch (Exception ex) {
ex.printStackTrace();
}

return SingletonConnection.con;
}
}
@@ -29,8 +29,9 @@ public UserDataAccessObject() {

private synchronized Users get(ResultSet rs) {
List<UserBean> users = new ArrayList<>();
try (Connection con = this.getCon();
Statement stmnt = this.getStmt()) {
try {
Connection con = this.getCon();
Statement stmnt = this.getStmt();
con.setReadOnly(true);
while (rs.next()) {
int id = rs.getInt("id");
@@ -59,9 +60,9 @@ private synchronized Users get(ResultSet rs) {
*/
public Users findAll() {
this.createConnection();
try (Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement(this.getAllQuery())) {
try {
Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement(this.getAllQuery());
ResultSet rs = stmnt.executeQuery();
return get(rs);
} catch (SQLException e) {
@@ -82,9 +83,10 @@ public Users findAll() {
*/
public Users get(String id, String hash) {
this.createConnection();

try (Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement(this.getAllQuery() + " where username=? AND password=?;")) {

try {
Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement(this.getAllQuery() + " where username=? AND password=?;");
stmnt.setString(1, id);
stmnt.setString(2, hash);
ResultSet rs = stmnt.executeQuery();
@@ -104,9 +106,10 @@ public Users get(String id, String hash) {
*/
public Users findByUsername(String username) {
this.createConnection();

try (Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement(this.getAllQuery() + " where username=?;")) {

try {
Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement(this.getAllQuery() + " where username=?;");
stmnt.setString(1, username);
ResultSet rs = stmnt.executeQuery();
return get(rs);
@@ -125,9 +128,10 @@ public Users findByUsername(String username) {
*/
public Users findByUserid(String id) {
this.createConnection();

try (Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement("select * from user where id=?;")) {

try {
Connection con = this.getCon();
PreparedStatement stmnt = con.prepareStatement("select * from user where id=?;");
stmnt.setString(1, id);
ResultSet rs = stmnt.executeQuery();
return get(rs);
@@ -146,7 +150,7 @@ public Users findByUserid(String id) {
*/
public boolean insert(UserBean user) {
this.createConnection();

try (Connection con = this.getCon();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO ? (id, username, password, admin, addressid) VALUES (?,?,?,?,?);")) {
pstmt.setString(1, this.getTableName());
@@ -172,7 +176,7 @@ public boolean insert(UserBean user) {
*/
public boolean update(UserBean user) {
this.createConnection();

try (Connection con = this.getCon();
PreparedStatement pstmt = con.prepareStatement("UPDATE ? SET username=?, password=?, admin=?, addressid=? where id=")) {
pstmt.setString(1, this.getTableName());
@@ -199,7 +203,7 @@ public boolean update(UserBean user) {
*/
public boolean delete(UserBean user) {
this.createConnection();

try (Connection con = this.getCon();
PreparedStatement pstmt = con.prepareStatement("DELETE FROM ? where id = ? ;")) {
pstmt.setString(1, this.getTableName());
@@ -13,9 +13,9 @@
import models.Visits;

/**
*
*
* VisitEvent DAO.
*
*
* @author Skyler Layne on Feb 17, 2016
*
*/
@@ -28,7 +28,7 @@ public VisitEventDataAccessObject() {

/**
* Get the PoItemBean from the passed result set.
*
*
* @param rs
* - the result of the query.
* @return - a list of the PoItemBean that are found from the query.
@@ -53,7 +53,6 @@ private Visits get(ResultSet rs) {

rs.close();
this.getStmt().close();
close();
return visits;

} catch (SQLException e) {
@@ -64,7 +63,7 @@ private Visits get(ResultSet rs) {

/**
* Get all the PoItemBean in the objects.
*
*
* @return - A list of all the PoItemBean.
*/
public Visits findAll() {
@@ -80,7 +79,7 @@ public Visits findAll() {

/**
* Get the Visits that happen on a particular day.
*
*
* @param day
* - the day to find visits (DD/MM/YYYY).
* @return - The Visits on that day.
@@ -98,7 +97,7 @@ public Visits findByDay(String day) {

/**
* Insert the visit event into the database.
*
*
* @param visit
* - the visit event.
* @return - if the insert was successful
@@ -115,7 +114,6 @@ public boolean insert(VisitEventBean visit) {
pstmt.setString(3, visit.getEventType());
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -125,7 +123,7 @@ public boolean insert(VisitEventBean visit) {

/**
* Update the visit event in the database.
*
*
* @param visit
* - the visit event.
* @return - if the update was successful.
@@ -141,7 +139,6 @@ public boolean update(VisitEventBean visit) {
pstmt.setString(2, visit.getEventType());
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());
@@ -151,7 +148,7 @@ public boolean update(VisitEventBean visit) {

/**
* Delete the visit event from the database.
*
*
* @param visit
* - the visit event.
* @return - if the delete was successful.
@@ -164,7 +161,6 @@ public boolean delete(VisitEventBean visit) {
pstmt = this.getCon().prepareStatement(insert);
pstmt.executeUpdate();
pstmt.close();
close();
return true;
} catch (SQLException e) {
System.out.println("SQL Exception" + e.getErrorCode() + e.getMessage());