Try SQL Server on Fedora

masanori_msl

Masui Masanori

Posted on December 16, 2023

Try SQL Server on Fedora

Intro

I will try installing Microsoft SQL Server on Fedora 39 in this time.
After installing, I will try accessing it from my Spring Boot application.

Environments

  • Fedora Linux 39(Workstation Edition)
  • SQL Server 2022
  • openjdk 17.0.9 2023-10-17(Red_Hat-17.0.9.0.9-2)

Installing

According to the document, there is no SQL Server installing for Fedora.
So I will add repositories first.

sudo dnf config-manager --add-repo=https://packages.microsoft.com/config/rhel/9/mssql-server-2022.repo
sudo dnf config-manager --add-repo=https://packages.microsoft.com/config/rhel/9/prod.repo

sudo dnf check-update
sudo dnf -y install mssql-server mssql-tools
Enter fullscreen mode Exit fullscreen mode

After installing, I will execute setting up command.

sudo /opt/mssql/bin/sqlservr-setup
Enter fullscreen mode Exit fullscreen mode

Opening the port to accept access from another PC.

sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
sudo firewall-cmd --reload
Enter fullscreen mode Exit fullscreen mode

Adding "mssql-tools" path into .bashrc.

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

After installing SQL Server, I will also install DBeaver to access it by GUI tool.

sudo rpm -Uvh ./dbeaver-ce-23.3.0-stable.x86_64.rpm
Enter fullscreen mode Exit fullscreen mode
CREATE TABLE users (id bigint identity(1, 1) primary key,
    name varchar(32) not null,
    last_update_date datetimeoffset default current_timestamp AT TIME ZONE 'Tokyo Standard Time');
    go
Enter fullscreen mode Exit fullscreen mode

Access SQL Server from my Spring Boot project

To access SQL Server from my Spring Boot project, I will add Microsoft JDBC Driver for SQL Server.

build.gradle

...

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.apache.poi:poi:5.2.5'
    implementation 'org.apache.poi:poi-ooxml:5.2.5'
    implementation 'com.microsoft.sqlserver:mssql-jdbc:12.4.2.jre11'

    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
...
Enter fullscreen mode Exit fullscreen mode

UserAccessSample.java

package jp.masanori.springbootsample.users;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserAccessSample {
    public String accessSample() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost;encrypt=false;database=master;integratedSecurity=false;user=sa;password=PASSWORD";
            Connection con = DriverManager.getConnection(connectionUrl);
            String SQL = "SELECT * FROM users";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                System.out.println(
                        rs.getInt("id")
                                + ", " + rs.getString("name"));
            }
            return "OK";
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "NG";
    }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
masanori_msl
Masui Masanori

Posted on December 16, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

An E-Commerce Web Application
csharp An E-Commerce Web Application

July 13, 2024

Try SQL Server on Fedora
sqlserver Try SQL Server on Fedora

December 16, 2023