java RMI with XML的简单成绩管理系统

// QueryInterface.java

import java.rmi.*;

public interface QueryInterface extends Remote
{
 public String listAll() throws RemoteException;
 public String insert(String SID,String SNAME,String SCHOOL,String courseA,
       String courseB,String courseC,String courseD) throws RemoteException;
 public String update(String SID,String SNAME,String SCHOOL,String courseA,
       String courseB,String courseC,String courseD)throws RemoteException;
 public String delete(String SID) throws RemoteException;
 public String sumScore() throws RemoteException;
 public String find(String SID,String attribute) throws RemoteException;
}
//QueryImpl.java

import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;

public class QueryImpl extends UnicastRemoteObject implements QueryInterface
{
 public QueryImpl() throws RemoteException
 {
  super();
 }

 private Document getDocument()
 {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = null;
  try {
     db = dbf.newDocumentBuilder();
  }
  catch (ParserConfigurationException pce) {
     pce.printStackTrace(); //出异常时输出异常信息,然后退出,下同
     return null;
  }

  Document doc = null;
  try {
     doc = db.parse("SCORE.xml");
  }
  catch (Exception dom) {
     dom.printStackTrace();
     return null;
  }
  return doc;
 }

 private String getValue(NodeList n)
 {
  if (n.getLength() == 1){
   Element e = (Element) n.item(0);
   Text t = (Text) e.getFirstChild();
   return t.getNodeValue();
  }
  else return "";
 }

 public String listAll() throws RemoteException
 {//打印表
  String temp="SID/t/tSNAME/tSCHOOL/t/tCOURSEA/tCOURSEB/tCOURSEC/tCOURSED/n";
  Document doc = getDocument();
  Element root = doc.getDocumentElement();
  NodeList students = root.getElementsByTagName("STUDENT");
  if(students != null)
  {
   for(int i = 0 ; i < students.getLength() ; i ++)
   {
    Element student = (Element) students.item(i);
    String SID = student.getAttribute("SID");
    String SNAME = "";
    SNAME = getValue(student.getElementsByTagName("SNAME"));
    temp=temp + SID +'/t'
     + SNAME +'/t'
     + getValue(student.getElementsByTagName("SCHOOL")) +'/t'
     + getValue(student.getElementsByTagName("COURSEA")) +'/t'
     + getValue(student.getElementsByTagName("COURSEB")) +'/t'
     + getValue(student.getElementsByTagName("COURSEC")) +'/t'
     + getValue(student.getElementsByTagName("COURSED")) +'/n';
   }
  }
  return temp;
 }

 private void insertElement(Document doc, Element student, String attribute, String value)
 {
  Element e = doc.createElement(attribute);
  student.appendChild(e);
  Text t = doc.createTextNode(value);
  e.appendChild(t);
 }

 public String insert(String SID,String SNAME,String SCHOOL,String courseA,
       String courseB,String courseC,String courseD) throws RemoteException
 {//插入操作
  Document doc = getDocument();
  Element root = doc.getDocumentElement();
  Element student = doc.createElement("STUDENT");
  student.setAttribute("SID",SID);
  root.appendChild(student);
  insertElement(doc,student,"SNAME",SNAME);
  insertElement(doc,student,"SCHOOL",SCHOOL);
  insertElement(doc,student,"COURSEA",courseA);
  insertElement(doc,student,"COURSEB",courseB);
  insertElement(doc,student,"COURSEC",courseC);
  insertElement(doc,student,"COURSED",courseD);
  try{
   FileOutputStream outStream = new FileOutputStream("SCORE.xml");
   OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
   ( (XmlDocument) doc).write(outWriter, "GB2312");
   outWriter.close();
   outStream.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
   return "Insert unsuccessfully.";
  }
  return "Insert successfully.";
 }
 
