Skip to content

Commit

Permalink
Class 18 - Maven
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrecpedro committed Aug 18, 2022
1 parent 0d6db1a commit c0f636e
Show file tree
Hide file tree
Showing 28 changed files with 1,005 additions and 22 deletions.
6 changes: 0 additions & 6 deletions Aula 18 - Maven/class_18_maven/Medicine/.idea/workspace.xml

This file was deleted.

16 changes: 0 additions & 16 deletions Aula 18 - Maven/class_18_maven/Medicine/pom.xml

This file was deleted.

11 changes: 11 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/.idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

463 changes: 463 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/.idea/dbnavigator.xml

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/.idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/avisos.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[18-08-2022 20:04:10] [ DEBUG] [MedicineDaoImpl:26] Saving a new medicine: Medicine { name = Ibuprofeno | laboratory = Aché | quantity = 395 | price = R$ 23,00 }
7 changes: 7 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--DROP TABLE IF EXISTS medicines;
CREATE TABLE IF NOT EXISTS medicines (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(64),
laboratory VARCHAR(64),
quantity INT,
price DOUBLE);
46 changes: 46 additions & 0 deletions Aula 18 - Maven/class_18_maven/Pharmacy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>org.example</groupId>
<artifactId>Pharmacy</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.dh.medicine.dao;

public interface IDao<T> {
T save(T t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.dh.medicine.dao;

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

public class SettingJDBC {
/** Attributes **/
private String jdbcDriver, dbUrl, userName, userPassword;

/** Constructor **/
public SettingJDBC(String jdbcDriver, String dbUrl, String userName, String userPassword) {
this.jdbcDriver = jdbcDriver;
this.dbUrl = dbUrl;
this.userName = userName;
this.userPassword = userPassword;
}

public SettingJDBC() {
this.jdbcDriver = "org.h2.Driver";
this.dbUrl = "jdbc:h2:mem:medicines;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'create.sql'";
this.userName = "sa";
this.userPassword = "";
}

/** Method **/
public Connection connectWithDatabase() {
Connection connection = null;

try {
Class.forName(jdbcDriver).newInstance();
connection = DriverManager.getConnection(dbUrl, userName, userPassword);
} catch (Exception e) {
e.printStackTrace();
}

return connection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.dh.medicine.dao.impl;

import com.dh.medicine.dao.IDao;
import com.dh.medicine.dao.SettingJDBC;
import com.dh.medicine.model.Medicine;
import org.apache.log4j.Logger;

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

public class MedicineDaoImpl implements IDao<Medicine> {
/** Attributes **/
private SettingJDBC settingJDBC;
final static Logger logger = Logger.getLogger(MedicineDaoImpl.class);

/** Constructor **/
public MedicineDaoImpl(SettingJDBC settingJDBC) {
this.settingJDBC = settingJDBC;
}

/** Methods **/
@Override
public Medicine save(Medicine medicine) {
logger.debug("Saving a new medicine: " + medicine.toString());
Connection connection = settingJDBC.connectWithDatabase();
Statement statement = null;
String query = String.format("INSERT INTO medicines (name, laboratory, quantity, price) " +
"VALUES ('%s', '%s', '%s', '%s')",
medicine.getName(), medicine.getLaboratory(), medicine.getQuantity(),
medicine.getPrice());

try {
statement = connection.createStatement();
statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
ResultSet resultSet = statement.getGeneratedKeys();
if (resultSet.next())
medicine.setId(resultSet.getInt(1));

connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}

return medicine;
}
}
Loading

0 comments on commit c0f636e

Please sign in to comment.