一个RMI调用的小问题!HELP!!(50分)

  • 主题发起人 主题发起人 jeremy_z
  • 开始时间 开始时间
J

jeremy_z

Unregistered / Unconfirmed
GUEST, unregistred user!
用RMI实现查询数据库,但程序有问题,请版主们帮帮忙!
Lookup.java//接口
import java.rmi.*;
public interface Lookup extends Remote{
Stringdo
Query(String str) throws RemoteException;
}
LookupClient.java//客户端
import java.rmi.*;
import java.rmi.server.*;
public class LookupClient{
public static void main(String[] args) {
System.setSecurityManager(new RMISecurityManager());
String url = "rmi://jeremy/";
try {
Lookup backResult = (Lookup)Naming.lookup(url + "date");
System.out.println(backResult.doQuery(args[0]));
}//try
catch(Exception e){
System.out.println("Error " + e);
}//catch
System.exit(0);
}//main
}//Lookup
LookupImpl.java//实现接口的服务器类,主要实现数据查询功能
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
public class LookupImpl extends UnicastRemoteObject implements Lookup {
public Stringdo
Query(String str) throws RemoteException {
String dbUrl = "jdbc:odbc:test";
String user = "";
String password = "";
String ret = "";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection(dbUrl, user, password);
Statement s = c.createStatement();
ResultSet result = s.executeQuery("SELECT Name, MP, EMail,QQ " +"FROM test.mdb test " +"WHERE " +"(QQ='" + str + "') " +" AND (EMAIL Is Not Null) " +"ORDER BY Name");
ret = result.getString("Name") + ", "+ result.getString("MP")+ ": " + result.getString("EMAIL") +result.getString("QQ");
s.close();
}//try
catch(Exception e) {
e.printStackTrace();
}//catch()
return ret;
}//doQuery
}//LookupImpl
LookupServer.java//RMI服务器
import java.rmi.*;
import java.rmi.server.*;
import sun.applet.*;
public class LookupServer {
public static void main(String[] args) {
try {
System.out.print("Constructing server implementations...");
LookupImpl p1 = new LookupImpl();
System.out.println("Succeed!!");
System.out.print("Binding server implementations to registry...");
Naming.rebind("date", p1);
System.out.println("Succeed!!");
System.out.println("Waiting for invocations from clients...");
}//try
catch(Exception e){
System.out.println("Error: " + e);
}//catch
}//main
} //Lookup
最后运行Javac *.java
出错,提示为
LookupImpl.java:4: unreported exception java.rmi.RemoteException;
must be caught or declared to be thrown
public c(小三角所在处)lass LookupImpl extends UnicastRemoteObject implements Lookup {
帮帮忙啊~~~~~~~~~~
 
在类LookupImpl中加入构造函数,你必须让构造函数也throws RemoteException
public LookupImpl() throws RemoteException
{
}
具体原因请看Think in Java 2(真真是一个好书!!!)
 
非常感谢,我刚刚开始学习Java可能有很多问题,希望多交流!
 

Similar threads

后退
顶部