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



我正在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)
    {
    }
 }

尝试用try and 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"));
           }

}捕捉(异常e) {}

最新更新