我是这样做的:主线程每个5秒读取数据库中的记录(通过thread.sleep(5000)实现),找到记录后(记录可能不止一条),将这些记录放入一个list结构中。另外一个线程定时扫描这个list队列,如果有内容,发送之,然后从list中删除之。不知道这样是否可行?
是否还有其它的办法?

解决方案 »

  1.   

    先写一个延时线程,就像用JAVA实现时间的显示一样再写一个线程专门用来读取数据庫,用上一个延时线程每隔一段时间激活它一次最后写一个发送数据线程,发送给服务器端
    服务器端只需一个用来接收数据的线程即可,用死循环,不断读取客户端发送的信息最好用ObjectInputStream和ObjectOutputStream,写一个类,把数据库中资料的字段写进类中,然后传对象,不过要记得,此类必须支持串行化,即implements java.io.Serializable
      

  2.   

    定时程序可以利用操作系统来实现也可以通过线程(如果对时间精度要求不高的话),接受端可以利用serverSocket类一直监听客户端的连接。
      

  3.   

    在发送方用Thread类的sleep方法让读数据的的方法隔一段时间执行一次,然后调用send方法发送数据。
      

  4.   

    用time 类实现定时的效果应该也可以!
      

  5.   

    MyTest.java  定时查询数据库,将找到的记录发送到server端
    MyTestServer.java  服务器端运行后结果不正确,如果数据库中有两条记录,读完第一条后发送的时候有问题,服务器端好像接收不到MyTest.java
    -------------------
    import java.util.*;
    import java.sql.*;
    import java.io.*;
    import java.net.*;public class MyTest 
    {  
    public static void main(String[] args) 
    {   
    try { 
    //load driver
    Class.forName("org.gjt.mm.mysql.Driver");
    } catch(Exception e) {
    e.printStackTrace();
    }

    TimerTask tt = new TimerTask() {      
    public void run() {        


    //查询数据库
    try {
    //make a connection
    String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gb2312";
    Connection con = DriverManager.getConnection(url,"root","");

    //
    Statement stmt = con.createStatement();
    String query = "select * from e_units";
    ResultSet rs = stmt.executeQuery(query);
    while(rs.next())
    {
    String uname = rs.getString("unitname");
    System.out.println(uname);


    Socket clientSocket = null;
    DataOutputStream os = null;
    DataInputStream is = null;

    try {
    clientSocket = new Socket("localhost", 8888);
    os = new DataOutputStream(clientSocket.getOutputStream());
    is = new DataInputStream(clientSocket.getInputStream());
    } catch(Exception e) {
    e.printStackTrace();
    }

    try {

    os.writeBytes(uname);

    String responseLine;
    while ((responseLine = is.readLine()) != null) {
    System.out.println("from server : " + responseLine);
    } os.close();
    is.close();
    clientSocket.close();
    } catch(Exception e) {
    e.printStackTrace();
    }


    } rs.close();
    stmt.close();
    con.close();


    } catch(Exception e) {
    e.printStackTrace();
    }
    }    
    };    

    final long loPeriod = 5000;

    new Timer().scheduleAtFixedRate(tt, 0, loPeriod); }
    }MyTestServer.java
    ----------------------
    import java.io.*;
    import java.net.*;public class MyTestServer {
        public static void main(String args[]) {        ServerSocket serverSocket = null;
            Socket clientSocket = null;
            DataInputStream is;
            PrintStream os;
            String s;        try {
               serverSocket = new ServerSocket(8888);
            }
            catch (IOException e) {
               System.out.println(e);
            }    try {
             clientSocket = serverSocket.accept();
                is = new DataInputStream(clientSocket.getInputStream());
    os = new PrintStream(clientSocket.getOutputStream());            while (true) {
                System.out.println("**");
                  s = is.readLine();
                  System.out.println("from client : " + s);
                  os.println(s); 
               }
            }   
    catch (IOException e) {
             System.out.println(e);
            }
        }
    }
      

  6.   

    为什么会这样?服务器端处于死循环:服务器端代码:
    try {
      serverSocket = new ServerSocket(8888);
    }
    catch (IOException e) {
      System.out.println(e);
    }   try {
      clientSocket = serverSocket.accept();
      reader = new BufferedReader(
         new InputStreamReader(clientSocket.getInputStream()));  while (true) {
        System.out.println("**");
        s = reader.readLine();
        System.out.println("from client : " + s);            
      }
    }   
    catch (IOException e) {
      e.printStackTrace();
    }执行的结果:
    不停打印 from client : null