try {
BufferedReader br = new BufferedReader(new InputStreamReader(item.getInputStream()));
String strLine = "";
StringTokenizer st = null;
while ((strLine = br.readLine()) != null) {
st = new StringTokenizer(strLine, "\t");
while (st.hasMoreTokens()) {
urlcnt = st.nextToken();
srccnt = st.nextToken();
contentType = st.nextToken();
verticle = st.nextToken();
timeFrame = st.nextToken();
}
if (con == null) {
SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
con = SQLConnection.getNewConnection();
stmt = con.createStatement();
}
try {
ResultSet rs;
boolean hasRows = false;
rs = stmt.executeQuery("select url from urls_temp where url='"+urlcnt+"'");
while (rs.next()) {
hasRows=true;
i++;
}
if (!hasRows) {
j++;
PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_temp(url, source_name, is_active, is_periodic, Link_Type, New_Entry, verticle, periodic_timeframe) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
if (timeFrame.equalsIgnoreCase("Daily")) {
insertUrlStatement.setString(1, urlcnt);
insertUrlStatement.setString(2, srccnt);
insertUrlStatement.setInt(3, 1);
insertUrlStatement.setInt(4, 0);
insertUrlStatement.setString(5, contentType);
insertUrlStatement.setInt(6, 1);
insertUrlStatement.setString(7, verticle);
insertUrlStatement.setString(8, timeFrame);
insertUrlStatement.executeUpdate();
insertUrlStatement.close();
} else {
insertUrlStatement.setString(1, urlcnt);
insertUrlStatement.setString(2, srccnt);
insertUrlStatement.setInt(3, 1);
insertUrlStatement.setInt(4, 1);
insertUrlStatement.setString(5, contentType);
insertUrlStatement.setInt(6, 1);
insertUrlStatement.setString(7, verticle);
insertUrlStatement.setString(8, timeFrame);
insertUrlStatement.executeUpdate();
}
}
}
}
}
上面的代碼用於將詳細信息上傳到數據庫,並在CSV文件中給出選項卡。 CSV文件的從CSV文件讀取
樣本格式將作如下安排,並能正常工作:
http://avb.com(tab space)asdf(tab space)asdf(tab space)asdd(tab space)asdf
http://anything.com(tab space)asdf(tab space)asdf(tab space)asdfasd(tab space)asdfsadf
有時候我會需要一些null
值被插入到從CSV文件作爲數據庫如下:
http://asdf.com(tab space)(tab space)aasddf(tab space)(tab space)asdfsad
但是這不起作用,沒有任何東西插入到數據庫中。
什麼修改必須完成上述程序爲表中的第二和第四(srccnt
& verticle
)列插入null
值?
您的代碼不完整。 'try'語句的'catch()'和/或'finally'子句在哪裏?如果你忽略了異常,那麼這就是開始的地方......打印棧跟蹤。 – 2010-11-16 04:29:24
「但這不起作用..沒有任何東西插入到數據庫中。」 ...告訴我你正在遇到某種形式的錯誤。其次,如果您在\ t中使用逗號,但\ t之後的空格爲「http://asdf.com(製表符空格)(製表符空格)aasddf(製表符空格)」(製表符空格)asdfsad「... this會返回一個「空格」標記嗎?如果你不想要空格,那麼在標記之前你應該把它們除掉。像strLine.replaceAll(「\\ w +」,「」)應該做的伎倆。也可以使用split()代替tokenizer。 – CoolBeans 2010-11-16 05:02:52