我可以从数据库中输入jLabel文本吗?

时间:2015-09-28 16:06:57

标签: java database netbeans jlabel

我需要一些小工程的帮助,我必须做的工作。在这个项目中,我需要从数据库中获取一个特定的单元格,查询必须看起来像这样select * from app_1 where id_user_main=?

jLabel4 = new javax.swing.JLabel();
jLabel4.setText(null);
{
    String sql = "select * from info where id_user_main=?";  
    try {
        conn = MySQLConnect.ConnectDb();
        pst = conn.prepareStatement(sql);
        pst.setInt(1, id);
        rs = pst.executeQuery();

        if (rs.next()) { 
            jLabel4.setText(rs.getString("info"));
        }
    } catch(Exception e) {

    }   
}

1 个答案:

答案 0 :(得分:2)

简单地将问题分成两个子问题:

  • 从数据库中检索字符串
  • 将字符串设置为标签

举个例子:

if (isset($_FILES['fileToUpload']) &&
  $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) {

  //check MIME TYPE
  $finfo = new finfo(FILEINFO_MIME_TYPE);
  if (false === $ext = array_search(
    $finfo->file($_FILES['fileToUpload']['tmp_name']),
    array(
        'jpg' => 'image/jpeg',
        'png' => 'image/png',
        'gif' => 'image/gif',
    ),
    true
  )) {
      throw new RuntimeException('Invalid file format.');
  }
  $destination = sprintf('./uploads/%s.%s',
       sha1_file($_FILES['fileToUpload']['tmp_name']),
       $ext
  );

  //move the file to a temp folder
  if (!move_uploaded_file(
    $_FILES['fileToUpload']['tmp_name'],
    $destination
  )) {
    throw new RuntimeException('Failed to move uploaded file.');
  }

  //Attach file
  $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'],
                     basename($destination));
  //delete the file
  unlink($destination);
}