Skip to content

Commit dd80922

Browse files
committed
Initial commit
0 parents  commit dd80922

File tree

10 files changed

+561
-0
lines changed

10 files changed

+561
-0
lines changed

nb-configuration.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<netbeans.hint.jdkPlatform>JDK_13</netbeans.hint.jdkPlatform>
17+
</properties>
18+
</project-shared-configuration>

pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.stackabuse.postgresql</groupId>
5+
<artifactId>java-postgresql-sample</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>java-postgresql-sample</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<maven.compiler.target>13</maven.compiler.target>
13+
<maven.compiler.source>13</maven.compiler.source>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.13-rc-2</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
25+
<dependency>
26+
<groupId>org.postgresql</groupId>
27+
<artifactId>postgresql</artifactId>
28+
<version>42.2.9</version>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<finalName>java-postgresql-sample</finalName>
34+
<plugins>
35+
<plugin>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<version>3.8.1</version>
38+
</plugin>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-dependency-plugin</artifactId>
42+
<version>3.1.1</version>
43+
<executions>
44+
<execution>
45+
<id>copy-dependencies</id>
46+
<phase>prepare-package</phase>
47+
<goals>
48+
<goal>copy-dependencies</goal>
49+
</goals>
50+
<configuration>
51+
<outputDirectory>
52+
${project.build.directory}/libs
53+
</outputDirectory>
54+
</configuration>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-jar-plugin</artifactId>
61+
<version>3.2.0</version>
62+
<configuration>
63+
<archive>
64+
<manifest>
65+
<addClasspath>true</addClasspath>
66+
<classpathPrefix>libs/</classpathPrefix>
67+
<mainClass>com.stackabuse.postgresql.CustomerApplication</mainClass>
68+
</manifest>
69+
</archive>
70+
</configuration>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.stackabuse.postgresql;
2+
3+
import com.stackabuse.postgresql.api.Customer;
4+
import com.stackabuse.postgresql.api.NonExistentEntityException;
5+
import com.stackabuse.postgresql.core.NonExistentCustomerException;
6+
import com.stackabuse.postgresql.core.PostgreSqlDao;
7+
import com.stackabuse.postgresql.spi.Dao;
8+
import java.util.Collection;
9+
import java.util.Optional;
10+
import java.util.logging.Level;
11+
import java.util.logging.Logger;
12+
13+
/**
14+
*
15+
* @author Hiram K. <https://github.com/IdelsTak>
16+
*/
17+
public class CustomerApplication {
18+
19+
private static final Logger LOGGER = Logger.getLogger(CustomerApplication.class.getName());
20+
private static final Dao<Customer, Integer> CUSTOMER_DAO = new PostgreSqlDao();
21+
22+
public static void main(String[] args) {
23+
//Test whether an exception is thrown when
24+
//the database is queried for a non-existent customer
25+
//But, if the customer does exist, the details will be printed
26+
//on the console
27+
try {
28+
Customer customer = getCustomer(1);
29+
} catch (NonExistentEntityException ex) {
30+
LOGGER.log(Level.WARNING, ex.getMessage());
31+
}
32+
//Test whether a customer can be added to the database
33+
Customer firstCustomer = new Customer("Manuel", "Kelley", "[email protected]");
34+
Customer secondCustomer = new Customer("Joshua", "Daulton", "[email protected]");
35+
Customer thirdCustomer = new Customer("April", "Ellis", "[email protected]");
36+
addCustomer(firstCustomer).ifPresent(firstCustomer::setId);
37+
addCustomer(secondCustomer).ifPresent(secondCustomer::setId);
38+
addCustomer(thirdCustomer).ifPresent(thirdCustomer::setId);
39+
//Test whether the new customer's details can be edited
40+
firstCustomer.setFirstName("Franklin");
41+
firstCustomer.setLastName("Hudson");
42+
firstCustomer.setEmail("[email protected]");
43+
updateCustomer(firstCustomer);
44+
//Test whether all customers can be read from database
45+
getAllCustomers().forEach(System.out::println);
46+
//Test whether a customer can be deleted
47+
deleteCustomer(secondCustomer);
48+
}
49+
50+
public static Customer getCustomer(int id) throws NonExistentEntityException {
51+
Optional<Customer> customer = CUSTOMER_DAO.get(id);
52+
return customer.orElseThrow(NonExistentCustomerException::new);
53+
}
54+
55+
public static Collection<Customer> getAllCustomers() {
56+
return CUSTOMER_DAO.getAll();
57+
}
58+
59+
public static void updateCustomer(Customer customer) {
60+
CUSTOMER_DAO.update(customer);
61+
}
62+
63+
public static Optional<Integer> addCustomer(Customer customer) {
64+
return CUSTOMER_DAO.save(customer);
65+
}
66+
67+
public static void deleteCustomer(Customer customer) {
68+
CUSTOMER_DAO.delete(customer);
69+
}
70+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.stackabuse.postgresql.api;
7+
8+
/**
9+
*
10+
* @author Hiram K. <https://github.com/IdelsTak>
11+
*/
12+
public class Customer {
13+
14+
private Integer id;
15+
private String firstName;
16+
private String lastName;
17+
private String email;
18+
19+
public Customer(String firstName, String lastName) {
20+
this(firstName, lastName, null);
21+
}
22+
23+
public Customer(String firstName, String lastName, String email) {
24+
this(null, firstName, lastName, email);
25+
}
26+
27+
public Customer(Integer id, String firstName, String lastName, String email) {
28+
this.id = id;
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
this.email = email;
32+
}
33+
34+
public int getId() {
35+
return id;
36+
}
37+
38+
public void setId(int id) {
39+
this.id = id;
40+
}
41+
42+
public String getFirstName() {
43+
return firstName;
44+
}
45+
46+
public void setFirstName(String firstName) {
47+
this.firstName = firstName;
48+
}
49+
50+
public String getLastName() {
51+
return lastName;
52+
}
53+
54+
public void setLastName(String lastName) {
55+
this.lastName = lastName;
56+
}
57+
58+
public String getEmail() {
59+
return email;
60+
}
61+
62+
public void setEmail(String email) {
63+
this.email = email;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return "Customer["
69+
+ "id=" + id
70+
+ ", firstName=" + firstName
71+
+ ", lastName=" + lastName
72+
+ ", email=" + email
73+
+ ']';
74+
}
75+
76+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.stackabuse.postgresql.api;
7+
8+
/**
9+
*
10+
* @author Hiram K. <https://github.com/IdelsTak>
11+
*/
12+
public class NonExistentEntityException extends Throwable {
13+
14+
private static final long serialVersionUID = -3760558819369784286L;
15+
16+
public NonExistentEntityException(String message) {
17+
super(message);
18+
}
19+
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.stackabuse.postgresql.core;
7+
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.SQLException;
11+
import java.util.Optional;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
14+
15+
/**
16+
*
17+
* @author Hiram K. <https://github.com/IdelsTak>
18+
*/
19+
public class JdbcConnection {
20+
21+
private static final Logger LOGGER = Logger.getLogger(JdbcConnection.class.getName());
22+
private static Optional<Connection> connection = Optional.empty();
23+
24+
public static Optional<Connection> getConnection() {
25+
if (connection.isEmpty()) {
26+
String url = "jdbc:postgresql://localhost:5432/sampledb";
27+
String user = "postgres";
28+
String password = "aqua1-Was";
29+
30+
try {
31+
connection = Optional.ofNullable(DriverManager.getConnection(url, user, password));
32+
} catch (SQLException ex) {
33+
LOGGER.log(Level.SEVERE, null, ex);
34+
}
35+
}
36+
37+
return connection;
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.stackabuse.postgresql.core;
7+
8+
import com.stackabuse.postgresql.api.NonExistentEntityException;
9+
10+
11+
public class NonExistentCustomerException extends NonExistentEntityException {
12+
13+
private static final long serialVersionUID = 8633588908169766368L;
14+
15+
public NonExistentCustomerException() {
16+
super("Customer does not exist");
17+
}
18+
19+
}

0 commit comments

Comments
 (0)