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

📄 testself.java

📁 一个用java写的开源的数据库系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        try {            sStatement = cConnection.createStatement();        } catch (Exception e) {            e.printStackTrace();            print("Marotest init error: " + e.getMessage());        }        try {            // test duplicate keys & small transaction rollback            s = "CREATE TABLE marotest (id int PRIMARY KEY, dat int);"                + "INSERT INTO marotest VALUES (1,0);"                + "INSERT INTO marotest VALUES (2,0);"                + "INSERT INTO marotest VALUES (2,0);";            try {                sStatement.execute(s);                s = "";            } catch (Exception e) {}            if (s.equals("")) {                throw new Exception("Duplicate key gave no error on insert");            }            try {                s = "UPDATE marotest SET id=1, dat=-1 WHERE dat=0";                sStatement.execute(s);                s = "";            } catch (Exception e) {}            if (s.equals("")) {                throw new Exception("Duplicate key gave no error on update");            }            int count = 0;            s = "SELECT *, id as marotest_id FROM marotest";            r = sStatement.executeQuery(s);            while (r.next()) {                r.getFloat(1);                r.getString("id");                r.getInt("DaT");                r.getInt("marotest_id");                if (r.getShort("dat") != 0) {                    throw new Exception("Bad update worked");                }                r.getLong("DAT");                r.getString(2);                r.getObject("ID");                r.clearWarnings();                try {                    // this must throw an error                    r.getTimestamp("Timestamp?");                    count = 99;                } catch (Exception e) {}                count++;            }            r.close();            if (count != 2) {                throw new Exception("Should have 2 but has " + count                                    + " rows");            }            // test database meta data            DatabaseMetaData dbMeta = cConnection.getMetaData();            r = dbMeta.getColumns(null, "dbo", "MAROTEST", "%");            while (r.next()) {                s = r.getString(4).trim();    // COLUMN_NAME                int i = r.getInt(5);          // DATA_TYPE                s += i + r.getString("TYPE_NAME");                i = r.getInt(7);              // COLUMN_SIZE                i = r.getInt(9);              // "Decimal_Digits"                i = r.getInt(11);             // NULLABLE                s = s.toUpperCase();                if (!s.equals("ID4INTEGER") &&!s.equals("DAT4INTEGER")) {                    throw new Exception("Wrong database meta data");                }            }            s = "DROP TABLE marotest";            sStatement.execute(s);            sStatement.close();        } catch (Exception e) {            print("");            print("Marotest error: " + e);            print("with SQL command: " + s);            e.printStackTrace();        }    }    static void testPerformance(String url, String user, String password,                                int max,                                boolean persistent) throws Exception {        if (persistent) {            delete("test2.backup");            delete("test2.properties");            delete("test2.script");            delete("test2.data");        }        Statement  sStatement  = null;        Connection cConnection = null;        ResultSet  r;        String     s = "";        long       start;        boolean    bDropError = false;        String     name       = persistent ? "Persistent"                                           : "Memory";        print(name + " Performance");        try {            cConnection = DriverManager.getConnection(url, user, password);            sStatement  = cConnection.createStatement();        } catch (Exception e) {            e.printStackTrace();            print("TestSelf init error: " + e.getMessage());        }        try {            // cache, index and performance tests            s = "CREATE CACHED TABLE Addr(ID INT PRIMARY KEY,First CHAR,"                + "Name CHAR,ZIP INT)";            sStatement.execute(s);            s = "CREATE INDEX iName ON Addr(Name)";            sStatement.execute(s);            s = "SET WRITE_DELAY TRUE";            sStatement.execute(s);            start = System.currentTimeMillis();            for (int i = 0; i < max; i++) {                s = "INSERT INTO Addr VALUES(" + i + ",'Marcel" + i + "',"                    + "'Renggli" + (max - i - (i % 31)) + "',"                    + (3000 + i % 100) + ")";                if (sStatement.executeUpdate(s) != 1) {                    throw new Exception("Insert failed");                }                if (i % 100 == 0) {                    printStatus("insert   ", i, max, start);                }            }            printStatus("insert   ", max, max, start);            print("");            s = "SELECT COUNT(*) FROM Addr";            r = sStatement.executeQuery(s);            r.next();            int c = r.getInt(1);            if (c != max) {                throw new Exception("Count should be " + (max) + " but is "                                    + c);            }            if (persistent) {                // close & reopen to test backup                cConnection.close();                cConnection = DriverManager.getConnection(url, user,                        password);                sStatement = cConnection.createStatement();            }            start = System.currentTimeMillis();            for (int i = 0; i < max; i++) {                s = "UPDATE Addr SET Name='Robert" + (i + (i % 31))                    + "' WHERE ID=" + i;                if (sStatement.executeUpdate(s) != 1) {                    throw new Exception("Update failed");                }                if (i % 100 == 0) {                    printStatus("updated  ", i, max, start);                    // s="SELECT COUNT(*) FROM Addr";                    // r=sStatement.executeQuery(s);                    // r.next();                    // int c=r.getInt(1);                    // if(c!=max) {                    // throw new Exception("Count should be "+max+" but is "+c);                    // }                }            }            printStatus("update   ", max, max, start);            print("");            if (persistent) {                s = "SHUTDOWN IMMEDIATELY";                sStatement.execute(s);                // open the database; it must be restored after shutdown                cConnection.close();                cConnection = DriverManager.getConnection(url, user,                        password);                sStatement = cConnection.createStatement();            }            start = System.currentTimeMillis();            for (int i = 0; i < max; i++) {                s = "DELETE FROM Addr WHERE ID=" + (max - 1 - i);                if (sStatement.executeUpdate(s) != 1) {                    throw new Exception("Delete failed");                }                if (i % 100 == 0) {                    printStatus("deleting ", i, max, start);                    // s="SELECT COUNT(*) FROM Addr";                    // r=sStatement.executeQuery(s);                    // r.next();                    // int c=r.getInt(1);                    // if(c!=max-i-1) {                    // throw new Exception("Count should be "+(max-i-1)+" but is "+c);                    // }                }            }            printStatus("delete   ", max, max, start);            print("");            sStatement.execute("DROP TABLE Addr");        } catch (Exception e) {            print("");            print("TestSelf error: " + e);            print("with SQL command: " + s);            e.printStackTrace();        }        cConnection.close();        print("Test finished");    }    /**     *  Method declaration     *     * @param  stat     * @param  s     * @throws  Exception     */    static void test(Statement stat, String s) throws Exception {        String result = "";        char   type   = ' ';        if (s.startsWith("/*")) {            type = s.charAt(2);            int end = s.indexOf("*/");            result = s.substring(3, end);        }        try {            stat.execute(s);            int       u = stat.getUpdateCount();            int       i = 0;            ResultSet r;            switch (type) {                case ' ' :                    break;                case 'u' :                    if (u != Integer.parseInt(result)) {                        throw new Exception("Expected update count=" + result                                            + " but update count was " + u                                            + " / " + s);                    }                    break;                case 'r' :                    if (u != -1) {                        throw new Exception("Expected ResultSet"                                            + " but update count was " + u                                            + " / " + s);                    }                    r = stat.getResultSet();                    r.next();                    String col = r.getString(1);                    if (r.wasNull() || col == null) {                        if (!result.equals("")) {                            throw new Exception("Expected " + result                                                + " but got null / " + s);                        }                    } else if (!col.equals(result)) {                        throw new Exception("Expected >" + result + "<"                                            + " but got >" + col + "< / "                                            + s);                    }                    break;                case 'c' :                    if (u != -1) {                        throw new Exception("Expected ResultSet"                                            + " but update count was " + u                                            + " / " + s);                    }                    r = stat.getResultSet();                    while (r.next()) {                        i++;                    }                    if (i != Integer.parseInt(result)) {                        throw new Exception("Expected " + result + " rows "                                            + " but got " + i + " rows / "                                            + s);                    }                    break;                case 'e' :                    throw new Exception("Expected error "                                        + "but got no error / " + s);            }        } catch (SQLException e) {            if (type != 'e') {                throw new Exception("Expected " + type + "/" + result                                    + " but got error " + e.getMessage()                                    + " / " + s);            }        }    }    /**     *  Method declaration     *     * @param  s     * @param  i     * @param  max     * @param  start     */    static void printStatus(String s, int i, int max, long start) {        System.out.print(s + ": " + i + "/" + max + " " + (100 * i / max)                         + "% ");        long now = System.currentTimeMillis();        if (now > start) {            System.out.print((i * 1000 / (now - start)));        }        System.out.print(" rows/s                \r");    }    /**     *  Method declaration     *     * @param  s     */    private static void print(String s) {        System.out.println(s);    }}

⌨️ 快捷键说明

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