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. 呵呵,就者样
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 {
}
}
}
举个小例子:
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();
}
}
你看着改一下你的程序吧,我下班了
Shrewdcat(丧邦&灵猫&潇) ,调通了读数据库后,如何改成每隔30秒读呢?我主要是代码上不知如何下手改。。唉,正在看线程这章,我从未写过线程,有点摸不到头脑
去继承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.
呵呵,就者样
说明几个参数:
Task:这就是你的任务是TimerTask类实例,
delay:在调用后延时delay毫秒后运行
firstTime:第一次执行时间
period:隔这么多秒重复一次
要用的差不多就这点,scheduleAtFixedRate和schedule区别是前者会每次以相同速度执行,
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);
catch(InterruptedException ex) {}
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) {
}
}
因为Thread.sleep方法会抛出异常,在会抛出异常的地方请用try{}catch(异常名 实例){
}
<<用javax.swing.Timer就很简单>>
帮忙给大家解释一下,就是想做一个简单的数据库下相关的东西,为什么要引入javax.swing的包?为什么要和swing耦合在一起?
或者你只从swing包里面提取从相关的不也可以。
我用了这么久的java,还没听说不能引入javax。swing的,awt不要与swing混用倒是听过
呵呵。谁也没有说不允许引入javax.swing,俺也没有这么说。俺就是想问一下为什么,谢谢解释啊。
谢谢Acylas的支持~~
catch(InterruptedException ex) {}