Back to Articles
Java Programming7 min readOctober 21, 2026

Building a Desktop Application with JavaFX and SQL Server

Explain how a Java desktop application connects its interface, controllers, database layer, and SQL Server to manage student information.

Desktop apps are still a great way to learn how software is structured. In this post I explain how a **JavaFX** application can talk to **SQL Server** to manage student information. ## The layers A clean project separates responsibilities: | Layer | Job | |---|---| | **View (FXML)** | Describes the screens: tables, forms, buttons | | **Controller** | Handles clicks and input, calls the data layer | | **DAO / Repository** | Runs SQL queries | | **Database** | Stores the data in SQL Server | Keeping SQL out of controllers makes the code easier to test and change. ## Connecting to SQL Server Add the Microsoft JDBC driver to your project, then open a connection:

java
String url = "jdbc:sqlserver://localhost:1433;databaseName=SchoolDB;encrypt=true;trustServerCertificate=true";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
    // use the connection
}

Read the username and password from a config file or environment variable, never from source code. ## A simple DAO

java
public List<Student> findAll() throws SQLException {
    String sql = "SELECT id, name, email FROM students ORDER BY name";
    List<Student> list = new ArrayList<>();
    try (Connection conn = Database.getConnection();
         PreparedStatement ps = conn.prepareStatement(sql);
         ResultSet rs = ps.executeQuery()) {
        while (rs.next()) {
            list.add(new Student(rs.getInt("id"), rs.getString("name"), rs.getString("email")));
        }
    }
    return list;
}

Wiring it to the interface The controller calls the DAO and puts the results into a `TableView` using an `ObservableList`. When the user adds or deletes a student, the controller calls the DAO, then refreshes the list. ## Lessons learned - Always use `PreparedStatement` for queries that include user input. - Close resources with try-with-resources. - Run database work off the UI thread (for example with `Task`) so the window never freezes. With these habits, a small student manager can grow into a well-organized application.

Share this article
Tags:#Java#JavaFX#SQL Server#JDBC#Intermediate