1
|
|
|
/* |
2
|
|
|
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender.database; |
17
|
|
|
|
18
|
|
|
import java.sql.Connection; |
19
|
|
|
import static java.sql.DriverManager.getConnection; |
20
|
|
|
|
21
|
|
|
import com.strider.datadefender.DataDefenderException; |
22
|
|
|
import com.strider.datadefender.DbConfig; |
23
|
|
|
import org.apache.commons.lang3.StringUtils; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @author sdi |
28
|
|
|
*/ |
29
|
|
|
public class MsSqlDbConnection extends DbConnection { |
30
|
|
|
|
31
|
|
|
public MsSqlDbConnection(DbConfig config) throws DataDefenderException { |
32
|
|
|
super(config); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Establishes database connection |
37
|
|
|
* |
38
|
|
|
* @return Connection |
39
|
|
|
* @throws DatabaseException |
40
|
|
|
*/ |
41
|
|
|
@Override |
42
|
|
|
public Connection connect() throws DatabaseException { |
43
|
|
|
return doConnect(() -> getConnection(this.getUrl())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get connection URL. |
48
|
|
|
* @return String |
49
|
|
|
*/ |
50
|
|
|
protected String getUrl() { |
51
|
|
|
StringBuilder sqlServerUrl = new StringBuilder(config.getUrl()); |
52
|
|
|
if (!StringUtils.isBlank(config.getUsername())) { |
53
|
|
|
sqlServerUrl.append(";user=").append(config.getUsername()); |
54
|
|
|
} |
55
|
|
|
if (!StringUtils.isBlank(config.getPassword())) { |
56
|
|
|
sqlServerUrl.append(";password=").append(config.getPassword()); |
57
|
|
|
} |
58
|
|
|
return sqlServerUrl.toString(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|