avatarSamuel Catalano

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5121

Abstract

ationType.IDENTITY)</span> <span class="hljs-keyword">private</span> Long id;

<span class="hljs-meta">@Column(name = "name")</span>
<span class="hljs-keyword">private</span> String name;

<span class="hljs-meta">@Column(name = "age")</span>
<span class="hljs-keyword">private</span> <span class="hljs-type">int</span> age;

<span class="hljs-keyword">public</span> Long <span class="hljs-title function_">getId</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> id;
}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setId</span><span class="hljs-params">(Long id)</span> {
    <span class="hljs-built_in">this</span>.id = id;
}

<span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> name;
}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> {
    <span class="hljs-built_in">this</span>.name = name;
}

<span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getAge</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> age;
}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setAge</span><span class="hljs-params">(<span class="hljs-type">int</span> age)</span> {
    <span class="hljs-built_in">this</span>.age = age;
}

}</pre></div><p id="78a4">This code defines a data entity called Person with three properties: id, name, and age. The <b>@Entity</b> annotation marks the class as a data entity and the <b>@Table</b> annotation specifies the database table name.</p><h1 id="afc6">Step 6: Creating a Repository</h1><p id="f897">To access the database, we need to create a repository. Create a new Java interface in the src/main/java directory with the following code:</p><div id="455a"><pre><span class="hljs-keyword">package</span> com.example;

<span class="hljs-keyword">import</span> io.quarkus.hibernate.orm.panache.PanacheRepository;

<span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">PersonRepository</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">PanacheRepository</span><Person> {}</pre></div><p id="37ea">This code defines a repository interface called PersonRepository that extends the PanacheRepository interface from the Panache ORM library.</p><h1 id="c941">Step 7: Implementing the REST Endpoint</h1><p id="7f31">Now that we have set up the database integration, we can implement the REST endpoint. Open the GreetingResource.java file located in the src/main/java directory and replace the code with the following:</p><div id="21aa"><pre><span class="hljs-keyword">package</span> com.example;

<span class="hljs-keyword">import</span> javax.inject.Inject; <span class="hljs-keyword">import</span> javax.ws.rs.*; <span class="hljs-keyword">import</span> javax.ws.rs.core.MediaType; <span class="hljs-keyword">import</span> java.util.List;

