Skip to content
Open

done #17

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
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.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.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.

44 changes: 35 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,41 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.zipcoder.lab</groupId>
<artifactId>jdbcdao</artifactId>
<groupId>org.example</groupId>
<artifactId>intro-to-jdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
102 changes: 102 additions & 0 deletions src/main/java/daos/CityRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package daos;

import models.City;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* @author git-leon
* @version 1.0.0
* @date 8/4/21 3:05 PM
*/
public class CityRepository implements Repo {
private Connection connection;

public CityRepository(Connection connection) {
this.connection = connection;
}

@Override
public Connection getConnection() {
return connection;
}

public void create(City city) {
executeStatement(String.format(new StringBuilder()
.append("INSERT INTO ciudades.city(")
.append("id, name, population, level) ")
.append("VALUES (%s, '%s', %s, %s);")
.toString(),
city.getId(),
city.getName(),
city.getPopulation(),
city.getLevel()));
}

public List<City> readAll() {
ResultSet resultSet = executeQuery("SELECT * FROM ciudades.city;");
List<City> list = new ArrayList<>();
try {
while (resultSet.next()) {
String id = resultSet.getString(1);
String name = resultSet.getString(2);
Integer population = resultSet.getInt(3);
Integer level = resultSet.getInt(4);
list.add(new City(
Long.parseLong(id),
name,
population,
level));
// Integer.parseInt(population),
// Integer.parseInt(level)));
}
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
}
return list;
}

public City read(Long cityId) {
return readAll()
.stream()
.filter(City -> City.getId().equals(cityId))
.findAny()
.get();
}

public void update(Long id, City newCityData) {
executeStatement(String.format(new StringBuilder()
.append("UPDATE ciudades.city SET ")
.append("name = '%s', ")
.append("population = '%s',")
.append("level = '%s' ")
.append("WHERE id = %s;")
.toString(),

newCityData.getName(),
newCityData.getPopulation(),
newCityData.getLevel(),
id));


}

public void delete(Long id) {
executeStatement(String.format(new StringBuilder()
.append("DELETE FROM ciudades.city ")
.append("WHERE id = %s;")
.toString(),
id));

}

public void delete(City city) {
delete(city.getId());

}

}
38 changes: 38 additions & 0 deletions src/main/java/daos/Conecter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package daos;

import com.mysql.cj.jdbc.Driver;

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

public class Conecter {

public Conecter(){}


public static void registerJDBCDriver() {
// Attempt to register JDBC Driver
try {
DriverManager.registerDriver(Driver.class.newInstance());
} catch (InstantiationException | IllegalAccessException | SQLException e1) {
throw new RuntimeException(e1);
}
}

public static Connection getConnection(String dbVendor) {
String username = "laura";
String password = "zipcode0";
String url = new StringBuilder()
.append("jdbc:")
.append(dbVendor)
.append("://127.0.0.1/")
.append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC")
.toString();
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
throw new Error(e);
}
}
}
Empty file removed src/main/java/daos/DELETEME.txt
Empty file.
40 changes: 40 additions & 0 deletions src/main/java/daos/Repo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package daos;

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

public interface Repo {

default void executeStatement(String sqlStatement) {
try {
Statement statement = getScrollableStatement();
statement.execute(sqlStatement);
} catch (SQLException e) {
throw new Error(e);
}
}

default Statement getScrollableStatement() {
int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
try { // scrollable statements can be iterated more than once without closing
return getConnection().createStatement(resultSetType, resultSetConcurrency);
} catch (SQLException e) {
throw new Error(e);
}
}

default ResultSet executeQuery(String sqlQuery) {
try {
Statement statement = getScrollableStatement();
return statement.executeQuery(sqlQuery);
} catch (SQLException e) {
throw new Error(e);
}
}

Connection getConnection();

}
72 changes: 72 additions & 0 deletions src/main/java/models/City.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package models;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* @author git-leon
* @version 1.0.0
* @date 8/4/21 3:03 PM
*/
public class City {
private Long id;
private String name;
private int population;
private int level;

public City() {
}

public City(Long id, String name, int population, int level) {
this.id = id;
this.name = name;
this.population = population;
this.level = level;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPopulation() {
return population;
}

public void setPopulation(int population) {
this.population = population;
}

public int getLevel() {
return level;
}

public void setLevel(int level) {
this.level = level;
}

@Override
public String toString() {
try {
return new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
return "City{" +
"id=" + id +
", name='" + name + '\'' +
", population=" + population +
", level=" + level +
'}';
}
}
}
Empty file removed src/main/java/models/DELETEME.txt
Empty file.
Loading