2014-02-17 31 views
-1

我試圖從url下載內容並將其插入到我的數據庫中。我已經下載了它,但它在文本區域中顯示在一行中。我希望它在文本區域中顯示爲一個段落(如果需要,可以包裹)。這是我曾嘗試:從文本區域下載url存儲內容

String x=""; 


try { 
    conn = DriverManager.getConnection ("jdbc:mysql://localhost/mirroreddatabase", "root", ""); 
    String data=cmbGeneNames.getSelectedItem().toString(); 

    String sql="select * from omimmirrored where symbolGeneSymbol LIKE '%"+data+"%'";//change table 

    PreparedStatement pst=conn.prepareStatement(sql); 

    ResultSet rs=pst.executeQuery(); 

    //which checkboxes are checked 
    while ((rs.next())) { 
     x = rs.getString("links");  
    } 
    conn.close(); 
} 
catch(Exception e) { 
    JOptionPane.showMessageDialog(null,e); 
} 

URL u; 
InputStream is = null; 
DataInputStream dis; 
String s; 

try { 
    conn = DriverManager.getConnection ("jdbc:mysql://localhost/mirroreddatabase", "root", ""); 
    String data=cmbGeneNames.getSelectedItem().toString(); 
    u = new URL(x); 

    is = u.openStream();   

    dis = new DataInputStream(new BufferedInputStream(is)); 

    while ((s = dis.readLine())!= null) { 


    //try { 

    String sql = "update omimmirrored set sequence=? where symbolGeneSymbol LIKE '%"+data+"%'"; 
    PreparedStatement statement=conn.prepareStatement(sql); 
    statement.setString(1, s); 

    statement.executeUpdate(); 

     //which checkboxes are checked 
     //conn.close(); 
    //} 
} catch(Exception e) { 
    JOptionPane.showMessageDialog(null,e); 

} finally { 

    try { 
     is.close(); 

    } catch (IOException ioe) { 

    } 
    try { 

     conn.close(); 
    } 
    catch(SQLException sqlException) {  
    } // end of 'finally' clause  
} 
+0

你在取? html頁面? –

+0

是的HTML頁面,但內容被存儲在一行,我希望它被存儲爲一個段落。 – user2944911

+3

你應該更多地關注代碼格式,並確保在catch子句中有一些東西 - 至少是日誌記錄 –

回答

0

我建議你可以嘗試使用ByteBuffer類從包NIO例如,你可以讀取該文件作爲其原這樣的:

int amount = 0; // return The number of bytes read, 
ByteBuffer buf = ByteBuffer.allocate(1024); 
while(amount != -1){ 
    amount = source.read(buf); // source is your Input Stream, here source is object of type FileChannel 
    buf.rewind(); // Rewinds this buffer. The position is set to zero and the mark is discarded 
    if(amount != -1){ 
     for(int i = 0 ; i < amount ; i++) 
      System.out.print((char)buf.get(i)); 
     } 
    } 
相關問題