DaoImpl.java
5.4 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.airport.dao.impl;
import com.airport.bean.MessageBak;
import com.airport.dao.Dao;
import com.airport.util.ConfigUtils;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import oracle.sql.CLOB;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
public class DaoImpl implements Dao {
private static final Logger logger = Logger.getLogger(DaoImpl.class);
private JdbcTemplate jdbcTemplate;
public DaoImpl() {
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int insertRecord(final String oper, final String sndr) {
KeyHolder keyHolder = new GeneratedKeyHolder();
this.jdbcTemplate.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
DaoImpl.logger.info("oper=" + oper + " sndr=" + sndr);
String sql = "insert into MESSAGE_BAK(OPER,SNDR,SNTM,content) values(?,?,sysdate,empty_clob())";
PreparedStatement ps = connection.prepareStatement(sql, new String[]{"FID"});
ps.setString(1, oper);
ps.setString(2, sndr);
return ps;
}
}, keyHolder);
int generatedId = keyHolder.getKey().intValue();
return generatedId;
}
public void saveRecord(String messaegType, String sndr, String xmlContent) throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
int fid = -100;
try {
fid = this.insertRecord(messaegType, sndr);
conn = this.jdbcTemplate.getDataSource().getConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
String SQL = "select CONTENT from MESSAGE_BAK where FID=" + fid + " and SNDR='CFPS' for update";
rs = stmt.executeQuery(SQL);
while(rs.next()) {
CLOB clob = (CLOB)rs.getClob(1);
Writer os = clob.getCharacterOutputStream();
os.write(xmlContent);
os.flush();
os.close();
}
conn.commit();
} catch (SQLException var14) {
if (fid > 0) {
this.delete(fid);
}
var14.printStackTrace();
conn.rollback();
} finally {
if (conn != null) {
conn.setAutoCommit(true);
}
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
public void delete(int fid) {
this.jdbcTemplate.update("delete from MESSAGE_BAK where fid=" + fid);
}
public int getMaxFID() {
return this.jdbcTemplate.queryForInt("select max(FID) from MESSAGE_BAK where SNDR='CFPS'");
}
public List<MessageBak> getRecordByFID(int fid) {
String SQL_FMT = "select FID,CONTENT from MESSAGE_BAK where (FID>%s and FID<%s+%s) and (SNDR='CFPS') order by FID";
String sql = String.format(SQL_FMT, fid, fid, ConfigUtils.RECORD_COUNT);
logger.info(sql);
final LobHandler lobHandler = new DefaultLobHandler();
final ArrayList xmlList = new ArrayList();
try {
this.jdbcTemplate.query(sql, new AbstractLobStreamingResultSetExtractor() {
protected void streamData(ResultSet rs) throws SQLException, IOException, DataAccessException {
String content = "";
while(rs.next()) {
int fid = rs.getInt(1);
content = lobHandler.getClobAsString(rs, "CONTENT");
if (content != null) {
MessageBak obj = new MessageBak(fid, content);
xmlList.add(obj);
} else {
DaoImpl.logger.error("FID=" + fid + " content is NULL");
}
}
}
});
} catch (EmptyResultDataAccessException var7) {
var7.printStackTrace();
}
return xmlList;
}
public int getRecordCount() {
return this.jdbcTemplate.queryForInt("select count(*) from MESSAGE_BAK where SNDR='CFPS' ");
}
public void update(int fid, int times) {
String sql = String.format("update MESSAGE_BAK set OUTFLAG=%s ,OUTTM=sysdate where FID=%s", times, fid);
logger.info("sql=" + sql);
this.jdbcTemplate.update(sql);
}
}