-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataFetcherFacadeImpl.java
70 lines (68 loc) · 2.27 KB
/
DataFetcherFacadeImpl.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
package facade;
import java.sql.*;
/**
* Created by Beka on 16.04.17.
*/
public class DataFetcherFacadeImpl implements DataFetcherFacade {
@Override
public int getImportantValue() throws DataFetchException {
Connection conn = null;
PreparedStatement prep = null;
CallableStatement call = null;
ResultSet rset = null;
try {
Class.forName("<driver>").newInstance();
conn = DriverManager.getConnection("<database>");
String sql = "SELECT * FROM <table> WHERE <column name> =?";
prep = conn.prepareStatement(sql);
prep.setString(1, "<column value>");
rset = prep.executeQuery();
if (rset.next()) {
System.out.println(rset.getString("<column name"));
}
sql = "{call <stored procedure>( ?, ? )}";
call = conn.prepareCall(sql);
call.setInt(1, 1972);
call.registerOutParameter(2, Types.INTEGER);
call.execute();
return call.getInt(2);
} catch (SQLException e) {
throw new DataFetchException(e);
} catch (InstantiationException e) {
throw new DataFetchException(e);
} catch (ClassNotFoundException e) {
throw new DataFetchException(e);
} catch (IllegalAccessException e) {
throw new DataFetchException(e);
} finally {
if (rset != null) {
try {
rset.close();
} catch (SQLException ex) {
throw new DataFetchException(ex);
}
}
if (prep != null) {
try {
prep.close();
} catch (SQLException ex) {
throw new DataFetchException(ex);
}
}
if (call != null) {
try {
call.close();
} catch (SQLException ex) {
throw new DataFetchException(ex);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
throw new DataFetchException(ex);
}
}
}
}
}