M
M
Michael2016-09-07 18:11:07
Java
Michael, 2016-09-07 18:11:07

How to attach a Database to your GitHub project?

Hello, I have a small Swing project using JDBC. And I have a database on my computer. So I want to put this project on GitHub, because then I will give a link to this project in my resume. I have a question - a person who wants to check the application will open my project, but will knock out a million errors, since he will not have this database on his computer. What to do in this case? Just attach the DB dump file to the project?? And then he will need to import this dump on his computer or what?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evhen, 2016-09-07
@M-Misha-M

If the database is not large, then you can use an embedded (imbedded) database, such as H2 .
The base will rise along with your application.
All you need is to connect the dependencies of the JDBC driver

<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.192</version>
        </dependency>

And specify the URL with the required parameters
Simple example
import java.sql.*;

public class H2Main {

    static {
        try {
            Class.forName("org.h2.Driver");
        } catch (ClassNotFoundException e) {
            System.err.println("Error load H2 JDBC driver: " + e.getMessage());
        }
    }


    public static void main(String[] args) {

        try (Connection con = DriverManager.getConnection("jdbc:h2:./h2example", "sa", "")) {

            createSimpleDBSchema(con);

            System.out.printf("Message from H2: %s\n", getMessage(con));


        } catch (Exception e) {
            System.err.printf("%s: %s\n", e.getClass().getSimpleName(), e.getMessage());
        }


    }


    static void createSimpleDBSchema(Connection con) {
        try (Statement stmt = con.createStatement()) {

            stmt.executeUpdate("CREATE TABLE HelloH2 (message varchar(255) NULL);");

            stmt.executeUpdate("INSERT INTO HelloH2 (message) VALUES ('Hello World!')");

        } catch (SQLException e) {
            System.err.printf("%s: %s\n", e.getClass().getSimpleName(), e.getMessage());
        }
    }


    static String getMessage(Connection con) throws SQLException {

        try (Statement stmt = con.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT message FROM HelloH2")) {

            if (rs.next())
                return rs.getString("message");
            else
                return null;

        }
    }

}

A
Anton, 2016-09-07
Reytarovsky @Antonchik

Use migrations.

F
Fat Lorrie, 2016-09-07
@Free_ze

In the simplest case, you can just put the CREATE script next to it (for example, in the Database folder) . That's enough for a portfolio. For an actively "living project" with users, which implies some kind of patches and new versions, it would also be nice to put migration scripts nearby (which are updated in the same commit, where it matters), which would help users update the database structure in a couple of clicks without damage to their data. These scripts should be based on the initial state of the database (updated incrementally).

E
evnuh, 2016-09-07
@evnuh

Use SQLite and store the database file in a repository

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question