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

finished lab #3

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ fabric.properties
.idea/sonarlint

# End of https://www.gitignore.io/api/intellij

/target/
/target/*
/target/**

*.DS_Store
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
</dependencies>
</project>
172 changes: 172 additions & 0 deletions src/main/java/daos/DAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package daos;

import models.User;
import models.ConnectionFactory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DAO implements UserDAO {

private List<User> allUsers;

public User findById(Integer id) {

Connection connection = ConnectionFactory.getConnection();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user WHERE id=" + id);
if(rs.next())
{
return extractUserFromResultSet(rs);
}
} catch (SQLException ex) {
ex.printStackTrace();
}

return null;
}

public List<User> findAll() {
allUsers = new ArrayList<User>();
Connection connection = ConnectionFactory.getConnection();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
while(rs.next())
{
allUsers.add(extractUserFromResultSet(rs));
}
return allUsers;

} catch (SQLException ex) {
ex.printStackTrace();
}

return null;
}

public Boolean updateUser(User dto) {
Connection connection = ConnectionFactory.getConnection();
try {
PreparedStatement ps = createStatement(StatementType.UPDATE,connection,dto);
int i = ps.executeUpdate();
if(i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}

public Boolean createUser(User dto) {
Connection connection = ConnectionFactory.getConnection();
try {
PreparedStatement ps = createStatement(StatementType.INSERT,connection,dto);
int i = ps.executeUpdate();
if(i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;

}

public Boolean deleteUser(Integer id) {

Connection connection = ConnectionFactory.getConnection();
try {
Statement stmt = connection.createStatement();
int i = stmt.executeUpdate("DELETE FROM user WHERE id=" + id);
if(i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}


private User extractUserFromResultSet(ResultSet rs) throws SQLException {
User user = new User();
user.setId( rs.getInt("id") );
user.setName( rs.getString("name") );
user.setPass( rs.getString("password") );
user.setEmail(rs.getString("email") );
user.setPhoneNumber(rs.getString("phone_number"));
return user;
}

private PreparedStatement createStatement (StatementType stmtype, Connection connection, User dto) throws SQLException {
PreparedStatement ps;

if (stmtype.getStatementType().equals("Insert")){
ps = connection.prepareStatement("INSERT INTO user VALUES (NULL, ?, ?, ?,?)");
ps.setString(1, dto.getName());
ps.setString(2, dto.getPass());
ps.setString(3,dto.getEmail());
ps.setString(4,dto.getPhoneNumber());

return ps;
}

if (stmtype.getStatementType().equals("Update")){
ps = connection.prepareStatement("UPDATE user SET name=?, password=?, email=?, phone_number=? WHERE id=?");
ps.setString(1, dto.getName());
ps.setString(2, dto.getPass());
ps.setString(3,dto.getEmail());
ps.setString(4,dto.getPhoneNumber());
ps.setInt(5, dto.getId());

return ps;
}
return null;
}
}

enum StatementType{
INSERT (1,"Insert"), UPDATE (2,"Update");

private String statementType;


private Integer key;

StatementType(Integer key, String stmtType) {
statementType = stmtType;
this.key = key;
}


public String getStatementType() {
return statementType;
}

public Integer getKey() {
return key;
}

}
Empty file removed src/main/java/daos/DELETEME.txt
Empty file.
13 changes: 13 additions & 0 deletions src/main/java/daos/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package daos;

import models.User;

import java.util.List;

public interface UserDAO {
User findById (Integer id);
List<User> findAll();
Boolean updateUser (User dto);
Boolean createUser (User dto);
Boolean deleteUser (Integer id);
}
21 changes: 21 additions & 0 deletions src/main/java/models/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package models;

import com.mysql.jdbc.Driver;
import java.sql.*;

public class ConnectionFactory {

public static final String URL = "jdbc:mysql://localhost:3306/myDB?autoReconnect=true&useSSL=false";
public static final String USER = "root";
public static final String PASS = "easy123";


public static Connection getConnection() {
try {
DriverManager.registerDriver(new Driver());
return DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException ex) {
throw new RuntimeException("Error connecting to the database", ex);
}
}
}
Empty file removed src/main/java/models/DELETEME.txt
Empty file.
5 changes: 5 additions & 0 deletions src/main/java/models/DTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package models;

public interface DTO {
Integer getId();
}
62 changes: 62 additions & 0 deletions src/main/java/models/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package models;

public class User implements DTO {

private Integer id;
private String name;
private String pwd;
private String email;
private String phoneNumber;

// ---- constructors ---------------------
public User() {
}

public User(String name, String pass) {
this.name = name;
this.pwd = pass;
}

public User(Integer id, String name, String pass, String email, String phoneNumber) {
this.id = id;
this.name = name;
this.pwd = pass;
this.email = email;
this.phoneNumber = phoneNumber;
}

// --------- getters & setters -------------

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pwd;
}
public void setPass(String pass) {
this.pwd = pass;
}

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
66 changes: 66 additions & 0 deletions src/test/java/daos/DAOTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package daos;

import models.User;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class DAOTest {

DAO testDao;


@Before
public void setUp() throws Exception {
testDao = new DAO();
}


@Test
public void findById() {
User expected = testDao.findById(4);
Assert.assertEquals("Nedda",expected.getName());
}

@Test
public void findAll() {
List<User> allUsers = testDao.findAll();
Integer expected = 10;
Integer actual = allUsers.size();

Assert.assertEquals(expected,actual);
}

@Test
public void updateUser() {
String expected = "newpassword";
User testUpdate = new User(7,"Matilda",expected,null,null);
testDao.updateUser(testUpdate);

User updated = testDao.findById(7);
String actual = updated.getPass();

Assert.assertEquals(expected,actual);
}

@Test
public void createUser() {
User testUpdate = new User(19,"Mark",null,null,null);
testDao.createUser(testUpdate);
Integer expected = 11;
Integer actual = testDao.findAll().size();

Assert.assertEquals(expected,actual);
}

@Test
public void deleteUser() {
testDao.deleteUser(7);
Integer expected = 10;
Integer actual = testDao.findAll().size();

Assert.assertEquals(expected,actual);
}
}
Empty file removed src/test/java/daos/DELETEME.txt
Empty file.
Loading