2010-11-16 141 views
0
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值?

+1

您的代碼不完整。 'try'語句的'catch()'和/或'finally'子句在哪裏?如果你忽略了異常,那麼這就是開始的地方......打印棧跟蹤。 – 2010-11-16 04:29:24

+0

「但這不起作用..沒有任何東西插入到數據庫中。」 ...告訴我你正在遇到某種形式的錯誤。其次,如果您在\ t中使用逗號,但\ t之後的空格爲「http://asdf.com(製表符空格)(製表符空格)aasddf(製表符空格)」(製表符空格)asdfsad「... this會返回一個「空格」標記嗎?如果你不想要空格,那麼在標記之前你應該把它們除掉。像strLine.replaceAll(「\\ w +」,「」)應該做的伎倆。也可以使用split()代替tokenizer。 – CoolBeans 2010-11-16 05:02:52

回答

1

StringTokenizer將連續的分隔符視爲單個分隔符。你說輸入包含[tab space]作爲分隔符,但其餘的代碼似乎並沒有期待空格,所以如果沒有更多的信息,我會猜測輸入是用製表符分隔的(不是[tab空格]),並且相鄰的分隔符被忽略,最後的nextToken()會拋出一個你忽略的異常。

的這裏的答案是這樣使用split()重寫,如Javadoc

的StringTokenizer推薦是一個傳統類 保持兼容性的原因 雖然它的使用是在新 代碼氣餒。建議任何尋求此功能的 都使用String的 拆分方法或代替使用java.util.regex包的 。

也就是說,你應該看看任何現有的CSV庫(Google for Java CSV)。

0

我會建議您調試您的代碼,以更具體地瞭解它的行爲不像您期望的那樣,然後在必要時發佈更具體的問題。

0

我使用FileHelpers解析csv文件。 http://www.filehelpers.com/。它讀取一個csv文件,並在對象中給我一個輸出,這是我的願望。而且如果我傳遞對象列表,它會爲我創建一個csv文件。給它一個機會。