junit 5 example (#2050)
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package com.stackify.daos;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.stackify.models.User;
|
||||
import com.stackify.utils.ConnectionUtil;
|
||||
|
||||
public class UserDAO {
|
||||
|
||||
private Logger logger = LogManager.getLogger(UserDAO.class);
|
||||
|
||||
public void createTable() {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
String createQuery = "CREATE TABLE users(email varchar(50) primary key, name varchar(50))";
|
||||
PreparedStatement pstmt = con.prepareStatement(createQuery);
|
||||
|
||||
pstmt.execute();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void add(User user) {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
|
||||
String insertQuery = "INSERT INTO users(email,name) VALUES(?,?)";
|
||||
PreparedStatement pstmt = con.prepareStatement(insertQuery);
|
||||
pstmt.setString(1, user.getEmail());
|
||||
pstmt.setString(2, user.getName());
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void update(User user) {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
|
||||
String updateQuery = "UPDATE users SET name=? WHERE email=?";
|
||||
PreparedStatement pstmt = con.prepareStatement(updateQuery);
|
||||
pstmt.setString(1, user.getName());
|
||||
pstmt.setString(2, user.getEmail());
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(User user) {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
|
||||
String deleteQuery = "DELETE FROM users WHERE email=?";
|
||||
PreparedStatement pstmt = con.prepareStatement(deleteQuery);
|
||||
pstmt.setString(1, user.getEmail());
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(String email) {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
|
||||
String deleteQuery = "DELETE FROM users WHERE email=?";
|
||||
PreparedStatement pstmt = con.prepareStatement(deleteQuery);
|
||||
pstmt.setString(1, email);
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public User findOne(String email) {
|
||||
User user = null;
|
||||
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
String query = "SELECT * FROM users WHERE email=?";
|
||||
PreparedStatement pstmt = con.prepareStatement(query);
|
||||
pstmt.setString(1, email);
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
user = new User();
|
||||
user.setEmail(rs.getString("email"));
|
||||
user.setName(rs.getString("name"));
|
||||
}
|
||||
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public List<User> findAll() {
|
||||
List<User> users = new ArrayList<>();
|
||||
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
String query = "SELECT * FROM users";
|
||||
PreparedStatement pstmt = con.prepareStatement(query);
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
User user = new User();
|
||||
user.setEmail(rs.getString("email"));
|
||||
user.setName(rs.getString("name"));
|
||||
users.add(user);
|
||||
}
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
|
||||
String deleteQuery = "DELETE FROM users";
|
||||
PreparedStatement pstmt = con.prepareStatement(deleteQuery);
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.stackify.models;
|
||||
|
||||
public class User {
|
||||
private String email;
|
||||
private String name;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String email, String name) {
|
||||
super();
|
||||
this.email = email;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((email == null) ? 0 : email.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
User other = (User) obj;
|
||||
if (email == null) {
|
||||
if (other.email != null)
|
||||
return false;
|
||||
} else if (!email.equals(other.email))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.stackify.utils;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class ConnectionUtil {
|
||||
|
||||
private static Logger logger = LogManager.getLogger(ConnectionUtil.class);
|
||||
|
||||
public static Connection getConnection() {
|
||||
try {
|
||||
Properties props = new Properties();
|
||||
props.load(ConnectionUtil.class.getResourceAsStream("jdbc.properties"));
|
||||
Class.forName(props.getProperty("jdbc.driver"));
|
||||
Connection con = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.user"), props.getProperty("jdbc.password"));
|
||||
return con;
|
||||
}
|
||||
|
||||
catch (FileNotFoundException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
} catch (IOException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
} catch (ClassNotFoundException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
jdbc.driver=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
|
||||
jdbc.user=
|
||||
jdbc.password=
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user