我在mysql中建了两个数据库A》B,程序每隔一秒读一次A数据库的表,如果有记录则写入B的表中。
import java.net.URL;
import java.sql.*;
class  name
{
public static void main(String[] args) 
{
int j=1;
Dbconnection Localcon=new Dbconnection("jdbc:odbc:tests","sun.jdbc.odbc.JdbcOdbcDriver","aa","");
Dbconnection Remotecon=new Dbconnection("jdbc:odbc:test","sun.jdbc.odbc.JdbcOdbcDriver","aa","");
ResultSet Localset;
ResultSet Remoteset;
        String strup;
String strde;
System.out.println("定义表单集完毕");
while(j<6)
{
j++;
try
{
Localset=Localcon.stmt.executeQuery("select SrcTermID,MsgContent from sms_mo");
System.out.println("搜索表完毕");
while(Localset.next())
{
System.out.println("有记录");
ReceiveData.SrcTermID=Localset.getString("SrcTermID");
System.out.println(ReceiveData.SrcTermID);
ReceiveData.MsgContent=Localset.getString("MsgContent");
System.out.println(ReceiveData.MsgContent);
strup="Update test SET Msgcontent='"+ReceiveData.MsgContent+"' WHERE SrcTermID='"+ReceiveData.SrcTermID+"'";
                strde="DELETE from sms_mo WHERE SrcTermID='"+ReceiveData.SrcTermID+"'";
Remotecon.stmt.executeUpdate(strup);
System.out.println("更新数据库test完毕");
//Localcon.stmt.executeUpdate(strde);
System.out.println("删除原记录完毕");

}
try
{
Thread.sleep(5000);
System.out.println("休息");
}
catch(InterruptedException ex){}

//Localcon.closecon();
//Remotecon.closecon();
//System.out.println("关闭数据库");
}
catch(SQLException e)
{
while(e!=null)
{
System.out.println("数据库异常啊");
System.out.println(e.getSQLState());
System.out.println(e.getMessage());
System.out.println(e.getErrorCode());
e=e.getNextException();
}
} }
}
}
class Dbconnection
{
private String dburl,dbDriver,dbuser,dbpass;
public Connection con;
public Statement stmt; public Dbconnection(String str1,String str2,String str3,String str4)
{
this.dburl=str1;
this.dbDriver=str2;
this.dbuser=str3;
this.dbpass=str4;
try
{
try
{
Class.forName(dbDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("class not found");
System.err.println(e.getMessage());
}
con=DriverManager.getConnection(dburl,dbuser,dbpass);
stmt=con.createStatement();
System.out.println("数据库已连接");
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("数据库异常");
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
}
}
}
public void closecon()
{
try{
con.close();}
catch(SQLException exc)
{
while(exc!=null)
{
System.out.println("数据库异常");
System.out.println(exc.getMessage());
System.out.println(exc.getErrorCode());
exc=exc.getNextException();
}
}
}
}
class ReceiveData 
{
  public ReceiveData() 
  {
    TableName="ReceiveQueue";
    SrcTermID="";
    SrcTermType="";
    DestTermID="";
    MsgFormat="";
    MsgContent="";
    gateway="";
    linkID="";
    ReceiveTime="";
    id="";
  }
  public static String TableName;
  public static String SrcTermID;
  public static String SrcTermType;
  public static String DestTermID;
  public static String MsgFormat;
  public static String MsgContent;
  public static String linkID;
  public static String id;
  public static String gateway;
  public static String ReceiveTime;
}
以下是执行情况,
C:\>java name
数据库已连接
数据库已连接
定义表单集完毕
搜索表完毕
有记录
12345678990
ssssssssssssssss
更新数据库test完毕
删除原记录完毕
数据库异常啊
null
ResultSet is closed〈-------?????
0
搜索表完毕
有记录
13935842499
aaaaaaaaaaaaa
更新数据库test完毕
删除原记录完毕
数据库异常啊
null
ResultSet is closed〈-------?????
0
搜索表完毕
休息
搜索表完毕
休息
搜索表完毕
休息