如何从数据库中检索数据并在netbeans中显示在JLabel上

时间:2014-05-20 00:55:20

标签: java netbeans

我正在使用Netbeans进行项目,并希望在从JLabel选择员工时显示JComboBox上的员工详细信息。

我试过......但对画面没有影响。

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
   try {                  

                   Class.forName("com.mysql.jdbc.Driver");                
                   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tnpcb","root","");
                   String sql="select curr_no,receive_date from tapal where user_id='"+jComboBox1.getSelectedItem()+"'";


                   pst=con.prepareStatement(sql);

                   rs=pst.executeQuery();

                   while(rs.next())
                   {
                       System.out.println("hi22");
                       jLabel1.setText(rs.getString("curr_no"));
                       jLabel1.setText(rs.getString("receive_date"));
                   }
    }
    catch(ClassNotFoundException | SQLException e)
    {

    }
 }

1 个答案:

答案 0 :(得分:0)

尝试使用try和catch

包围你的代码

尝试{   PST = con.prepareStatement(SQL);

           rs=pst.executeQuery();

           while(rs.next())
           {
               System.out.println("hi22");
           jLabel1.setText(rs.getString("curr_no"));
           jLabel1.setText(rs.getString("receive_date"));
           }

} catch(例外e){}