2014-02-17 1 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

나는 당신이이 같은 원래로 파일 읽을 수 있습니다 예를 들어 패키지 NIO에서 ByteBuffer 클래스를 사용하려고 할 수 있습니다 제안 :

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)); 
     } 
    }