Created
January 13, 2015 13:51
-
-
Save freekman/e1a893f76437399198ea to your computer and use it in GitHub Desktop.
Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1.adapter.persistence; | |
import com.google.common.collect.Lists; | |
import task1.core.User; | |
import task1.core.UserRepository; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.List; | |
/** | |
* @author Ivan Genchev ([email protected]) | |
*/ | |
public class PersistentUserRepository implements UserRepository { | |
interface RowFetcher<T> { | |
T fetchRow(ResultSet rs) throws SQLException; | |
} | |
@Override | |
public void register(User user) { | |
Connection connection = getConnection(); | |
PreparedStatement stmt; | |
try { | |
// Protects us from SQL injection | |
stmt = connection.prepareStatement("insert into taskone(id, name, age, address) values(?,?,?,?)"); | |
stmt.setInt(1, user.id.intValue()); | |
stmt.setString(2, user.name); | |
stmt.setInt(3, user.age); | |
stmt.setString(4, user.address); | |
stmt.execute(); | |
stmt.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
connection.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public List<User> findAll() { | |
return fetchRows("select id, name, age, address from taskone", new RowFetcher<User>() { | |
@Override | |
public User fetchRow(ResultSet rs) throws SQLException { | |
Integer id = rs.getInt(1); // test with get long | |
String name = rs.getString(2); | |
Integer age = rs.getInt(3); | |
String address = rs.getString(4); | |
return new User(Long.valueOf(id), name, address, age); | |
} | |
}); | |
} | |
private <T> List<T> fetchRows(String query, RowFetcher<T> fetcher) { | |
Connection connection = getConnection(); | |
List<T> result = Lists.newArrayList(); | |
try { | |
Statement stmt = connection.createStatement(); | |
ResultSet rs = stmt.executeQuery(query); | |
while (rs.next()) { | |
T rowItem = fetcher.fetchRow(rs); | |
result.add(rowItem); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
connection.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
return result; | |
} | |
private Connection getConnection() { | |
try { | |
return DriverManager.getConnection("jdbc:postgresql://localhost:5432/jdbc_tasks", "postgres", "1234"); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
//TODO(mgenov) we have to handle properly connection errors | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1.adapter.persistence; | |
import org.junit.Before; | |
import org.junit.Test; | |
import task1.core.User; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.util.List; | |
import static org.hamcrest.Matchers.is; | |
import static org.junit.Assert.*; | |
/** | |
* @author Ivan Genchev ([email protected]) | |
*/ | |
public class PersistentUserRepositoryTest { | |
@Before | |
public void cleanUp() { | |
try { | |
Connection connection = DriverManager.getConnection( | |
"jdbc:postgresql://localhost:5432/jdbc_tasks", "postgres", "1234" | |
); | |
connection.createStatement().executeUpdate("truncate taskone"); | |
connection.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Test | |
public void getWhatWasPut() throws Exception { | |
PersistentUserRepository repository = new PersistentUserRepository(); | |
User user = new User(1l, "Stefan", "my street", 23); | |
repository.register(user); | |
List<User> users = repository.findAll(); | |
assertThat(users.size(), is(1)); | |
assertThat(users.get(0).id,is(1l)); | |
assertThat(users.get(0).name,is("Stefan")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1.core; | |
/** | |
* @author Ivan Genchev ([email protected]) | |
*/ | |
public class User { | |
public final Long id; | |
public final String name; | |
public final String address; | |
public final Integer age; | |
public User(Long id, String name, String address, Integer age) { | |
this.id = id; | |
this.name = name; | |
this.address = address; | |
this.age = age; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1.core; | |
import java.util.List; | |
/** | |
* @author Ivan Genchev ([email protected]) | |
*/ | |
public interface UserRepository { | |
void register(User user); | |
List<User> findAll(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment