我有这个代码的问题..我想从平面文件中提取数据并将其存储到数据库中。平面文件格式如下: -
DT|00000001|TMDWH|UNIFI|00380520160|MAH SIEW YIN|11 |JALAN PP 2/8|TAMAN PUTRA PRIMA|PUCHONG|SELANGOR|47100|MALAYSIA|801110-14-5498||||||VOBB||A||11|JALAN PP 2/8|||TAMAN PUTRA PRIMA
DT|00000002|TMDWH|UNIFI|00322012091|JUNITA BINTI JAMAL|6 10 KONDOMINIUM FAJARIA|JALAN PANTAI BARU|KUALA LUMPUR|KUALA LUMPUR|WILAYAH PERSEKUTUAN|59200|MALAYSIA|800129-09-5078||||||VOBB||A|||JALAN PANTAI BARU|6|KONDOMINIUM FAJARIA|KUALA LUMPUR
代码:
public void massageData(String tmp) {
String RecordType = "";
String RecordNumber = "";
String sourceSystemId = "";
String targetSystemId = "";
String TelNo = "";
String Name = "";
String Addr1 = "";
String Addr2 = "";
String Addr3 = "";
String TownCity = "";
String State = "";
String PostalCd = "";
String Country = "";
String NewICNo = "";
String OldICNo = "";
String PassportNo = "";
String BRN = "";
String Latitude = "";
String Longitude = "";
String ServiceType = "";
String IndicatorType = "";
//add
String CreateDate = "";
String Filler = "";
String CRNL = "";
String HouseNo = "";
String LotNo = "";
String StreetName = "";
String AptNo = "";
String BuildingName = "";
//add
String LowID = "";
String HighID = "";
String SectionName = "";
tmp = tmp.replace("\""," "); // remove " with blank
tmp = tmp.replace("\'","\'\'");
String[] recArray = tmp.split("\\|");
RecordType = recArray[1].trim();
RecordNumber = recArray[2].trim();
sourceSystemId = recArray[3].trim();
targetSystemId = recArray[4].trim();
TelNo = recArray[5].trim();
Name = recArray[6].trim();
Addr1 = recArray[7].trim();
Addr2 = recArray[8].trim();
Addr3 = recArray[9].trim();
TownCity = recArray[10].trim();
State = recArray[11].trim();
PostalCd = recArray[12].trim();
Country = recArray[13].trim();
NewICNo = recArray[14].trim();
OldICNo = recArray[15].trim();
PassportNo = recArray[16].trim();
BRN = recArray[17].trim();
Latitude = recArray[18].trim();
Longitude = recArray[19].trim();
ServiceType = recArray[20].trim();
IndicatorType = recArray[21].trim();
//add
CreateDate = recArray[22].trim();
Filler = recArray[23].trim();
CRNL = recArray[24].trim();
//
HouseNo = recArray[25].trim();
LotNo = recArray[26].trim();
StreetName = recArray[27].trim();
AptNo = recArray[28].trim();
BuildingName = recArray[29].trim();
//add
LowID = recArray[30].trim();
HighID = recArray[31].trim();
//
SectionName = recArray[32].trim();
Connection conn = null;
ResultSet rs = null;
PreparedStatement stmt = null;
logger.info("masuk messageData");
// get actual telephone number
String actualMSISDN = parseMSISDN(TelNo);
String [] aNo = getAreaCode(actualMSISDN).split("\\|");
String iCtr = getiCtr(actualMSISDN);
iCtr = recArray[0].trim();
String stateCode = lookupStateCode(State);
String sQuery = "insert into DATA_999 (ID,RecordType,RecordNumber,SourceSystemApplicationId,TargetApplicationId,TelNo,Name,HouseNo,StreetName,AppartmentSuite,TownCity,State,PostalCode,Country,NewIC,OldIC,PassportNo,BRN,LatitudeDecimal,LongitudeDecimal,ServiceType,IndicatorType,CreateDate,Filler,Cr_Nl,HouseNo_New,LotNo_New,StreetName_New,AptNo_New,BuildingName_New,LowIDRange,HighIDRange,SectionName) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try {
conn = ds.getConnection();
// insert post process data to data_999 table
logger.info("start Query");
stmt = conn.prepareStatement(sQuery);
stmt.setString(0,iCtr);
stmt.setString(1,RecordType);
stmt.setString(2,RecordNumber);
stmt.setString(3,sourceSystemId);
stmt.setString(4,targetSystemId);
stmt.setString(5,TelNo);
stmt.setString(6,Name);
stmt.setString(7,Addr1);
stmt.setString(8,Addr2);
stmt.setString(9,Addr3);
stmt.setString(10,TownCity);
stmt.setString(11,State);
stmt.setString(12,PostalCd);
stmt.setString(13,Country);
stmt.setString(14,NewICNo);
stmt.setString(15,OldICNo);
stmt.setString(16,PassportNo);
stmt.setString(17,BRN);
stmt.setString(18,Latitude);
stmt.setString(19,Longitude);
stmt.setString(20,ServiceType);
stmt.setString(21,IndicatorType);
//add
stmt.setString(22,CreateDate);
stmt.setString(23,Filler);
stmt.setString(24,CRNL);
//
stmt.setString(25,HouseNo);
stmt.setString(26,LotNo);
stmt.setString(27,StreetName);
stmt.setString(28,AptNo);
stmt.setString(29,BuildingName);
//add
stmt.setString(30,LowID);
stmt.setString(31,HighID);
//
stmt.setString(32,SectionName);
//stmt = conn.prepareStatement(sQuery);
int dbStat = stmt.executeUpdate();
conn.close();
} catch (SQLException s){
logger.error(s.getMessage());
}
finally {
try {if (stmt != null) stmt.close();} catch (SQLException e) {}
try {if (conn != null) conn.close();} catch (SQLException e) {}
}
我真的希望这里有人可以帮助我。
目前的结果:
没有数据存储到数据库中,代码已成功编译!
预期结果
所有数据都将存储到数据库DATA_999中。
答案 0 :(得分:1)
与我认为哪个是zero-based的其他Java API不同,SQL API是基于一的 - 意味着它从一开始计数。您的代码正在尝试设置第0个字段,该字段应该爆炸。
作为旁注,因为每个字段都有任何特殊处理,所以只需迭代字段并设置stmt参数就可以用几行代替所有代码 - 即不要为每个字段使用变量:
// fyi, the regex of this split trims automatically
String[] fields = tmp.replace("\""," ").replace("\'","\'\'").trim().split("\\s*\\|\\s*");
// Do any special field processing (most need none)
field[0] = getiCtr(parseMSISDN(field[5])); // for example - just do what you need
// Now set all the SQL params
int col = 0;
for (String field : fields) {
stmt.setString(++col, field); // Note: SQL API is 1-based (not zero-based)
}
答案 1 :(得分:0)
预准备报表的索引是从1开始的:
将stmt.setString(0,iCtr);
更改为stmt.setString(1,iCtr);
。 (并调整以下内容)
请发布您获得的例外情况。它会给我们提供更多可能出错的提示
编辑:
表格varchar
中的所有字段都是?您的行中有值可以建模为int
s。