-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 } |
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); |
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; | ||
} | ||
} |