private void readData() {
Connection connection = null;
Statement stm = null;
ResultSet rs = null;
try {
// 连接数据源
connection = dataSource.getConnection();
// 获得Statement对象
stm = connection.createStatement();
// 返回ResultSet结果集
rs = stm.executeQuery(sql);
// 获得ResultSetMetaData对象
ResultSetMetaData rsmd = rs.getMetaData();
// 获得返回此 ResultSet 对象中的列数
int count = rsmd.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i < count; i++) {
// 获取指定列的表目录名称
String label = rsmd.getColumnLabel(i);//--关键代码,用来读取数据列字段
if(label.equals("IP")){
label="IP";//修正IP字段
}else if(label.equals("LOGIN_IP")){
label="loginIP";//修正登录IP字段
}else{
label=AgFileUtil.underlineToCamel(label);
}
// 以 Object 的形式获取此 ResultSet 对象的当前行中指定列的值
Object object = rs.getObject(i);//--关键代码,用来读取数值
// 把数据库中的字段名和值对应为一个map对象中的一个键值对
map.put(label, object);
}
try {
AgTrTransferAcounts agTrTransferAcounts = new AgTrTransferAcounts();
// 把map中的数据赋值给bean
BeanUtils.populate(agTrTransferAcounts, map);//--关键代码,用来赋值
// 将读取的数据压入写队列
AgConstant.TRQUEUE.put(agTrTransferAcounts);
// 写文件状态(进行中)
syncTime = agTrTransferAcounts.getSyncTime().toString();
content = syncTime + "#" + "0";
}
}
} catch (Exception ex) {
//异常时记录数据没有同步完成
AgFileUtil.rewriteFile(file, content);
ex.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Connection connection = null;
Statement stm = null;
ResultSet rs = null;
try {
// 连接数据源
connection = dataSource.getConnection();
// 获得Statement对象
stm = connection.createStatement();
// 返回ResultSet结果集
rs = stm.executeQuery(sql);
// 获得ResultSetMetaData对象
ResultSetMetaData rsmd = rs.getMetaData();
// 获得返回此 ResultSet 对象中的列数
int count = rsmd.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i < count; i++) {
// 获取指定列的表目录名称
String label = rsmd.getColumnLabel(i);//--关键代码,用来读取数据列字段
if(label.equals("IP")){
label="IP";//修正IP字段
}else if(label.equals("LOGIN_IP")){
label="loginIP";//修正登录IP字段
}else{
label=AgFileUtil.underlineToCamel(label);
}
// 以 Object 的形式获取此 ResultSet 对象的当前行中指定列的值
Object object = rs.getObject(i);//--关键代码,用来读取数值
// 把数据库中的字段名和值对应为一个map对象中的一个键值对
map.put(label, object);
}
try {
AgTrTransferAcounts agTrTransferAcounts = new AgTrTransferAcounts();
// 把map中的数据赋值给bean
BeanUtils.populate(agTrTransferAcounts, map);//--关键代码,用来赋值
// 将读取的数据压入写队列
AgConstant.TRQUEUE.put(agTrTransferAcounts);
// 写文件状态(进行中)
syncTime = agTrTransferAcounts.getSyncTime().toString();
content = syncTime + "#" + "0";
} catch (IllegalAccessException e) {
//异常时记录数据没有同步完成
AgFileUtil.rewriteFile(file, content);
} catch (InvocationTargetException e) {
//异常时记录数据没有同步完成
AgFileUtil.rewriteFile(file, content);
}
}
} catch (Exception ex) {
//异常时记录数据没有同步完成
AgFileUtil.rewriteFile(file, content);
ex.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}