DBF java 操作 dbf foxpro 例子

public Map<String, Object> getResultSet(
String sql) {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
DataSource dataSource = jdbcTemplate.getDataSource();
connection = dataSource.getConnection();
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

rs = statement.executeQuery(sql);
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();

String[] strutName = new String[columnCount]; // 列名称
byte[] strutType = new byte[columnCount]; // 列类型
int[] structLength = new int[columnCount];// 列长度

rs.last();// the last row in this ResultSet object
int itemCount = rs.getRow();

rs.beforeFirst();// just before the first row
Object[][] data = new Object[itemCount][columnCount];

for (int i = 1; i <= columnCount; i++) {
strutType[i - 1] = DBFField.FIELD_TYPE_C; // string类型
strutName[i - 1] = meta.getColumnName(i); // 列名
}

int i = 0;

while (rs.next()) {

for (int j = 0; j < columnCount; j++) {
Object da = null;
try {
if (rs.getObject(j + 1) != null) {

da = new String(rs.getObject(j + 1).toString().getBytes("GBK"), "ISO-8859-1");

}
if (da == null) {
structLength[j] = 1;
} else {
structLength[j] = ((String) da).length();
}

} catch (UnsupportedEncodingException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
throw new DataAccessFailureException(ErrorCodes.ERRORCODES_SYSTEM_ERROR.getStrVlue());
}
data[i][j] = da;
}

i++;
}

Map<String, Object> map = new HashMap<String, Object>();
map.put("strutTypeArray", strutType);
map.put("strutNameArray", strutName);
// 导出DBF时,名字和性别长度分别为20和4
if (strutName.length > 2) {
if (structLength.length > 1 && strutName[1].equals("NAME")) {
structLength[1] = 20;
}
if (structLength.length > 2 && strutName[2].equals("XB")) {
structLength[2] = 4;
}

if (structLength.length > 12 && strutName[12].equals("KCBH")) {
structLength[12] = 5;
}

if (structLength.length > 13 && strutName[13].equals("ZWH")) {
structLength[13] = 5;
}

if (structLength.length > 14 && strutName[14].equals("KH")) {
structLength[14] = 20;
}

if (structLength.length > 15 && strutName[15].equals("XLDM")) {
structLength[15] = 5;
}

if (structLength.length > 16 && strutName[16].equals("SXZY")) {
structLength[16] = 30;
}

if (structLength.length > 17 && strutName[17].equals("WJHQK")) {
structLength[17] = 5;
}

if (structLength.length > 18 && strutName[18].equals("SS")) {
structLength[18] = 5;
}

if (structLength.length > 19 && strutName[19].equals("KMDM")) {
structLength[19] = 5;
}

if (structLength.length > 5 && strutName[5].equals("ZYDM")) {
structLength[5] = 10;
}

if (structLength.length > 6 && strutName[6].equals("BKDW")) {
structLength[6] = 100;
}

if (structLength.length > 7 && strutName[7].equals("ZWDM")) {
structLength[7] = 10;
}

if (structLength.length > 8 && strutName[8].equals("JBDM")) {
structLength[8] = 10;
}

if (structLength.length > 9 && strutName[9].equals("KD")) {
structLength[9] = 10;
}

if (structLength.length > 10 && strutName[10].equals("BMD")) {
structLength[10] = 5;
}

if (structLength.length > 11 && strutName[11].equals("KQ")) {
structLength[11] = 5;
}

}
map.put("structLengthArray", structLength);
map.put("data", data);
return map;
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
throw new DataAccessFailureException(ErrorCodes.ERRORCODES_SYSTEM_ERROR.getStrVlue());
} finally {
try {
if (rs != null) {
rs.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
throw new DataAccessFailureException(ErrorCodes.ERRORCODES_SYSTEM_ERROR.getStrVlue());
}
}

你可能感兴趣的:(sql)