呵呵呵,我实在不好意思,那一章节的书我还没看到,这就要用到吗?唉,如何写呢,我不知如何写也,你能否帮我改改我这段程序,将它改成Thread+Timer,因为现在就在调试程序呀谢了,我今晚回去kk这方面

解决方案 »

  1.   

    同意  billdavid()  兄弟的。 关键就是设计一个计数器,没个30秒,就去执行对数据库操作的函数一次。 你把程序出问题的地方标出来,我们也好解决问题。大家都省点时间吗。
      

  2.   

    这是调通以后的代码:
    import java.io.*;
    import java.util.*;
    import java.sql.*;public class testdb {
      public String DBDriver = "com.informix.jdbc.IfxDriver";
      public String ConnStr = "jdbc:informix-sqli://192.168.1.2:12345/testdb:informixserver = test";
      public String username = "test";
      public String password = "test";
      public Connection conn = null;
      public Statement stmt = null;
      public ResultSet query(String QueryString) throws Exception {
        ResultSet r = null;
        stmt = conn.createStatement();
        r = stmt.executeQuery(QueryString);
        return r;
      }  public static void main(String args[]) {
        String sql = "select * from test where id='1'";
        testdb test = new testdb();
        try {
          Class.forName("DBDriver");
          String sUrl = test.ConnStr;
          test.conn = DriverManager.getConnection(sUrl, test.username, test.password);
          ResultSet t = test.query(sql);
          while (t.next()) {
            System.out.print(t.getString("id") + "|");
            System.out.print(t.getString("name"));
          }
          if (t!= null)
            t.close();
          if (test.stmt != null)
            test.stmt.close();
          test.conn.close();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        finally {
        }
      }
    }
      

  3.   

    实现线程有两种方法1.继承Thread 类2.实现Runable接口
    举个小例子:
    import java.awt.*;public class testRunnable implements Runnable
    {
        String str;
        public testRunnable(String s)
        {
            str=s;
        }
        public void run()
        {
            for(int i=0;i<3;i++)
            {
                try{
                System.out.println(str);
                Thread.sleep(1000);
                }catch(Exception e){}
            }
        }
        public static void main(String args[])
        {
            testRunnable t1=new testRunnable("test...1   ");
            t1.start();
            testRunnable t2=new testRunnable("test...2   ");
            t2.start();
        }
    }
    你看着改一下你的程序吧,我下班了
      

  4.   

    ****万分感谢各位给小弟的指点****
    Shrewdcat(丧邦&灵猫&潇) ,调通了读数据库后,如何改成每隔30秒读呢?我主要是代码上不知如何下手改。。唉,正在看线程这章,我从未写过线程,有点摸不到头脑
      

  5.   

    to楼主
    去继承timer类或,timertask类
    你实现重写run方法
    类里面有个函数叫void schedule(TimerTask task, Date time) 
              Schedules the specified task for execution at the specified time. 
     void schedule(TimerTask task, Date firstTime, long period) 
              Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. 
     void schedule(TimerTask task, long delay) 
              Schedules the specified task for execution after the specified delay. 
     void schedule(TimerTask task, long delay, long period) 
              Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. 
     void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) 
              Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. 
     void scheduleAtFixedRate(TimerTask task, long delay, long period) 
              Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
    呵呵,就者样
      

  6.   

    再来补充几句:
    说明几个参数:
    Task:这就是你的任务是TimerTask类实例,
    delay:在调用后延时delay毫秒后运行
    firstTime:第一次执行时间
    period:隔这么多秒重复一次
    要用的差不多就这点,scheduleAtFixedRate和schedule区别是前者会每次以相同速度执行,
      

  7.   

    我改了一下maweihua(laoma) 给的小例子,为什么我将Thread.sleep(1000)y放到main中编译会报错。。
    import java.awt.*;
    public class testRunnable implements Runnable
    {
        String str;
        public testRunnable(String s)
        {
            str=s;
        }
        public void run()
        {
            for(int i=0;i<3;i++)
            {
                try{
                System.out.println(str);
             //   Thread.sleep(1000);
                }catch(Exception e){}
            }
        }
        public static void main(String args[])
        {  while(true)
           {
            Runnable t1=new testRunnable("test...1   ");
            Thread r1=new Thread(t1);
            r1.start();
            Runnable t2=new testRunnable("test...2   ");
            Thread r2=new Thread(t2);
            r2.start();
            Thread.sleep(1000);
            }
         }
    }
    报错如下:unreported exception java.lang.InterruptedException; must
    be caught or declared to be thrown
            Thread.sleep(1000);
      

  8.   

    try{Thread.sleep(1000);}
    catch(InterruptedException ex) {}
      

  9.   

    用javax.swing.Timer就很简单,
    Timer timer= new Timer(30 * 1000, new SymAction());每隔30秒就会自动调用timer_actionPerformed方法,方法里面你要读数据库什么的随便你
    class SymAction implements java.awt.event.ActionListener {
     public void actionPerformed(java.awt.event.ActionEvent event) {
       Object ojbect = event.getSource();
       if (object == timer) timer_actionPerformed(event);
    }
      void timer_actionPerformed(java.awt.event.ActionEvent event) {
    }
    }
      

  10.   

    to楼主:
    因为Thread.sleep方法会抛出异常,在会抛出异常的地方请用try{}catch(异常名 实例){
    }
      

  11.   

    倒,你认为这样可行吗??数据库包括网络都会被拖跨,占用最大资源的就是连接数据库,要通过好几层才可以跟ORACLE连接,这样你的数据库会很慢的,给你个建议,不要断开连接,你就一直连接在数据库上,连上了不占什么资源,反正是在连接池中,而使用线程,每30秒让他醒来执行完再谁,就此循环~
      

  12.   

    TO Acylas(Acylas) :
    <<用javax.swing.Timer就很简单>>
    帮忙给大家解释一下,就是想做一个简单的数据库下相关的东西,为什么要引入javax.swing的包?为什么要和swing耦合在一起?
      

  13.   

    楼主可没有那样的要求,就算是有,不想用swing,jdk里面有源程序你可以将它转过来,
    或者你只从swing包里面提取从相关的不也可以。
    我用了这么久的java,还没听说不能引入javax。swing的,awt不要与swing混用倒是听过
      

  14.   

    我赞成 cocosunshine(我爱阳光) 的观点,如果真的是每隔30秒去链接数据库,那这数据库和网络也差不多废了。使用连接池一直连着资源会节省很多,不过30秒读写一次数据库还是有些浪费
      

  15.   

    cocosunshine(我爱阳光),能否说明点,如何让他一直连在数据库上而不断开呢?用线程吗?那是不是用run()时不要test.conn.close()就行了吗?呵呵呵,我有些不明白,望指教
      

  16.   

    你在循环之前(线程运行之前)打开跟数据库的链接,配置好connection,在循环过程(线程运行过程)只是查询和写入,在循环结束(线程结束)或者是退出程序的时候关闭connection
      

  17.   

    TO Acylas(Acylas) :
    呵呵。谁也没有说不允许引入javax.swing,俺也没有这么说。俺就是想问一下为什么,谢谢解释啊。
      

  18.   

    呵呵,其实我的意思就是你使用线程一直连接数据库,当30秒执行一次run,剩下时间为sleep,这样做法比较好~~
    谢谢Acylas的支持~~
      

  19.   

    try{Thread.sleep(long);}
    catch(InterruptedException ex) {}