⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 romulustoolset.java

📁 《Java案例开发》源代码( 考勤系统的完整实现!) 包括数据库 网络通讯&uml
💻 JAVA
字号:
/* * RomulusToolSet.java * * Created on 2003年11月6日, 下午12:36 */package romulus;import java.sql.*;import java.util.*;import java.io.*;/** * The facade class of the whole Romulus system. * Such class is designed for maintain the whole Romulus system include all the RomulusNode. * Outer applications use this class to do the loading, storing and maintain. * For Example: * RomulusTestApp will use such class to get the initial test for testing. * RomulusDesignApp can use it to maintain all of the RomulusNodes. * (But such application will not be implemented in Demo so this class will not include any other RomulusNodes-maintain-methods). * And RomulusReportApp can get the historical test results. * @author  Romulus * @version 1.0 */public class RomulusToolSet {        /** The connection of the database.*/    private Connection con;        /** The visitors.*/    private DBInitLoadVisitor dbinitload;    private DBLoadVisitor dbload;    private GradeVisitor grade;            /** Creates new RomulusToolSet */    private RomulusToolSet() throws ClassNotFoundException, SQLException{        Class.forName("oracle.jdbc.driver.OracleDriver");        con = java.sql.DriverManager.getConnection(            "jdbc:oracle:thin:@10.222.3.12:1521:cpickfc",            "scott", "tiger");        dbinitload = new DBInitLoadVisitor(con);        dbload = new DBLoadVisitor(con);        grade = new GradeVisitor(con);    }        /** Creates new RomulusToolSet use a connection */    private RomulusToolSet(Connection con){        this.con = con;        dbinitload = new DBInitLoadVisitor(this.con);        dbload = new DBLoadVisitor(this.con);        grade = new GradeVisitor(this.con);    }        /** close the RomulusToolSet */    public void close() throws SQLException{        con.close();    }        /** finalize the RomulusToolSet */    private void finalizer() throws Throwable{        con.close();    }        /** check the RomulusToolSet */    public boolean isClosed() throws SQLException{        return con.isClosed();    }        /** Get default RomulusToolSet */    public static RomulusToolSet getInstance() throws ClassNotFoundException, SQLException{        return new RomulusToolSet();    }        /** Get RomulusToolSet use a database connection*/    public static RomulusToolSet getInstance(Connection con) throws ClassNotFoundException, SQLException{        return new RomulusToolSet(con);    }        /** A tool method used to get the s_ident from a table.     * @param table the table name.     * @param ident the string ident.     */    static int getSIdent(Connection con, String table, String ident) throws SQLException{        PreparedStatement pre = con.prepareStatement("select s_ident from "+table+" where ident  = ?");        pre.setString(1, ident);        ResultSet res = pre.executeQuery();        res.next();        return res.getInt(1);    }        /** A tool method used to get the ident from a table.     * @param table the table name.     * @param ident the int s_ident.     */    static String getIdent(Connection con, String table, int s_ident) throws SQLException{        Statement sta = con.createStatement();        ResultSet res = sta.executeQuery("select ident from "+table+" where s_ident = "+s_ident);        res.next();        return res.getString(1);    }    /** get the basic test*/    private Test getBasicTest(int s_ident) throws SQLException, RomulusException{        Statement sta = con.createStatement();        ResultSet res = sta.executeQuery("select ident, timing, title from test where s_ident  = " + s_ident);        if(res.next()){            return new Test(res.getString(1), res.getInt(2), res.getString(3));        }        else{            throw new RomulusException(RomulusException.IdentError);        }    }                /** get the basic test*/    private Test getBasicTest(String ident) throws SQLException, RomulusException{        PreparedStatement pre = con.prepareStatement("select ident, timing, title from test where ident  = ?");        pre.setString(1, ident);        ResultSet res = pre.executeQuery();        if(res.next()){            return new Test(res.getString(1), res.getInt(2), res.getString(3));        }        else{            throw new RomulusException(RomulusException.IdentError);        }    }            /** Get the full test use an int ident of the test.*/    public Test getFullTest(int s_ident) throws SQLException, RomulusException{        Test ret = getBasicTest(s_ident);        ret.Accept(dbload);        return ret;    }        /** Get the full test use a string ident of the test.*/    public Test getFullTest(String ident) throws SQLException, RomulusException{        Test ret = getBasicTest(ident);        ret.Accept(dbload);        return ret;    }        /** Get the initial test use an int ident of the test.*/    public Test getInitTest(int s_ident) throws SQLException, RomulusException{        Test ret = getBasicTest(s_ident);        ret.Accept(dbinitload);        return ret;    }        /** Get the initial test use a string ident of the test.*/    public Test getInitTest(String ident) throws SQLException, RomulusException{        Test ret = getBasicTest(ident);        ret.Accept(dbinitload);        return ret;    }        /** Store a new designed test to the database.     * Is not supported in demo.     */    public void storeTest(Test t){    }        /** Delete a existing test use a int ident.     * Note that in the Romulus System, delete a Test will not delete the questions included.     * Delete a question also will not delete the objective, feedback and question item.     * But delete a feedback, objective or question item will delete the content included.     */    public void delTest(int s_ident) throws SQLException{        Statement sta = con.createStatement();        sta.execute("delete from test where s_ident = "+s_ident);    }        /** Delete a existing test use a string ident.*/    public void delTest(String ident) throws SQLException{        PreparedStatement pre = con.prepareStatement("delete from test where ident  = ?");        pre.setString(1, ident);        pre.execute();    }        /** store a test result to the database.     * @return an long integer ident used to get the result back.     * ???     */    public long storeTestRes(Test t){        long ret=0;        return ret;    }        /** get a test result from the database.     * @return just the score of the test.     * ???     */    public int getTestRes(long s_ident){        int ret = 0;        return ret;    }        /** delete a test result from the database.     * ???     */    public void delTestRes(long s_ident){    }        /** grade the answered test, set the full answered test and get the score.     */    public double gradeTest(Test t) throws SQLException, RomulusException{        return grade.VisitTestGetScore(con, t);    }        /** main method used to test the Romulus System.*/    public static void main(String args[]){       RomulusToolSet rts = null ;        try{            //oracle            //rts = RomulusToolSet.getInstance();            //Test t =rts.getInitTest(12);                        //access            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");            Connection con = java.sql.DriverManager.getConnection("jdbc:odbc:Romulus", "Romulus", "romulus");            rts = RomulusToolSet.getInstance(con);            Test t = rts.getInitTest(1);                        QuestionItem qitem = null;            for(Iterator i = t.questionIterator(); i.hasNext(); ){                qitem = ((Question)i.next()).getItem();                if(qitem instanceof FIB){                    ((FIB)qitem).setAnswer("FIB");                }            }            double a = rts.gradeTest(t);            System.out.println(a+"");            System.out.println("Test information:");                        FileOutputStream fout = new FileOutputStream("f:\\TestData.ser");            ObjectOutputStream oout = new ObjectOutputStream(fout);            oout.writeObject(t);            oout.close();            FileInputStream fin = new FileInputStream("f:\\TestData.ser");            ObjectInputStream oin = new ObjectInputStream(fin);            Test testin = (Test)(oin.readObject());            oin.close();/*            System.out.println(t.getIdent()+"\t"+t.getTiming()+"\t"+t.getTitle());            //questions            Iterator ques = t.questionIterator();            System.out.println("\tQuestion information:");            Question question = null;            //objectives            Iterator objs = null;            Objective obj = null;                        while(ques.hasNext()){                question = (Question)ques.next();                System.out.println("\t"+question.getIdent()+"\t"+question.getTiming()+"\t"+question.getTitle());                objs = question.objectiveIterator();                System.out.println("\t\tObjective information:");                while(objs.hasNext()){                    obj = (Objective)objs.next();                    System.out.println("\t\t\t"+View.getView(obj.getView()));                }            } */        }        catch(SQLException e){	            do{	                System.out.println("Error Code:" + e.getErrorCode());	                e.printStackTrace();	                e = e.getNextException();	            }while(e == null);        }catch(Exception e){            e.printStackTrace();        }        finally{            try{                rts.close();            }catch(Exception e){                e.printStackTrace();            }        }    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -