从文本区域的url商店下载内容

时间:2014-02-17 07:09:59

标签: java swing joptionpane

我正在尝试从网址下载内容并将其插入我的数据库。我已经下载了它,但它在文本区域中显示在一行中。我希望它在文本区域中显示为段落(必要时包裹)。这是我尝试过的:

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      
}

1 个答案:

答案 0 :(得分: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));
        }
    }