/
DatabaseImpl.java
125 lines (102 loc) · 3.37 KB
/
DatabaseImpl.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported
* License (the "License"). You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
*
* http://creativecommons.org/licenses/by-nc/3.0/
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
package gr.seab.r2rml.beans;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Database functions
* @author nkons
*
*/
public class DatabaseImpl implements Database {
private static final Logger log = LoggerFactory.getLogger(Database.class);
private Connection connection;
private Properties properties = new Properties();
private String propertiesFilename;
private Util util;
public DatabaseImpl() {
}
/**
*
*/
public DatabaseImpl(String propertiesFilename) {
try {
properties.load(new FileInputStream(propertiesFilename));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public Connection openConnection() {
log.info("Establishing source (relational) connection.");
if (connection == null) {
try {
String driver = properties.getProperty("db.driver");
Class.forName(driver);
String databaseType = util.findDatabaseType(driver);
String dbConnectionString = "jdbc:" + databaseType + "://" + properties.getProperty("db.host") + ":" + properties.getProperty("db.port") + "/" + properties.getProperty("db.name");
connection = DriverManager.getConnection(dbConnectionString, properties.getProperty("db.login"), properties.getProperty("db.password"));
log.info("Established source (relational) connection.");
return connection;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
} else {
return connection;
}
return null;
}
public ResultSet query(String query) {
ResultSet result = null;
try {
if (connection == null) openConnection();
if (!StringUtils.containsIgnoreCase(query, "limit") && properties.containsKey("db.limit")) {
query += " LIMIT " + properties.getProperty("db.limit");
}
//log.info(query);
//PreparedStatement preparedStatement = connection.prepareStatement(query);
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
result = statement.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
}
return result;
}
public String getPropertiesFilename() {
return propertiesFilename;
}
public void setPropertiesFilename(String propertiesFilename) {
this.propertiesFilename = propertiesFilename;
}
public Util getUtil() {
return util;
}
public void setUtil(Util util) {
this.util = util;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}