Skip to content

mzrghorbani/spring-boot-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot SQLite Example

A simple Spring Boot project that demonstrates how to connect to a SQLite database and display data on a web page. Intended as a beginner tutorial for students learning web development.

Prerequisites

  • Java 17 or later — Download JDK
  • Git — to clone the repository

No need to install Gradle separately. The project includes the Gradle wrapper (gradlew).

How to run

  1. Clone the repository and navigate into the project folder:

    git clone <repository-url>
    cd spring-boot-project
  2. Build the project:

    ./gradlew build -x test
  3. Run the application:

    ./gradlew bootRun
  4. Open your browser and go to http://localhost:8080

Troubleshooting

Port 8080 is already in use

If you see this error when running ./gradlew bootRun:

Web server failed to start. Port 8080 was already in use.

It means a previous instance of the application is still running. To fix it, find and stop that process:

On macOS / Linux:

fuser -k 8080/tcp

On Windows:

netstat -ano | findstr :8080
taskkill /PID <PID> /F

Replace <PID> with the number from the first column. Then run ./gradlew bootRun again.

Project structure

src/
├── main/
│   ├── java/com/example/demo/
│   │   ├── DemoApplication.java      # Entry point
│   │   ├── Student.java              # JPA entity
│   │   ├── StudentRepository.java    # Spring Data repository
│   │   └── StudentController.java    # Web controller
│   └── resources/
│       ├── application.properties    # Database configuration
│       └── templates/index.html      # Thymeleaf web page

Technologies used

  • Spring Boot 3.4.3 — application framework
  • Spring Data JPA — database access
  • SQLite — lightweight file-based database
  • Thymeleaf — server-side HTML templating

How SQLite is integrated with Spring Boot

Spring Boot normally expects databases like MySQL or PostgreSQL. Connecting to SQLite requires three small additions:

1. Dependencies (build.gradle)

implementation 'org.hibernate.orm:hibernate-community-dialects' // SQLite dialect for Hibernate
runtimeOnly 'org.xerial:sqlite-jdbc:3.45.1.0'                  // JDBC driver for SQLite

2. Configuration (application.properties)

spring.datasource.url=jdbc:sqlite:test.db               # path to the SQLite file
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect
spring.jpa.hibernate.ddl-auto=update                    # auto-creates tables on startup

3. Entity class (Student.java)

A plain Java class annotated with @Entity is all that is needed. Spring Data JPA maps it to a table in the SQLite file automatically.

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
}

When the app starts, Hibernate reads the dialect, connects to test.db (creating the file if it does not exist), and creates the student table based on the entity.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors