Example Usage
91 | *
92 | * {@code
93 | * public class Application {
94 | * public static void main(String[] args) {
95 | * try {
96 | * DataSource dataSource = DataSourceFactory.createMySQLDataSource(
97 | * MySQLCredentials.of(address, port, database, username, password)
98 | * );
99 | *
100 | * // Do something with the dataSource (...)
101 | * } catch (DatabaseConnectionException exception) {
102 | * System.out.println("DatabaseConnection Error: " + exception.getMessage();
103 | * } catch (DriverNotFoundException exception) {
104 | * System.out.println("DriverNotFound Error: " + exception.getMessage();
105 | * }
106 | * }
107 | * }}
108 | *
109 | * @param mySQLCredentials
110 | * Credentials of the MySQL Database.
111 | *
112 | * @throws DatabaseConnectionException
113 | *
114 | * - If the credentials is wrong.
115 | *
116 | *
117 | * @throws DatabaseDriverNotFoundException
118 | *
119 | * - If the MySQL driver was not found.
120 | *
121 | *
122 | * @return A datasource with MySQL connection.
123 | *
124 | * */
125 | public static DataSource createMySQLDataSource(@NotNull MySQLCredentials mySQLCredentials) throws DatabaseConnectionException, DatabaseDriverNotFoundException {
126 | return new MySQL(mySQLCredentials);
127 | }
128 |
129 | /**
130 | * Create a DataSource with PostgreSQL Url.
131 | *
132 | * Example Usage
133 | *
134 | * {@code
135 | * public class Application {
136 | * public static void main(String[] args) {
137 | * try {
138 | * DataSource dataSource = DataSourceFactory.createPostgreSQLDataSource(
139 | * PostgreSQLCredentials.of(address, port, database, username, password)
140 | * );
141 | *
142 | * // Do something with the dataSource (...)
143 | * } catch (DatabaseConnectionException exception) {
144 | * System.out.println("DatabaseConnection Error: " + exception.getMessage();
145 | * } catch (DriverNotFoundException exception) {
146 | * System.out.println("DriverNotFound Error: " + exception.getMessage();
147 | * }
148 | * }
149 | * }}
150 | *
151 | * @param postgreSQLCredentials
152 | * Credentials of the PostgreSQL Database.
153 | *
154 | * @throws DatabaseConnectionException
155 | *
156 | * - If the credentials is wrong.
157 | *
158 | *
159 | * @throws DatabaseDriverNotFoundException
160 | *
161 | * - If the PostgreSQL driver was not found.
162 | *
163 | *
164 | * @return A datasource with PostgreSQL connection.
165 | *
166 | * */
167 | public static DataSource createPostgreSQLDataSource(@NotNull PostgreSQLCredentials postgreSQLCredentials) throws DatabaseDriverNotFoundException, DatabaseConnectionException {
168 | return new PostgreSQL(postgreSQLCredentials);
169 | }
170 |
171 | /**
172 | * Create a DataSource with MariaDB Url.
173 | *
174 | * Example Usage
175 | *
176 | * {@code
177 | * public class Application {
178 | * public static void main(String[] args) {
179 | * try {
180 | * DataSource dataSource = DataSourceFactory.createMariaDBDataSource(
181 | * MariaDBCredentials.of(address, port, database, username, password)
182 | * );
183 | *
184 | * // Do something with the dataSource (...)
185 | * } catch (DatabaseConnectionException exception) {
186 | * System.out.println("DatabaseConnection Error: " + exception.getMessage();
187 | * } catch (DriverNotFoundException exception) {
188 | * System.out.println("DriverNotFound Error: " + exception.getMessage();
189 | * }
190 | * }
191 | * }}
192 | *
193 | * @param mariaDBCredentials
194 | * Credentials of the MariaDB Database.
195 | *
196 | * @throws DatabaseConnectionException
197 | *
198 | * - If the credentials is wrong.
199 | *
200 | *
201 | * @throws DatabaseDriverNotFoundException
202 | *
203 | * - If the MariaDB driver was not found.
204 | *
205 | *
206 | * @return A datasource with MariaDB connection.
207 | *
208 | * */
209 | public static DataSource createMariaDBDataSource(@NotNull MariaDBCredentials mariaDBCredentials) throws DatabaseDriverNotFoundException, DatabaseConnectionException {
210 | return new MariaDB(mariaDBCredentials);
211 | }
212 |
213 | }
214 |
--------------------------------------------------------------------------------
/sql/src/main/java/com/github/mattnicee7/mattlib/datasource/impl/MariaDB.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of MattLib, licensed under the MIT License.
3 | *
4 | * Copyright (c) mattnicee7
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.github.mattnicee7.mattlib.datasource.impl;
26 |
27 | import com.github.mattnicee7.mattlib.credentials.impl.MariaDBCredentials;
28 | import com.github.mattnicee7.mattlib.datasource.DataSource;
29 | import com.github.mattnicee7.mattlib.exception.DatabaseConnectionException;
30 | import com.github.mattnicee7.mattlib.exception.DatabaseDriverNotFoundException;
31 | import org.jetbrains.annotations.NotNull;
32 |
33 | import java.sql.Connection;
34 | import java.sql.DriverManager;
35 | import java.sql.SQLException;
36 |
37 | public class MariaDB implements DataSource {
38 |
39 | private final Connection connection;
40 |
41 | public MariaDB(@NotNull MariaDBCredentials mariaDBCredentials) throws DatabaseDriverNotFoundException, DatabaseConnectionException {
42 | try {
43 | Class.forName("org.mariadb.jdbc.Driver");
44 |
45 | this.connection = DriverManager.getConnection(
46 | mariaDBCredentials.getUrl(),
47 | mariaDBCredentials.getUsername(),
48 | mariaDBCredentials.getPassword());
49 | } catch (ClassNotFoundException exception) {
50 | throw new DatabaseDriverNotFoundException("MariaDB Driver not found.");
51 | } catch (SQLException exception) {
52 | throw new DatabaseConnectionException("Failed to connect with MariaDB.");
53 | }
54 | }
55 |
56 | @Override
57 | public Connection getConnection() throws SQLException {
58 | return connection;
59 | }
60 |
61 | @Override
62 | public void closeConnection() {
63 | try {
64 | if (!connection.isClosed())
65 | connection.close();
66 | } catch (SQLException exception) {
67 | exception.printStackTrace();
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/sql/src/main/java/com/github/mattnicee7/mattlib/datasource/impl/MySQL.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of MattLib, licensed under the MIT License.
3 | *
4 | * Copyright (c) mattnicee7
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.github.mattnicee7.mattlib.datasource.impl;
26 |
27 | import com.github.mattnicee7.mattlib.credentials.impl.MySQLCredentials;
28 | import com.github.mattnicee7.mattlib.datasource.DataSource;
29 | import com.github.mattnicee7.mattlib.exception.DatabaseConnectionException;
30 | import com.github.mattnicee7.mattlib.exception.DatabaseDriverNotFoundException;
31 | import org.jetbrains.annotations.NotNull;
32 |
33 | import java.sql.Connection;
34 | import java.sql.DriverManager;
35 | import java.sql.SQLException;
36 |
37 | /**
38 | * Class responsible for storing the connection with MySQL. You can close the connection whenever you want.
39 | */
40 | public class MySQL implements DataSource {
41 |
42 | private final Connection connection;
43 |
44 | public MySQL(@NotNull MySQLCredentials mySQLCredentials) throws DatabaseConnectionException, DatabaseDriverNotFoundException {
45 | try {
46 | Class.forName("com.mysql.jdbc.Driver");
47 |
48 | this.connection = DriverManager.getConnection(
49 | mySQLCredentials.getUrl(),
50 | mySQLCredentials.getUsername(),
51 | mySQLCredentials.getPassword()
52 | );
53 |
54 | } catch (ClassNotFoundException exception) {
55 | throw new DatabaseDriverNotFoundException("MySQL Driver not found.");
56 | } catch (SQLException exception) {
57 | throw new DatabaseConnectionException("Failed to connect with MySQL.");
58 | }
59 | }
60 |
61 | @Override
62 | public Connection getConnection() {
63 | return connection;
64 | }
65 |
66 | @Override
67 | public void closeConnection() {
68 | try {
69 | if (!connection.isClosed())
70 | connection.close();
71 | } catch (SQLException exception) {
72 | exception.printStackTrace();
73 | }
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/sql/src/main/java/com/github/mattnicee7/mattlib/datasource/impl/PostgreSQL.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of MattLib, licensed under the MIT License.
3 | *
4 | * Copyright (c) mattnicee7
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.github.mattnicee7.mattlib.datasource.impl;
26 |
27 | import com.github.mattnicee7.mattlib.credentials.impl.PostgreSQLCredentials;
28 | import com.github.mattnicee7.mattlib.datasource.DataSource;
29 | import com.github.mattnicee7.mattlib.exception.DatabaseConnectionException;
30 | import com.github.mattnicee7.mattlib.exception.DatabaseDriverNotFoundException;
31 | import org.jetbrains.annotations.NotNull;
32 |
33 | import java.sql.Connection;
34 | import java.sql.DriverManager;
35 | import java.sql.SQLException;
36 |
37 | public class PostgreSQL implements DataSource {
38 |
39 | private final Connection connection;
40 |
41 | public PostgreSQL(@NotNull PostgreSQLCredentials postgreSQLCredentials) throws DatabaseDriverNotFoundException, DatabaseConnectionException {
42 | try {
43 | Class.forName("org.postgresql.Driver");
44 |
45 | this.connection = DriverManager.getConnection(
46 | postgreSQLCredentials.getUrl(),
47 | postgreSQLCredentials.getUsername(),
48 | postgreSQLCredentials.getPassword()
49 | );
50 |
51 | } catch (ClassNotFoundException exception) {
52 | throw new DatabaseDriverNotFoundException("PostgreSQL Driver not found.");
53 | } catch (SQLException exception) {
54 | throw new DatabaseConnectionException("Failed to connect with PostgreSQL.");
55 | }
56 | }
57 |
58 | @Override
59 | public Connection getConnection() {
60 | return connection;
61 | }
62 |
63 | @Override
64 | public void closeConnection() {
65 | try {
66 | if (!connection.isClosed())
67 | connection.close();
68 | } catch (SQLException exception) {
69 | exception.printStackTrace();
70 | }
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/sql/src/main/java/com/github/mattnicee7/mattlib/datasource/impl/SQLite.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of MattLib, licensed under the MIT License.
3 | *
4 | * Copyright (c) mattnicee7
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.github.mattnicee7.mattlib.datasource.impl;
26 |
27 | import com.github.mattnicee7.mattlib.credentials.impl.SQLiteCredentials;
28 | import com.github.mattnicee7.mattlib.datasource.DataSource;
29 | import com.github.mattnicee7.mattlib.exception.DatabaseDriverNotFoundException;
30 | import org.jetbrains.annotations.NotNull;
31 |
32 | import java.sql.Connection;
33 | import java.sql.DriverManager;
34 | import java.sql.SQLException;
35 |
36 | /**
37 | * Class responsible for storing the connection with SQLite.
38 | */
39 | public class SQLite implements DataSource {
40 |
41 | private final String url;
42 |
43 | public SQLite(@NotNull SQLiteCredentials sqLiteCredentials) throws DatabaseDriverNotFoundException {
44 | try {
45 | this.url = sqLiteCredentials.getUrl();
46 |
47 | Class.forName("org.sqlite.JDBC");
48 | } catch (ClassNotFoundException exception) {
49 | throw new DatabaseDriverNotFoundException("SQLite driver not found.");
50 | }
51 |
52 | }
53 |
54 | @Override
55 | public Connection getConnection() throws SQLException {
56 | return DriverManager.getConnection(url);
57 | }
58 |
59 | @Override
60 | public void closeConnection() {
61 | // Do nothing because SQLite doesn't need to be closed.
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/sql/src/main/java/com/github/mattnicee7/mattlib/exception/DatabaseConnectionException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of MattLib, licensed under the MIT License.
3 | *
4 | * Copyright (c) mattnicee7
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.github.mattnicee7.mattlib.exception;
26 |
27 | import java.sql.SQLException;
28 |
29 | /**
30 | * Custom exception used when something in the attempt to connect to the database goes wrong, such as wrong credentials, etc.
31 | */
32 | public class DatabaseConnectionException extends SQLException {
33 |
34 | public DatabaseConnectionException(String message) {
35 | super(message);
36 | }
37 |
38 | public DatabaseConnectionException(String message, Throwable cause) {
39 | super(message, cause);
40 | }
41 |
42 | public DatabaseConnectionException(Throwable cause) {
43 | super(cause);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/sql/src/main/java/com/github/mattnicee7/mattlib/exception/DatabaseDriverNotFoundException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of MattLib, licensed under the MIT License.
3 | *
4 | * Copyright (c) mattnicee7
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.github.mattnicee7.mattlib.exception;
26 |
27 | import java.sql.SQLException;
28 |
29 | /**
30 | * Custom exception used when some database driver is not found. Probably an error in your project
31 | */
32 | public class DatabaseDriverNotFoundException extends SQLException {
33 |
34 | public DatabaseDriverNotFoundException(String message) {
35 | super(message);
36 | }
37 |
38 | public DatabaseDriverNotFoundException(String message, Throwable cause) {
39 | super(message, cause);
40 | }
41 |
42 | public DatabaseDriverNotFoundException(Throwable cause) {
43 | super(cause);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/sql/src/main/java/com/github/mattnicee7/mattlib/util/PreparedStatementBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of MattLib, licensed under the MIT License.
3 | *
4 | * Copyright (c) mattnicee7
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.github.mattnicee7.mattlib.util;
26 |
27 | import lombok.val;
28 | import org.jetbrains.annotations.NotNull;
29 |
30 | import java.sql.Connection;
31 | import java.sql.PreparedStatement;
32 | import java.sql.SQLException;
33 | import java.util.HashMap;
34 | import java.util.Map;
35 |
36 | /**
37 | * The PreparedStatementBuilder class is a utility class that allows you to build a prepared statement in steps
38 | * Credits: chicoferreira, thanks so much chico.
39 | */
40 | public class PreparedStatementBuilder {
41 |
42 | private final String query;
43 | private final Map parameters;
44 |
45 | public PreparedStatementBuilder(@NotNull String query) {
46 | this.query = query;
47 | this.parameters = new HashMap<>();
48 | }
49 |
50 | public static PreparedStatementBuilder of(@NotNull String query) {
51 | return new PreparedStatementBuilder(query);
52 | }
53 |
54 | /**
55 | * This function adds a parameter to the prepared statement
56 | *
57 | * @param index The index of the parameter in the SQL statement.
58 | * @param value The value to bind to the parameter.
59 | * @return Nothing.
60 | */
61 | public PreparedStatementBuilder parameter(int index, @NotNull Object value) {
62 | this.parameters.put(index, value);
63 | return this;
64 | }
65 |
66 | /**
67 | * Build a prepared statement from the query and parameters
68 | *
69 | * @param connection The connection to the database.
70 | * @return PreparedStatement
71 | */
72 | public PreparedStatement build(@NotNull Connection connection) throws SQLException {
73 | val preparedStatement = connection.prepareStatement(this.query);
74 |
75 | for (Map.Entry entry : parameters.entrySet())
76 | preparedStatement.setObject(entry.getKey(), entry.getValue());
77 |
78 | return preparedStatement;
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------