summaryrefslogtreecommitdiff
path: root/src/database/ConnectionBuilder.java
blob: 74e5c338e25cc5456cf5b340254bef07fa0f8426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package database;

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

/**
 * Builder for connection.
 *
 * @author Peter Wu
 */
public class ConnectionBuilder {

    private String dbms;
    private String serverName;
    private int port;
    private String dbName;
    private String username;
    private String password;

    /**
     * Sets up a ConnectionBuilder for PostgreSQL and serverName localhost. The
     * username, password and database name must still be supplied.
     */
    public ConnectionBuilder() {
        dbms = "postgresql";
        serverName = "localhost";
        port = 5432;
    }

    public ConnectionBuilder setDbms(String dbms) {
        this.dbms = dbms;
        return this;
    }

    public ConnectionBuilder setServerName(String serverName) {
        this.serverName = serverName;
        return this;
    }

    public ConnectionBuilder setPort(int port) {
        this.port = port;
        return this;
    }

    public ConnectionBuilder setDbName(String dbName) {
        this.dbName = dbName;
        return this;
    }

    public ConnectionBuilder setUsername(String username) {
        this.username = username;
        return this;
    }

    public ConnectionBuilder setPassword(String password) {
        this.password = password;
        return this;
    }

    public Connection create() throws SQLException {
        String url = "jdbc:" + dbms + "://" + serverName + ":" + port + "/" + dbName;
        return DriverManager.getConnection(url, username, password);
    }
}