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

📄 test.java

📁 一个简单的数据库
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package test;import interpreterManager.*;import stringManager.*;import fileManager.*;import java.io.*;import apiManager.*;import bufferedManager.*;import database.*;import catalogManager.*;import indexManager.*;import java.util.ArrayList;/** * * @author outlaw */public class Test {    public static void test1() throws Exception    {            /*Interpreter.createDataBase("");            for(int i=0;i<100;i++){                APIManager.createTable("table"+i,new String[1],new String[1],0);            }*/            Interpreter.dispatchInstr("create database outlaw (  );");            Interpreter.dispatchInstr("execfile D:/课程/数据库系统设计/DataBase/createtb.txt");                   Interpreter.dispatchInstr("insert into student values ('12345678','wy',22,'M');");              /*System.out.println("-----------------------------------------");            DataBaseData db=CatalogManager.getCurDB();            DataBaseData db=FileManager.readInDB("D:/workspace/outlaw/outlaw.db");            ArrayList<TableData> tables=db.getTables();            TableData table=null;            System.out.println(db.getPath()+db.getName()+"   table size:  "+tables.size());                        for(int i=0;i<tables.size();i++)            {                table=tables.get(i);                //table=FileManager.readInTable("D:/workspace/outlaw/tables/student.tbl");                System.out.println(table.getPath()+table.getName());                for(int j=0;j<table.getAttributes().size();j++)                {                    Attribute attrib=table.getAttributes().get(j);                    System.out.print(attrib.getName()+":   "+attrib.getType());                    System.out.println((attrib.isKey()?"key":"")+(attrib.isUnique()?"unique":""));                }            }*/             }    public static void test2() throws Exception    {        Interpreter.dispatchInstr("create database outlaw1 (  );");        Interpreter.dispatchInstr("create database outlaw2 (  );");        Interpreter.dispatchInstr("create database outlaw3 (  );");        FileManager.writeOutDataBaseInfo();    }    public static void test3() throws Exception    {        CatalogManager.initDBInfo();        TableData tb=null;        DataBaseData db=null;        String name;        for(int i=0;i<CatalogManager.getTotalDBnumber();i++)        {            name=CatalogManager.getAllDBName().get(i);            db=FileManager.readInDBByName(name);            System.out.println(db.getName());            System.out.println("-------------------------------------------------");            for(int j=0;j<db.getTBNumber();j++)            {                tb=db.getTables().get(j);                for(int k=0;k<tb.getAttributes().size();k++)                {                    System.out.println(tb.getAttributes().get(k).getName());                }            }        }    }    public static void test6() throws Exception    {         APIManager.open();         for(int t=0;t<CatalogManager.getCurDB().getTables().size();t++)         {             TableData tb=CatalogManager.getCurDB().getTables().get(t);             FileInputStream fis=new FileInputStream(tb.getFullName()+".tbl");             for(int k=0;k<1;k++)             {                 byte[] data=new byte[Block.BLOCKSIZE];                 fis.read(data);                 Block block=new Block(data,tb.getName(),0);                 System.out.println(block.getTupeNumber());                 Tuple[] tuples=block.getTuples();                 for(int i=0;i<tuples.length;i++)                 {                     if(tuples[i]!=null)                     {                         String[] values=tuples[i].getValues();                         for(int j=0;j<values.length;j++)                             System.out.println(values[j]);                         System.out.println("----------tuple---------------------------");                     }                 }             }             System.out.println("----------------------table---------------------");         }         APIManager.close(null);    }    public static void test5() throws Exception    {        FileInputStream fis=new FileInputStream("D:/workspace/outlaw/tables/student.tbl");        int data;        while((data=fis.read())!=-1)        {            System.out.println(data);        }    }    public static void test7() throws Exception    {        //Interpreter.dispatchInstr("insert into student values ('12345678','wy',22,'M');");                Tuple tuple=new Tuple(new String[]{"12345678","wy","22","M"});        /*String[] values=tuple.getValues();        for(int i=0;i<values.length;i++)            System.out.println(values[i]);        */        FileOutputStream fos=new FileOutputStream("d:/test");        fos.write(tuple.toBytes(new String[]{"char8","char16","int","char1"}));        fos.close();    }    public static void test8() throws Exception    {        FileInputStream fis=new FileInputStream("d:/test");        byte[] data=new byte[57];        fis.read(data);        Tuple tuple=new Tuple(data,new String[]{"char8","char16","int","char1"});        String[] values=tuple.getValues();        for(int i=0;i<values.length;i++)            System.out.println(values[i]);          fis.close();    }    public static void init() throws Exception    {        APIManager.open();    }    public static void close() throws Exception    {        APIManager.close(null);    }    public static void test11() throws Exception    {        init();        FileInputStream fis=new FileInputStream("D:/workspace/outlaw/tables/ageIndex.ind");        ObjectInputStream ois=new ObjectInputStream(fis);        Index index=(Index) ois.readObject();        int x=22;        Comparable key=x;        Bucket bucket=index.find(key);        System.out.println(bucket);        close();    }    public static void test12() throws Exception    {        init();        DataBaseData db=CatalogManager.getCurDB();        System.out.println(db.getFullName());        System.out.println("------------------------------------");        ArrayList<TableData> tables=db.getTables();        for(int i=0;i<tables.size();i++)        {            ArrayList<IndexTag> indices=tables.get(i).getIndices();            for(int j=0;j<indices.size();j++)            {                IndexTag tag=indices.get(j);                System.out.println(tag.getAttribName());            }        }        close();    }    public static void test13() throws Exception    {        init();        Interpreter.dispatchInstr("drop table student1;");        close();    }    public static void test14() throws Exception    {        init();        DataBaseData db=CatalogManager.getCurDB();        ArrayList<TableData> tables=db.getTables();        for(int i=0;i<tables.size();i++)        {            System.out.println(tables.get(i).getName());        }        close();    }    public static void test9() throws Exception    {        init();        //Interpreter.dispatchInstr("create index ageIndex on student1 (sage);");        Interpreter.dispatchInstr("create index nameIndex on student1 (sname);");        close();    }    public static void test10() throws Exception    {        init();        Tuple tuple=APIManager.selectTuple("student1", "sage", "123");        System.out.println(tuple);        close();    }    public static void test15() throws Exception    {        init();        Interpreter.dispatchInstr("delete from student2 where sage=7;");        //Interpreter.dispatchInstr("insert into student1 values ('7654321','ev',98,'w');");        //ArrayList<Tuple> tuples=StringManager.operaSelect("select * from student2 where sno='sa';");        //for(int i=0;i<tuples.size();i++)           // System.out.println(tuples.get(i));        //close();    }    public static void test4() throws Exception    {        APIManager.open();           Interpreter.dispatchInstr("create database outlaw (  );");        Interpreter.dispatchInstr("execfile D:/课程/数据库系统设计/DataBase/createtb.txt;");               Interpreter.dispatchInstr("insert into student1 values ('12345678','wy',22,'M');");        Interpreter.dispatchInstr("insert into student1 values ('1212','xd',123,'x');");        Interpreter.dispatchInstr("insert into student2 values ('sa','ii',7,'x');");        Interpreter.dispatchInstr("insert into student2 values ('assss','jj',8,'y');");        Interpreter.dispatchInstr("insert into student2 values ('dddd','kk',9,'z');");        APIManager.close(null);         //FileManager.writeOutDataBaseInfo();    }      public static void test16() throws Exception    {        init();        Interpreter.dispatchInstr("delete from student2 where sname='kk';");        close();    }    public static void test17() throws Exception    {        init();       // ArrayList<Tuple> tuples=StringManager.operaSelect("select * from student2 where sgender='z' ;");       // for(int i=0;i<tuples.size();i++)           // System.out.println(tuples.get(i));        //close();        }    public static void test18() throws Exception    {        init();        Interpreter.dispatchInstr("create table t1( id int ,name char(20) unique,age int ,salary float,primary key(id));");        close();    }    public static void test19() throws Exception    {        init();        //Interpreter.dispatchInstr("insert into t1 values(1,'Jim',20,2000.00);");        //Interpreter.dispatchInstr("insert into t1 values(2,'Kate',24,1800.00);");        //Interpreter.dispatchInstr("insert into t1 values(3,'John',34,4000.00);");        Interpreter.dispatchInstr("insert into t1 values(11,'John2',34,4000.00);");        /*Interpreter.dispatchInstr("insert into t1 values(4,'Marry',20,3000.00);");        Interpreter.dispatchInstr("insert into t1 values(5,'Tom',24,1850.00);");                Interpreter.dispatchInstr("insert into t1 values(6,'Queen',28,24000.00);");        Interpreter.dispatchInstr("insert into t1 values(7,'Porry',17,1000.00);");        Interpreter.dispatchInstr("insert into t1 values(8,'Green',24,8000.00);");        Interpreter.dispatchInstr("insert into t1 values(8,'Jim',20,4000.00);");        Interpreter.dispatchInstr("insert into t1 values(9,'Green',22,4000.00);");        Interpreter.dispatchInstr("create index name on t1(name);");        */        close();    }    public static void test20() throws Exception    {        init();       // ArrayList<Tuple> tuples=StringManager.operaSelect("select * from t1 where name<>'Jim';");       // for(int i=0;i<tuples.size();i++)           // System.out.println(tuples.get(i));       // close();    }    public static void test21() throws Exception    {        init();        Interpreter.dispatchInstr("create table author (a_id int, a_name char(100), primary key(a_name));");        close();    }    public static void test22() throws Exception     {        init();        Interpreter.dispatchInstr("select * from author;");        close();    }    public static void main(String[] args){        try{            test22();        }catch(Exception ex){            System.out.println(ex);            ex.printStackTrace();        }    }}

⌨️ 快捷键说明

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