diff --git a/.idea/compiler.xml b/.idea/compiler.xml index f006a55..5a76f28 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,6 +6,7 @@ + diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index d97c43d..a6249e2 100644 --- a/pom.xml +++ b/pom.xml @@ -4,15 +4,41 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.zipcoder.lab - jdbcdao + org.example + intro-to-jdbc 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.0 + + 1.8 + 1.8 + + + + + + + + + com.fasterxml.jackson.core + jackson-databind + 2.12.3 + + + mysql + mysql-connector-java + 8.0.18 + + + junit + junit + RELEASE + test + + - - - junit - junit - 4.10 - - \ No newline at end of file diff --git a/src/main/java/daos/CityRepository.java b/src/main/java/daos/CityRepository.java new file mode 100644 index 0000000..9b3de8b --- /dev/null +++ b/src/main/java/daos/CityRepository.java @@ -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 readAll() { + ResultSet resultSet = executeQuery("SELECT * FROM ciudades.city;"); + List 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()); + + } + +} diff --git a/src/main/java/daos/Conecter.java b/src/main/java/daos/Conecter.java new file mode 100644 index 0000000..f39825f --- /dev/null +++ b/src/main/java/daos/Conecter.java @@ -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); + } + } +} diff --git a/src/main/java/daos/DELETEME.txt b/src/main/java/daos/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/daos/Repo.java b/src/main/java/daos/Repo.java new file mode 100644 index 0000000..787de0e --- /dev/null +++ b/src/main/java/daos/Repo.java @@ -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(); + +} diff --git a/src/main/java/models/City.java b/src/main/java/models/City.java new file mode 100644 index 0000000..b43f3ab --- /dev/null +++ b/src/main/java/models/City.java @@ -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 + + '}'; + } + } +} diff --git a/src/main/java/models/DELETEME.txt b/src/main/java/models/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/models/MainApplication.java b/src/main/java/models/MainApplication.java new file mode 100644 index 0000000..00af527 --- /dev/null +++ b/src/main/java/models/MainApplication.java @@ -0,0 +1,116 @@ +package models; + +import com.mysql.cj.jdbc.Driver; +import daos.CityRepository; +import daos.Conecter; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.StringJoiner; + +/** + * @author git-leon + * @version 1.0.0 + * @date 8/2/21 9:49 AM + */ +public class MainApplication { + + public static void main(String[] args) { + Conecter conecter = new Conecter(); + conecter.registerJDBCDriver(); + Connection mysqlDbConnection = conecter.getConnection("mysql"); + CityRepository cityRepository = new CityRepository(mysqlDbConnection); + executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS ciudades;"); + executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS ciudades;"); + executeStatement(mysqlDbConnection, "USE ciudades;"); + executeStatement(mysqlDbConnection, new StringBuilder() + .append("CREATE TABLE IF NOT EXISTS ciudades.city(") + .append("id int auto_increment primary key,") + .append("name text not null,") + .append("population int not null,") + .append("level int null);") + .toString()); + + cityRepository.create(new City(12L, "Chicago", 3, 7)); + cityRepository.create(new City(13L, "New York", 8, 4)); + System.out.println(cityRepository.readAll()); + + } + + static ResultSet executeQuery(Connection connection, String sqlQuery) { + try { + Statement statement = getScrollableStatement(connection); + return statement.executeQuery(sqlQuery); + } catch (SQLException e) { + throw new Error(e); + } + } + + static void printResults(ResultSet resultSet) { + try { + for (int rowNumber = 0; resultSet.next(); rowNumber++) { + String firstColumnData = resultSet.getString(1); + String secondColumnData = resultSet.getString(2); + String thirdColumnData = resultSet.getString(3); + String forthColumnData = resultSet.getString(4); + System.out.println(new StringJoiner("\n") + .add("Row number = " + rowNumber) + .add("First Column = " + firstColumnData) + .add("Second Column = " + secondColumnData) + .add("Third column = " + thirdColumnData) + .add("Forth Column = " + forthColumnData)); + } + } catch (SQLException e) { + throw new Error(e); + } + } + + static void executeStatement(Connection connection, String sqlStatement) { + try { + Statement statement = getScrollableStatement(connection); + statement.execute(sqlStatement); + // connection.setAutoCommit(false); + } catch (SQLException e) { + throw new Error(e); + } + } + + static Statement getScrollableStatement(Connection connection) { + int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE; + int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY; + try { // scrollable statements can be iterated more than once without closing + return connection.createStatement(resultSetType, resultSetConcurrency); + } catch (SQLException e) { + throw new Error(e); + } + } + +// 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); +// } +// } +// +// static void registerJDBCDriver() { +// // Attempt to register JDBC Driver +// try { +// DriverManager.registerDriver(Driver.class.newInstance()); +// } catch (InstantiationException | IllegalAccessException | SQLException e1) { +// throw new RuntimeException(e1); +// } +// } + +} diff --git a/src/main/java/resources/application.properties b/src/main/java/resources/application.properties new file mode 100644 index 0000000..2fbc9ce --- /dev/null +++ b/src/main/java/resources/application.properties @@ -0,0 +1,12 @@ +#spring.jpa.show-sql=true +#spring.jpa.hibernate.ddl-auto=create +#spring.datasource.continue-on-error=true +#spring.main.banner-mode=off +#logging.level.org.springframework=ERROR +#spring.datasource.driver-class-name=com.mysql.jdbc.Driver +#spring.datasource.url=jdbc:mysql://localhost:3306/testdb?allowPublicKeyRetrival=true&useSSL=false&serverTimezone=UTC +#//spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC +#spring.datasource.username=laura +#spring.datasource.password=zipcode0 +# hibernate.dialect=org.hibernate.dialect.MySQL8Dialect +#//spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect \ No newline at end of file diff --git a/target/classes/daos/CityRepository.class b/target/classes/daos/CityRepository.class new file mode 100644 index 0000000..0b791fc Binary files /dev/null and b/target/classes/daos/CityRepository.class differ diff --git a/target/classes/daos/Conecter.class b/target/classes/daos/Conecter.class new file mode 100644 index 0000000..c78e80d Binary files /dev/null and b/target/classes/daos/Conecter.class differ diff --git a/target/classes/daos/Repo.class b/target/classes/daos/Repo.class new file mode 100644 index 0000000..e4870e3 Binary files /dev/null and b/target/classes/daos/Repo.class differ diff --git a/target/classes/models/City.class b/target/classes/models/City.class new file mode 100644 index 0000000..ac96b2e Binary files /dev/null and b/target/classes/models/City.class differ diff --git a/target/classes/models/MainApplication.class b/target/classes/models/MainApplication.class new file mode 100644 index 0000000..a8565dd Binary files /dev/null and b/target/classes/models/MainApplication.class differ