<span class="hljs-meta">@Path("/persons")</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonResource</span> {

<span class="hljs-meta">@Inject</span> PersonRepository repository;

<span class="hljs-meta">@GET</span> <span class="hljs-meta">@Produces(MediaType.APPLICATION_JSON)</span> <span class="hljs-keyword">public</span> List<Person> <span class="hljs-title function_">list</span><span class="hljs-params">()</span> { <span class="hljs-keyword">return</span> repository.listAll(); }

<span class="hljs-meta">@POST</span> <span class="hljs-meta">@Consumes(MediaType.APPLICATION_JSON)</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">add</span><span class="hljs-params">(Person person)</span> { repository.persist(person); } }</pre></div><p id="8402">This code defines a new REST endpoint at the /persons URL path. The endpoint has two methods: list() and add(). The list() method returns a list of all persons in the database, and the add() method adds a new person to the database.</p><h1 id="8b1b">Step 8: Testing the Application</h1><p id="cc65">To test the application, run the following command:</p><div id="3d80"><pre>mvn <span class="hljs-built_in">compile</span> quarkus:dev</pre></div><p id="3a12">This command compiles the application and starts a development mode with live reloading. Open a web browser and go to the <a href="http://localhost:8080/persons">http://localhost:8080/persons</a> URL. You should see an empty JSON array. Use a tool like a curl or Postman to add a new person to the database:</p><div id="a4fa"><pre>curl -X POST
-H

Options

<span class="hljs-string">"Content-Type: application/json"</span>
-d '{<span class="hljs-string">"name"</span>:<span class="hljs-string">"John Doe"</span>,<span class="hljs-string">"age"</span>:<span class="hljs-number">30</span>}'
http:<span class="hljs-comment">//localhost:8080/persons</span></pre></div><p id="c1ac">Refresh the web browser, and you should see the newly added person in the JSON array.</p><h1 id="bb0c">Conclusion</h1><p id="1ae9">In this tutorial, we learned how to create a simple Quarkus project with database integration. We added support for the PostgreSQL database and the Panache ORM library. We created a data entity, a repository, and a REST endpoint. Finally, we tested the application and verified that it worked as expected. Quarkus is a powerful framework that can help you build cloud-native applications quickly and efficiently.</p><div id="6174" class="link-block"> <a href="https://readmedium.com/rethrowing-an-exception-in-java-exception-handling-759f9c286a53"> <div> <div> <h2>Rethrowing An Exception In Java | Exception Handling</h2> <div><h3>To propagate an exception, the catch block can choose to rethrow the exception by using the throw statement.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*7tF5oPUP4Jv6jIuMudM-ag.png)"></div> </div> </div> </a> </div><div id="bfc6" class="link-block"> <a href="https://readmedium.com/how-to-use-try-with-resource-in-java-9c0b4ae48d21"> <div> <div> <h2>How To Use Try With Resource In Java</h2> <div><h3>The Java try with resources construct, is an exception handling mechanism that can automatically close resources like a…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*7tF5oPUP4Jv6jIuMudM-ag.png)"></div> </div> </div> </a> </div><div id="88ce" class="link-block"> <a href="https://readmedium.com/try-catch-finally-in-java-exception-handling-2ed273814cd8"> <div> <div> <h2>Try-Catch-Finally In Java | Exception Handling</h2> <div><h3>you can use try-catch-block to handle all kind of exceptions, in this article we will learn how to use this approch in…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*7tF5oPUP4Jv6jIuMudM-ag.png)"></div> </div> </div> </a> </div><div id="dd4d" class="link-block"> <a href="https://readmedium.com/getting-started-with-spring-boot-a-comprehensive-tutorial-for-beginners-78d1ffe65716"> <div> <div> <h2>Getting Started with Spring Boot: A Comprehensive Tutorial for Beginners</h2> <div><h3>Spring Boot is an open-source framework that allows developers to build production-ready, scalable, and easy-to-use…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Zx96SjGXxl4Q9yPbgjiV9A.png)"></div> </div> </div> </a> </div><div id="fa0a" class="link-block"> <a href="https://readmedium.com/guide-to-object-mapping-with-modelmapper-in-java-1dd8f517faf2"> <div> <div> <h2>Guide to Object Mapping with ModelMapper in Java</h2> <div><h3>Object mapping is a process of transforming data from one object to another. In Java, it can be a tedious task that…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*WvwtKW7a5nbj0MHH.png)"></div> </div> </div> </a> </div><div id="1106" class="link-block"> <a href="https://readmedium.com/most-commonly-asked-java8-stream-based-interview-question-part-2-bbe5bdd0129f"> <div> <div> <h2>Most Commonly asked Java8 Stream based Interview Question — Part 2</h2> <div><h3>This part is continuation to the already uploaded Part 1 of Most commonly asked Java 8 Stream Based Interview Question.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*An7__LNG-7Jr9cs-1zECMg.jpeg)"></div> </div> </div> </a> </div></article></body>

How to Create a Simple Quarkus Project with Database Integration

Quarkus is a modern Java framework that is designed for building cloud-native applications. It offers fast startup times, low memory usage, and great developer productivity. In this tutorial, we will learn how to create a simple Quarkus project with database integration.

Step 1: Setting up the Development Environment

Before we start, we need to set up the development environment. You will need the following software installed:

  • Java JDK 11 or later
  • Apache Maven 3.6.2 or later

Step 2: Creating a New Quarkus Project

To create a new Quarkus project, open a command prompt or terminal window and navigate to the directory where you want to create the project. Then, run the following command:

mvn io.quarkus:quarkus-maven-plugin:2.3.2.Final:create \
    -DprojectGroupId=com.example \
    -DprojectArtifactId=quarkus-demo \
    -DclassName="com.example.GreetingResource" \
    -Dpath="/hello"

This command creates a new Quarkus project with a sample REST endpoint.

Step 3: Adding Database Integration

To add database integration, we need to add the following dependencies to the pom.xml file:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>

These dependencies add support for the PostgreSQL database and the Panache ORM library.

Step 4: Configuring the Database Connection

Next, we need to configure the database connection. Open the application.properties file located in the src/main/resources directory and add the following lines:

quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/quarkus_demo
quarkus.datasource.username=postgres
quarkus.datasource.password=secret

Step 5: Creating a Data Entity

To store data in the database, we need to create a data entity. Create a new Java class in the src/main/java directory with the following code:

package com.example;

import javax.persistence.*;

@Entity
@Table(name = "persons")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;

    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 getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

This code defines a data entity called Person with three properties: id, name, and age. The @Entity annotation marks the class as a data entity and the @Table annotation specifies the database table name.

Step 6: Creating a Repository

To access the database, we need to create a repository. Create a new Java interface in the src/main/java directory with the following code:

package com.example;

import io.quarkus.hibernate.orm.panache.PanacheRepository;

public interface PersonRepository extends PanacheRepository<Person> {}

This code defines a repository interface called PersonRepository that extends the PanacheRepository interface from the Panache ORM library.

Step 7: Implementing the REST Endpoint

Now that we have set up the database integration, we can implement the REST endpoint. Open the GreetingResource.java file located in the src/main/java directory and replace the code with the following:

package com.example;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/persons")
public class PersonResource {

  @Inject
  PersonRepository repository;

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public List<Person> list() {
    return repository.listAll();
  }

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public void add(Person person) {
    repository.persist(person);
  }
}

This code defines a new REST endpoint at the /persons URL path. The endpoint has two methods: list() and add(). The list() method returns a list of all persons in the database, and the add() method adds a new person to the database.

Step 8: Testing the Application

To test the application, run the following command:

mvn compile quarkus:dev

This command compiles the application and starts a development mode with live reloading. Open a web browser and go to the http://localhost:8080/persons URL. You should see an empty JSON array. Use a tool like a curl or Postman to add a new person to the database:

curl -X POST \
     -H "Content-Type: application/json" \
     -d '{"name":"John Doe","age":30}' \
     http://localhost:8080/persons

Refresh the web browser, and you should see the newly added person in the JSON array.

Conclusion

In this tutorial, we learned how to create a simple Quarkus project with database integration. We added support for the PostgreSQL database and the Panache ORM library. We created a data entity, a repository, and a REST endpoint. Finally, we tested the application and verified that it worked as expected. Quarkus is a powerful framework that can help you build cloud-native applications quickly and efficiently.

Java
Quarkus
Database
Panache
Postgres
Recommended from ReadMedium