This is a simple Spring Boot application that allows you to execute dynamic SQL queries against a database using the javax.persistence.EntityManager API. The application exposes a REST endpoint that accepts a query parameter containing a SQL SELECT statement and returns the result set data as a list of JSON objects with named columns.
- Java 8 or higher
- Spring Boot 2.5.0 or higher
- H2 Database or any other relational database
- Clone this repository to your local machine.
- Open the project in your IDE.
- Run the application using mvn spring-boot:run or by running the DynamicQueryApplication class.
- Once the application is running, you can access the H2 Console by visiting
http://localhost:8080/h2-console
. Enterjdbc:h2:mem:dcbapp
as the JDBC URL and clickconnect
. - You can then execute SQL queries using the executeDynamicQuery endpoint. The endpoint URL is http://localhost:8080/?query=. Replace with your desired SQL query.
To execute a dynamic SQL query, make a GET request to the executeDynamicQuery endpoint with your desired SQL query as a parameter. For example, to retrieve all records from the employee table, you can make the following request:
GET http://localhost:8080/?query=SELECT * FROM employee
The response will be a JSON object containing the result set data. Here's an example response:
[
{
"ID": 1,
"EMAIL": "[email protected]",
"SALARY": 10000.00,
"DEPARTMENT_ID": 1
},
{
"ID": 2,
"EMAIL": "[email protected]",
"SALARY": 12000.00,
"DEPARTMENT_ID": 1
},
{
"ID": 3,
"EMAIL": "[email protected]",
"SALARY": 8000.00,
"DEPARTMENT_ID": 2
}
]
GET http://localhost:8080/?query=SELECT * FROM employee JOIN department ON employee.department_id = department.id
[
{
"ID": 1,
"EMAIL": "[email protected]",
"SALARY": 10000,
"DEPARTMENT_ID": 1,
"NAME": "Engineering"
},
{
"ID": 1,
"EMAIL": "[email protected]",
"SALARY": 12000,
"DEPARTMENT_ID": 1,
"NAME": "Engineering"
},
{
"ID": 2,
"EMAIL": "[email protected]",
"SALARY": 8000,
"DEPARTMENT_ID": 2,
"NAME": "Sales"
}
]
This application is designed for demonstration and testing purposes only and should not be used in production environments. It does not provide any security or access control mechanisms and assumes that the database connection is trusted.
If you find any issues with the project or want to contribute to its development, feel free to submit a pull request or open an issue.
This project is licensed under the MIT License. See the LICENSE file for details.