1 |
package dk.thoerup.webconfig; |
2 |
|
3 |
import java.io.Closeable; |
4 |
import java.io.IOException; |
5 |
import java.sql.Connection; |
6 |
import java.sql.PreparedStatement; |
7 |
import java.sql.ResultSet; |
8 |
import java.sql.SQLException; |
9 |
|
10 |
public class SQLConfigLoader extends ConfigLoader implements Closeable { |
11 |
|
12 |
PreparedStatement stmt; |
13 |
|
14 |
public SQLConfigLoader(Connection conn, String tableName) throws SQLException { |
15 |
String sql = "SELECT value FROM " + tableName + " WHERE key = ? "; |
16 |
stmt = conn.prepareStatement(sql); |
17 |
|
18 |
} |
19 |
|
20 |
@Override |
21 |
public String getValue(String paramName) { |
22 |
try { |
23 |
stmt.setString(1, paramName); |
24 |
} catch (SQLException e) { |
25 |
throw new RuntimeException("Error bindin paramter", e); |
26 |
} |
27 |
|
28 |
try (ResultSet res = stmt.executeQuery() ) { |
29 |
if (res.next() == false) { |
30 |
return null; |
31 |
} |
32 |
return res.getString(1); |
33 |
|
34 |
} catch (SQLException e) { |
35 |
throw new RuntimeException("Error executing sql", e); |
36 |
} |
37 |
|
38 |
|
39 |
} |
40 |
|
41 |
@Override |
42 |
public void close() throws IOException { |
43 |
try { |
44 |
stmt.close(); |
45 |
} catch(SQLException e) { |
46 |
throw new IOException(e); |
47 |
} |
48 |
} |
49 |
|
50 |
} |