Skip to content
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
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.

41 changes: 33 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,40 @@
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>
<groupId>org.example</groupId>
<artifactId>jdbcdao</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<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>
</project>
Empty file removed src/main/java/daos/DELETEME.txt
Empty file.
93 changes: 93 additions & 0 deletions src/main/java/daos/PotionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package daos;

import models.Potion;

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

public class PotionRepository implements Repository{

private Connection connection;

public PotionRepository(Connection connection) {
this.connection = connection;
}
public Connection getConnection() {
return connection;
}

public void create(Potion potion) {
executeStatement(String.format(new StringBuilder()
.append("INSERT INTO potions.potionsTable(")
.append("id, name, ingredient1, ingredient2, effect)")
.append("VALUES (%s, '%s', '%s', '%s', '%s');")
.toString(),
potion.getId(),
potion.getName(),
potion.getIngredient1(),
potion.getIngredient2(),
potion.getEffect()));
}

public List<Potion> findAll() {
ResultSet resultSet = executeQuery("SELECT * FROM potions.potionsTable;");
List<Potion> list = new ArrayList<>();
try {
while (resultSet.next()) {
String id = resultSet.getString(1);
String name = resultSet.getString(2);
String ingredient1 = resultSet.getString(3);
String ingredient2 = resultSet.getString(4);
String effect = resultSet.getString(5);
list.add(new Potion(
Long.parseLong(id),
name,
ingredient1,
ingredient2,
effect));

}
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
}
return list;
}

public Potion findById(Long id) {
return findAll()
.stream()
.filter(potion -> potion.getId().equals(id))
.findAny()
.get();
}

public void update(Long id, Potion newPotionData) {
executeStatement(String.format(new StringBuilder()
.append("UPDATE potions.potionsTable ")
.append("SET name = '%s', ingredient1 = '%s', ingredient2 = '%s', effect = '%s' WHERE id = %s;")
.toString(),
newPotionData.getName(),
newPotionData.getIngredient1(),
newPotionData.getIngredient2(),
newPotionData.getEffect(),
id));
}

public void delete(Long id) {
Potion potion = findById(id);
executeStatement(String.format(new StringBuilder()
.append("DELETE FROM potions.potionsTable WHERE id = %s")
.toString(),
id));
System.out.printf("%s has been deleted", potion.toString());
}

public void delete(Potion potion) {
Long id = potion.getId();
delete(id);
}
}
44 changes: 44 additions & 0 deletions src/main/java/daos/Repository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package daos;

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

/**
* @author git-leon
* @version 1.0.0
* @date 8/4/21 3:15 PM
*/
public interface Repository {

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();
}
85 changes: 85 additions & 0 deletions src/main/java/main/MainApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main;

import daos.PotionRepository;
import models.Potion;

import java.sql.Connection;
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) {
SQLConnect.registerJDBCDriver();
Connection mysqlDbConnection = SQLConnect.getConnection("mysql");
PotionRepository pokemonRepository = new PotionRepository(mysqlDbConnection);
executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS potions;");
executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS potions;");
executeStatement(mysqlDbConnection, "USE potions;");
executeStatement(mysqlDbConnection, new StringBuilder()
.append("CREATE TABLE IF NOT EXISTS potions.potionsTable(")
.append("id int auto_increment primary key,")
.append("name text not null,")
.append("ingredient1 text not null,")
.append("ingredient2 text not null,")
.append("effect text not null);")
.toString());

pokemonRepository.create(new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"));
pokemonRepository.create(new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"));
System.out.println(pokemonRepository.findAll());

}

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);
System.out.println(new StringJoiner("\n")
.add("Row number = " + rowNumber)
.add("First Column = " + firstColumnData)
.add("Second Column = " + secondColumnData)
.add("Third column = " + thirdColumnData));
}
} catch (SQLException e) {
throw new Error(e);
}
}

static void executeStatement(Connection connection, String sqlStatement) {
try {
Statement statement = getScrollableStatement(connection);
statement.execute(sqlStatement);
} 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);
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/main/SQLConnect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;
import java.util.StringJoiner;

public class SQLConnect {

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 = "jen";
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/models/DELETEME.txt
Empty file.
Loading