📄 queryclient.java
字号:
//QueryClient.java
import java.io.*;
import java.rmi.*;
import java.sql.*;
public class QueryClient
{
public static void main(String args[])
{
try
{
int RMIPort; //RMI服务端口
String hostName;//主机名
int choose=0; //选择操作
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the RMIRegistry host ip/name :");
hostName=br.readLine().trim();
System.out.print("Enter the Port No. :");
String portNum=br.readLine().trim();
RMIPort=Integer.parseInt(portNum);
String registryURL="rmi://"+hostName+":"+portNum+"/score";
QueryClientHelper helper=new QueryClientHelper(registryURL);//初始化helper
while(true)
{
System.out.println("please choose a job:");
System.out.println("\t0.quit");
System.out.println("\t1.listALL");
System.out.println("\t2.insert");
System.out.println("\t3.update");
System.out.println("\t4.delete");
System.out.println("\t5.sumScore");
System.out.print(":");
choose=Integer.parseInt(br.readLine().trim());
if(choose==0) break;
switch(choose)
{
case 1:helper.listAll();
break;
case 2:helper.insert();
break;
case 3:helper.update();
break;
case 4:helper.delete();
break;
case 5:helper.sumScore();
break;
default:System.out.println("Wrong choose.");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class QueryClientHelper
{//客户端应用逻辑与服务逻辑类
private QueryInterface agrent; //RMI代理
private BufferedReader br;
public QueryClientHelper(String registryURL)
{
try
{
br=new BufferedReader(new InputStreamReader(System.in));
agrent=(QueryInterface)Naming.lookup(registryURL); //名字查找代理
System.out.println("Lookup complete.");
}
catch(NotBoundException e)
{
e.printStackTrace();
}
catch(IOException io)
{
io.printStackTrace();
}
}
public void listAll()
{//打印表
try
{
System.out.println(agrent.listAll()); //RMI调用,打印表
}
catch(RemoteException re)
{
re.printStackTrace();
}
}
public void insert()
{//插入操作
String SID="";
String SNAME="";
String SCHOOL="";
String courseA="";
String courseB="";
String courseC="";
String courseD="";
try
{
System.out.print("please input the SID :");
SID=br.readLine().trim();
System.out.print("please input the SNAME :");
SNAME=br.readLine().trim();
System.out.print("please input the SCHOOL :");
SCHOOL=br.readLine().trim();
System.out.print("please input the courseA :");
courseA=br.readLine().trim();
System.out.print("please input the courseB :");
courseB=br.readLine().trim();
System.out.print("please input the courseC :");
courseC=br.readLine().trim();
System.out.print("please input the courseD :");
courseD=br.readLine().trim();
System.out.println(agrent.insert(SID,SNAME,SCHOOL,courseA,courseB,courseC,courseD));//RMI调用,插入操作
}
catch(RemoteException re)
{
re.printStackTrace();
}
catch(IOException io)
{
io.printStackTrace();
}
}
public void update()
{//更改表
try
{
System.out.print("please input the SID :");
String SID=br.readLine().trim();
if(agrent.find(SID,"SNAME").equals("Not found the SID."))
{
System.out.println("Not found the SID.");
}
else{
System.out.print("SNAME[" + agrent.find(SID,"SNAME") + "]:");
String SNAME=br.readLine().trim();
if(SNAME.equals("")) SNAME = agrent.find(SID,"SNAME");
System.out.print("SCHOOL[" + agrent.find(SID,"SCHOOL") + "]:");
String SCHOOL=br.readLine().trim();
if(SCHOOL.equals("")) SCHOOL = agrent.find(SID,"SCHOOL");
System.out.print("COURSEA[" + agrent.find(SID,"COURSEA") + "]:");
String courseA=br.readLine().trim();
if(courseA.equals("")) courseA = agrent.find(SID,"COURSEA");
System.out.print("COURSEB[" + agrent.find(SID,"COURSEB") + "]:");
String courseB=br.readLine().trim();
if(courseB.equals("")) courseB = agrent.find(SID,"COURSEB");
System.out.print("COURSEC[" + agrent.find(SID,"COURSEC") + "]:");
String courseC=br.readLine().trim();
if(courseC.equals("")) courseC = agrent.find(SID,"COURSEC");
System.out.print("COURSED[" + agrent.find(SID,"COURSED") + "]:");
String courseD=br.readLine().trim();
if(courseD.equals("")) courseD = agrent.find(SID,"COURSED");
System.out.println(agrent.update(SID,SNAME,SCHOOL,courseA,courseB,courseC,courseD));//RMI调用,更改表
}
}
catch(RemoteException re)
{
re.printStackTrace();
}
catch(IOException io)
{
io.printStackTrace();
}
}
public void delete()
{//删除关系模式
String SID="";
try
{
System.out.print("please input the SID: ");
SID=br.readLine();
System.out.println(agrent.delete(SID));//RMI调用,删除关系模式
}
catch(RemoteException re)
{
re.printStackTrace();
}
catch(IOException io)
{
io.printStackTrace();
}
}
public void sumScore()
{//计算某个学生的总成绩
String SID="";
try
{
System.out.println(agrent.sumScore());//RMI调用,计算某个学生的总成绩
}
catch(RemoteException re)
{
re.printStackTrace();
}
catch(IOException io)
{
io.printStackTrace();
}
}
}
//SCORE.xml
<?xml version="1.0" encoding="GB2312"?>
<STUDENTS>
</STUDENTS>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -