2

嗨,我已经编写了这段代码,我在这段代码中尝试的是获取方法的返回值到 java swing 标签

这是我的代码:

public static int search(java.util.Date date)
    {

         Connection conn = null;
         ResultSet rs = null;
         Statement st = null;
           int b=0;
          try
          {

          conn=DBMgr.openConnection();      
          DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
          String dateStr=formatter.format(date);  
          System.out.println("date"+dateStr);
          String sqlQuery = "select sum(time_spend) as Time_Billed_Per_Day,datetime from time_entry  where datetime like '"+dateStr+"%' ";


              st = conn.createStatement();
               rs = st.executeQuery(sqlQuery); 

               while(rs.next())
              {

                      b = rs.getInt(1);
                     System.out.println("BILL of the date u specified is:"+b);         
               }
        }
        catch(SQLException ex) 
        {
             System.out.println(ex.toString());  
        }
        finally
        {
            try
            {

                if(rs!=null)
                    rs.close();
                if(conn!=null)
                    DBMgr.closeConnection(conn);
            }
           catch(Exception ex)
           {
           }

        }
      return b;
      }

这是摆动代码:

JLabel lblTimeBilledDayText = new JLabel( "00:45:20" , JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);

我想获取方法的返回值来代替“00:45:20”

这个怎么做?

4

4 回答 4

7

尝试这个

JLabel lblTimeBilledDayText = new JLabel(String.valueOf(search(date)) , JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);
于 2013-06-10T08:55:34.260 回答
5

如果 b 是返回的 int 值,那么

JLabel lblTimeBilledDayText = new JLabel(new String(Integer.toString(b)), JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);
于 2013-06-10T08:56:50.210 回答
5
int searchResult = search( date);
// convert to String. You might want to opt for a method 
// which offers more formatting options
String searchResultAsString = Integer.toString( searchResult );

JLabel lblTimeBilledDayText = new JLabel( searchResultAsString , JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);

话虽如此,您不应该在同一个线程上混合数据库查询和 Swing 对象修改/创建。Swing 对象必须在事件调度线程 (EDT) 上处理,并且不应在 EDT 上执行长时间运行的操作(例如数据库查询),因为在操作发生时 UI 将被阻塞/无响应。

查看Swing 中的并发教程以获取更多信息。一个典型的解决方案是使用SwingWorker. 查询完成后,SwingWorker有一个简单的机制可以在正确的线程上更新您的 Swing 组件。

于 2013-06-10T08:59:01.660 回答
3

JLabel lblTimeBilledDayText = new JLabel(search(@year,@month,@date,00,45,20) , JLabel.RIGHT);

于 2013-06-10T09:07:05.903 回答