【问题标题】:Download content from url store in a text area从文本区域中的 url 存储下载内容
【发布时间】:2014-02-17 07:09:59
【问题描述】:

我正在尝试从 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      
}

【问题讨论】:

  • 你在拿什么? html页面?
  • 是的 html 页面,但内容存储在一行中,我希望它像段落一样存储。
  • 您应该更加注意代码格式并确保在 catch 子句中包含一些内容 - 至少记录日志

标签: java swing joptionpane


【解决方案1】:

我建议你可以尝试使用 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));
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多