 public String update(String SID,String SNAME,String SCHOOL,String courseA,
       String courseB,String courseC,String courseD) throws RemoteException
 {//更新操作
  Document doc = getDocument();
  Element root = doc.getDocumentElement();
  NodeList students = root.getElementsByTagName("STUDENT");
  if(students != null)
  {
   for(int i = 0 ; i < students.getLength() ; i ++)
   {
    Element student = (Element) students.item(i);
    if(SID.trim().equals(student.getAttribute("SID")))
    {
     root.removeChild(student);
     Element stu = doc.createElement("STUDENT");
     stu.setAttribute("SID",SID);
     root.appendChild(stu);
     insertElement(doc,stu,"SNAME",SNAME);
     insertElement(doc,stu,"SCHOOL",SCHOOL);
     insertElement(doc,stu,"COURSEA",courseA);
     insertElement(doc,stu,"COURSEB",courseB);
     insertElement(doc,stu,"COURSEC",courseC);
     insertElement(doc,stu,"COURSED",courseD);
     try{
      FileOutputStream outStream = new FileOutputStream("SCORE.xml");
      OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
      ( (XmlDocument) doc).write(outWriter, "GB2312");
      outWriter.close();
      outStream.close();
     }
     catch(Exception e)
     {
      e.printStackTrace();
      return "Update unsuccessfully.";
     }
     return "Update successfully.";
    }
   }
   return "Not found the SID.";
  }
  else return "Not found the SID.";
 }
 public String delete(String SID) throws RemoteException
 {//删除操作
  Document doc = getDocument();
  Element root = doc.getDocumentElement();
  NodeList students = root.getElementsByTagName("STUDENT");
  if(students != null)
  {
   for(int i = 0 ; i < students.getLength() ; i ++)
   {
    Element student = (Element) students.item(i);
    if(SID.trim().equals(student.getAttribute("SID")))
    {
     root.removeChild(student);
     try{
      FileOutputStream outStream = new FileOutputStream("SCORE.xml");
      OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
      ( (XmlDocument) doc).write(outWriter, "GB2312");
      outWriter.close();
      outStream.close();
     }
     catch(Exception e)
     {
      e.printStackTrace();
      return "Delete unsuccessfully.";
     }
     return "Delete successfully.";
    }
   }
   return "Not found the SID.";
  }
  else return "Not found the SID.";
 }
 public String sumScore() throws RemoteException
 {//计算学生总成绩

  String temp="SID/t/tSNAME/tSCHOOL/t/tsumScore/n";
  Document doc = getDocument();
  Element root = doc.getDocumentElement();
  NodeList students = root.getElementsByTagName("STUDENT");
  if(students != null)
  {
   for(int i = 0 ; i < students.getLength() ; i ++)
   {
    Element student = (Element) students.item(i);
    String SID = student.getAttribute("SID");
    String SNAME = "";
    SNAME = getValue(student.getElementsByTagName("SNAME"));
    int courseA = java.lang.Integer.parseInt(getValue(student.getElementsByTagName("COURSEA")));
    int courseB = java.lang.Integer.parseInt(getValue(student.getElementsByTagName("COURSEB")));
    int courseC = java.lang.Integer.parseInt(getValue(student.getElementsByTagName("COURSEC")));
    int courseD = java.lang.Integer.parseInt(getValue(student.getElementsByTagName("COURSED")));
    temp=temp + SID +'/t'
     + SNAME +'/t'
     + getValue(student.getElementsByTagName("SCHOOL")) +'/t'
     + java.lang.String.valueOf(courseA + courseB + courseC + courseD) +'/n';
   }
  }
  return temp; 
 }

 public String find(String SID, String attribute) throws RemoteException
 {
  Document doc = getDocument();
  Element root = doc.getDocumentElement();
  NodeList students = root.getElementsByTagName("STUDENT");
  if(students != null)
  {
   for(int i = 0 ; i < students.getLength() ; i ++)
   {
    Element student = (Element) students.item(i);
    if(SID.trim().equals(student.getAttribute("SID")))
    {
     return getValue(student.getElementsByTagName(attribute.trim()));
    }
   }
   return "Not found the SID.";
  }
  else return "Not found the SID.";
 }
}

//QueryServer.java

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.net.*;
import java.io.*;

public class QueryServer
{
 public static void main(String args[])
 {
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  String portNum,registryURL;
  try
  {
   System.out.print("Enter the RMIregistry port number :");
   portNum=(br.readLine()).trim();
   int RMIPortNum=Integer.parseInt(portNum);
   startRegistry(RMIPortNum);
   QueryImpl exportedObj=new QueryImpl() ;
   registryURL="rmi://localhost:"+portNum+"/score";
   Naming.rebind(registryURL,exportedObj);
   System.out.println("Server registered. Registry currently contains:");
   listRegistry(registryURL);
   System.out.println("Server is ready.");
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }

 private static void startRegistry(int RMIPortNum)throws RemoteException
 {
  try
  {
   Registry registry1=LocateRegistry.getRegistry(RMIPortNum);
   registry1.list();
  }
  catch(RemoteException e)
  {
   Registry registry1=LocateRegistry.createRegistry(RMIPortNum);
  }
 }

 private static void listRegistry(String registryURL)throws RemoteException,MalformedURLException
 {
  System.out.println("Registry"+registryURL+"contains: ");
  String []names=Naming.list(registryURL);
  for(int i=0;i<names.length;i++)
   System.out.println(names[i]);
 }
}

//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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值