1
1
package com .balinski .api_project .database ;
2
2
3
- import com .balinski .api_project .util .SqlExceptionPrinter ;
4
3
import org .apache .commons .dbcp2 .BasicDataSource ;
5
4
5
+ import java .io .IOException ;
6
6
import java .sql .*;
7
7
import java .util .*;
8
8
import java .util .stream .Stream ;
9
9
10
10
public class DatabaseProxy {
11
- private BasicDataSource dataSource ;
12
- private Connection connection ;
13
- private String url ;
14
- private String username ;
15
- private String password ;
11
+ protected static BasicDataSource dataSource ;
12
+ protected static Connection connection ;
13
+ protected static String driver ;
14
+ protected static String url ;
15
+ protected static String username ;
16
+ protected static String password ;
16
17
17
- public DatabaseProxy (Properties props ) {
18
- try {
19
- loadDatabaseProperties (props );
20
- initDataSource ();
21
- } catch (Exception e ) {
22
- System .err .println ("Cannot initialize database data source: " + e .getMessage ());
18
+ static {
19
+ loadDatabaseProperties ();
20
+ loadDriver ();
21
+ testConnection ();
22
+ initDataSource ();
23
+ }
24
+
25
+ private DatabaseProxy (){};
26
+
27
+ public static List <Map <String , Object >> querySelect (String sql ) throws DatabaseException {
28
+ List <Map <String , Object >> data ;
29
+
30
+ try (Connection connection = getConnection ()) {
31
+ try (Statement statement = connection .createStatement ()) {
32
+ try (ResultSet rs = statement .executeQuery (sql )) {
33
+ ResultSetMetaData md = rs .getMetaData ();
34
+ int columns = md .getColumnCount ();
35
+ data = new LinkedList <>();
36
+
37
+ while (rs .next ()) {
38
+ Map <String , Object > row = new HashMap <>(columns );
39
+
40
+ for (int i = 1 ; i <= columns ; i ++)
41
+ row .put (md .getColumnName (i ), rs .getObject (i ));
42
+
43
+ data .add (row );
44
+ }
45
+ }
46
+ }
47
+ } catch (SQLException e ) {
48
+ throw new DatabaseException ("An error occurred when trying to query the database." , e );
49
+ } finally {
50
+ closeConnection ();
51
+ }
52
+
53
+ return data ;
54
+ }
55
+
56
+ public static int queryUpdate (String sql , boolean transaction ) throws DatabaseException {
57
+ int rowsAffected = 0 ;
58
+
59
+ try (Connection connection = getConnection ()) {
60
+ if (transaction )
61
+ connection .setAutoCommit (false );
62
+
63
+ try (Statement statement = connection .createStatement ()) {
64
+ rowsAffected = statement .executeUpdate (sql );
65
+ }
66
+
67
+ if (transaction ) {
68
+ connection .setAutoCommit (true );
69
+ connection .commit ();
70
+ }
71
+ } catch (SQLException e ) {
72
+ e .printStackTrace ();
73
+ throw new DatabaseException ("An error occurred when trying to update the database." , e );
74
+ } finally {
75
+ closeConnection ();
23
76
}
77
+
78
+ return rowsAffected ;
24
79
}
25
80
26
- public Connection getConnection () {
81
+ protected static Connection getConnection () throws DatabaseException {
27
82
try {
28
- this . connection = dataSource .getConnection ();
83
+ connection = dataSource .getConnection ();
29
84
} catch (SQLException e ) {
30
- SqlExceptionPrinter . print ("Could not obtain an instance of connection from given data source." , e );
85
+ throw new DatabaseException ("Could not obtain an instance of connection from given data source." , e );
31
86
}
32
87
33
88
return connection ;
34
89
}
35
90
36
- public void closeConnection () {
91
+ protected static void closeConnection () throws DatabaseException {
37
92
if (connection == null )
38
93
return ;
39
94
40
95
try {
41
- this .connection .close ();
96
+ connection .close ();
97
+ } catch (SQLException e ) {
98
+ throw new DatabaseException ("Cannot close the connection." , e );
99
+ }
100
+ }
101
+
102
+ private static void testConnection () throws DatabaseException {
103
+ if (Stream .of (url , driver , username , password ).anyMatch (Objects ::isNull )) {
104
+ throw new DatabaseException ("One of the database properties (url, driver, username, password) is missing. " +
105
+ "Check the file containing database properties." );
106
+ }
107
+
108
+ try (Connection ignored = DriverManager .getConnection (url , username , password )) {
109
+ System .out .println ("The database connection was configured successfully." );
42
110
} catch (SQLException e ) {
43
- SqlExceptionPrinter . print ( "Cannot close the connection ." , e );
111
+ throw new DatabaseException ( "Wrong credentials or internal database error ." , e );
44
112
}
45
113
}
46
114
47
- private void initDataSource () {
115
+ protected static void initDataSource () {
48
116
dataSource = new BasicDataSource ();
49
117
dataSource .setUrl (url );
50
118
dataSource .setUsername (username );
@@ -54,20 +122,26 @@ private void initDataSource() {
54
122
dataSource .setMaxOpenPreparedStatements (100 );
55
123
}
56
124
57
- private void loadDatabaseProperties (Properties props ) throws NullPointerException , ClassNotFoundException {
125
+ protected static void loadDatabaseProperties () throws DatabaseException {
126
+ Properties props ;
127
+ try {
128
+ props = FilePropertiesLoader .load ("server/src/main/resources/database.properties" );
129
+ } catch (IOException e ) {
130
+ throw new DatabaseException ("Could not find database properties file under given path: "
131
+ + "server/src/main/resources/database.properties" , e );
132
+ }
133
+
58
134
url = props .getProperty ("url" );
59
- String driver = props .getProperty ("driver" );
135
+ driver = props .getProperty ("driver" );
60
136
username = props .getProperty ("username" );
61
137
password = props .getProperty ("password" );
138
+ }
62
139
63
- if (Stream .of (url , driver , username , password ).anyMatch (Objects ::isNull )) {
64
- throw new NullPointerException ("One or more of properties (url, driver, username, password) is missing" );
65
- }
66
-
140
+ private static void loadDriver () throws DatabaseException {
67
141
try {
68
142
Class .forName (driver );
69
143
} catch (ClassNotFoundException e ) {
70
- throw new ClassNotFoundException ("Driver class " + driver + " not found." , e );
144
+ throw new DatabaseException ("Driver class " + driver + " not found." , e );
71
145
}
72
146
}
73
147
}
0 commit comments