回 帖 发 新 帖 刷新版面

主题:[原创]都来看看啊!我编写了一个基本功能比较完整的数据库系统!

我编写了一个模拟数据库功能的程序,基本功能已经实现
保存、打开数据库;
添加、删除表;
添加、删除行;
添加、删除列;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

// 郭厚佐软件公司
// Title:ghzDatabase V1.0
// Author:Guohouzuo
// Description:Guohouzuo Database
// Version 1.0

class TableOrFieldNotFound
    extends Exception {
  public TableOrFieldNotFound() {

  }

  public TableOrFieldNotFound(String where) {
    super("(" + where + ")" + "Can't find this Field or Table!");
  }
}

class ConcurrentModificationException
    extends RuntimeException {

  public ConcurrentModificationException() {

  }

  public ConcurrentModificationException(String message) {
    super(message);
  }
}

interface Iterator {
  boolean hasNext();

  Object next();

  void remove();
}

interface ListIterator
    extends Iterator {
  boolean hasNext();

  Object next();

  boolean hasPrevious();

  Object previous();

  int nextIndex();

  int previousIndex();

  void remove();

  void set(Object o);

  void add(Object o);
}

class NoSuchElementException
    extends RuntimeException {

  public NoSuchElementException() {
    super();
  }

  public NoSuchElementException(String s) {
    super(s);
  }
}

class LinkedList {
  private transient LinkList header = new LinkList(null, null, null);

  private transient int size = 0;

  private transient int modCount = 0;

  public LinkedList() {
    header.next = header.previous = header;
  }

  public Object getFirst() {
    if (size == 0) {
      throw new NoSuchElementException();
    }

    return header.next.element;
  }

  public Object getLast() {
    if (size == 0) {
      throw new NoSuchElementException();
    }

    return header.previous.element;
  }

  public Object removeFirst() {
    Object first = header.next.element;
    remove(header.next);
    return first;
  }

  public Object removeLast() {
    Object last = header.previous.element;
    remove(header.previous);
    return last;
  }

  public boolean contains(Object o) {
    return indexOf(o) != -1;
  }

  public int size() {
    return size;
  }

  public boolean add(Object o) {
    LinkList newLinkList = new LinkList(o, header, header.previous);
    newLinkList.previous.next = newLinkList;
    newLinkList.next.previous = newLinkList;
    size++;
    modCount++;
    return true;
  }

  public boolean remove(Object o) {
    if (o == null) {
      for (LinkList e = header.next; e != header; e = e.next) {
        if (e.element == null) {
          if (e == header) {
            throw new NoSuchElementException();
          }
          e.previous.next = e.next;
          e.next.previous = e.previous;
          size--;
          modCount++;
          return true;
        }
      }
    }
    else {
      for (LinkList e = header.next; e != header; e = e.next) {
        if (o.equals(e.element)) {
          if (e == header) {
            throw new NoSuchElementException();
          }
          e.previous.next = e.next;
          e.next.previous = e.previous;
          size--;
          modCount++;
          return true;
        }
      }
    }
    return false;
  }

  public void clear() {
    modCount++;
    header.next = header.previous = header;
    size = 0;
  }

  public Object get(int index) {
    return LinkList(index).element;
  }

  public Object set(int index, Object element) {
    LinkList e = LinkList(index);
    Object oldVal = e.element;
    e.element = element;
    return oldVal;
  }

  public void add(int index, Object element) {
    LinkList newLinkList = new LinkList(element, (index == size ? header
                                                  : LinkList(index)),
                                        (index == size ? header :
                                         LinkList(index)).previous);
    newLinkList.previous.next = newLinkList;
    newLinkList.next.previous = newLinkList;
    size++;
    modCount++;
  }

  public Object remove(int index) {
    LinkList e = LinkList(index);
    if (e == header) {
      throw new NoSuchElementException();
    }

    e.previous.next = e.next;
    e.next.previous = e.previous;
    size--;
    modCount++;
    return e.element;
  }

  private LinkList LinkList(int index) {
    if (index < 0 || index >= size) {
      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
    }
    LinkList e = header;
    if (index < (size >> 1)) {
      for (int i = 0; i <= index; i++) {
        e = e.next;
      }
    }
    else {
      for (int i = size; i > index; i--) {
        e = e.previous;
      }
    }
    return e;
  }

  public int indexOf(Object o) {
    int index = 0;
    if (o == null) {
      for (LinkList e = header.next; e != header; e = e.next) {
        if (e.element == null) {
          return index;
        }
        index++;
      }
    }
    else {
      for (LinkList e = header.next; e != header; e = e.next) {
        if (o.equals(e.element)) {
          return index;
        }
        index++;
      }
    }
    return -1;
  }

  public int lastIndexOf(Object o) {
    int index = size;
    if (o == null) {
      for (LinkList e = header.previous; e != header; e = e.previous) {
        index--;
        if (e.element == null) {
          return index;
        }
      }
    }
    else {
      for (LinkList e = header.previous; e != header; e = e.previous) {
        index--;
        if (o.equals(e.element)) {
          return index;
        }
      }
    }
    return -1;
  }

  private static class LinkList {
    Object element;

    LinkList next;

    LinkList previous;

    LinkList(Object element, LinkList next, LinkList previous) {
      this.element = element;
      this.next = next;
      this.previous = previous;
    }
  }

  public Object[] toArray() {
    Object[] result = new Object[size];
    int i = 0;
    for (LinkList e = header.next; e != header; e = e.next) {
      result[i++] = e.element;
    }
    return result;
  }
}

class clsTable {
  private String name;

  LinkedList field;
  public clsTable() {

  }

  public clsTable(String tblname) {
    name = new String(tblname);
    field = new LinkedList();
    field.clear();
  }

  public String getName() {
    return name;
  }

  public void setName(String newtblname) {
    name = newtblname;
  }

  public void finalize() {
    name = null;
    field.clear();
    field = null;
    System.gc();
    Runtime.getRuntime().gc();
  }
}

class clsField {
  private String name;

  LinkedList value;
  public clsField() {

  }

  public clsField(String fldname) {
    name = new String(fldname);
    value = new LinkedList();
    value.clear();
  }

  public String getName() {
    return name;
  }

  public void setName(String newFieldName) {
    name = newFieldName;
  }

  public void finalize() {
    name = null;
    value.clear();
    value = null;
    System.gc();
    Runtime.getRuntime().gc();
  }
}

class clsDatabase {
  private String name;

  private String errorMessage;

  LinkedList table;

  public clsDatabase() {
    this.name = new String();
    this.errorMessage = new String();
    table = new LinkedList();
  }

  public String getName() {
    return name;
  }

  public void setName(String newDBName) {
    name = newDBName;
  }

  public int getTableCount() {
    return table.size();
  }

  public int getFieldCount(String tblname) {
    clsTable temp = null;
    int ret = 0;
    try {
      if (existsTable(tblname) == false) {
        errorMessage = "(getFieldCount)Can't find this table!";
      }
      else {
        temp = retTableRef(tblname);
        ret = temp.field.size();
      }
    }
    catch (Exception e) {
      errorMessage = "GetFieldCount Error!\nMessage:" + e;
    }
    finally {

    }
    return ret;
  }

  public void add_table(String tblname) {
    try {
      if (existsTable(tblname) == true || tblname.equals("")) {
        errorMessage = "Table Name Error!!!";
      }
      else {
        table.add(new clsTable(tblname));
      }
    }
    catch (Exception e) {
      errorMessage = "AddTable Error!\nMessage:" + e;
    }
    finally {

    }
  }

  public void del_table(String tblname) {
    try {
      clsTable temp;
      if (existsTable(tblname)) {
        for (int i = 0; i < table.size(); i++) {
          temp = (clsTable) table.get(i);
          if (temp.getName().equals(tblname)) {
            table.remove(i);
            break;
          }
        }
      }
      else {
        errorMessage = "(del_table)Can't find this table!";
      }
    }
    catch (Exception e) {
      errorMessage = "DelTable Error!\nMessage:" + e;
    }
    finally {

    }
  }

  public void add_field(String tblname, String fldname) {
    clsTable temp = null;
    try {
      if (existsTable(tblname) == true && !tblname.equals("")) {
        for (int i = 0; i < table.size(); i++) {
          temp = (clsTable) table.get(i);
          if (existsField(tblname, fldname) == true
              && !fldname.equals("")) {
            errorMessage = "Error Field Name!!!";
          }
          else {
            if (RowCountLargeThanOne(tblname) == false) {
              if (temp.getName().equals(tblname)) {
                temp.field.add(new clsField(fldname));
                table.set(i, temp);
                break;
              }
            }
            else {
              errorMessage =
                  "(AddField)About a has row table,you can not add a new field!";
            }
          }
        }
      }
      else {
        errorMessage = "(add_field)Can't find this table!";
      }
    }
    catch (Exception e) {
      errorMessage = "AddField Error!\nMessage:" + e;
    }
    finally {

    }
  }

  public void del_field(String tblname, String fldname) {
    try {
      clsTable temptbl;
      clsField tempfld;
      if (existsTable(tblname) && existsField(tblname, fldname)) {
        for (int k = 0; k < table.size(); k++) {
          temptbl = (clsTable) table.get(k);
          if (temptbl.getName().equals(tblname)) {
            for (int i = 0; i < temptbl.field.size(); i++) {
              tempfld = (clsField) temptbl.field.get(i);
              if (tempfld.getName().equals(fldname)) {
                tempfld.value.clear();
                temptbl.field.set(i, tempfld);
                temptbl.field.remove(i);
                table.set(k, temptbl);
                break;
              }
            }
          }
        }
      }
      else {
        errorMessage = "(del_field)Can't find this table or field!";
      }
    }
    catch (Exception e) {
      errorMessage = "DelField Error!\nMessage:" + e;
    }
    finally {

    }
  }

  public void add_row(String tblname, String value[]) {
    clsField tempfld;
    clsTable temptbl;
    try {
      for (int k = 0; k < table.size(); k++) {
        temptbl = (clsTable) table.get(k);
        if (temptbl.getName().equals(tblname)) {
          if (value.length == temptbl.field.size()) {
            for (int i = 0; i < temptbl.field.size(); i++) {
              tempfld = (clsField) temptbl.field.get(i);
              tempfld.value.add(value[i]);
              temptbl.field.set(i, tempfld);
            }
          }
          table.set(k, temptbl);
          break;
        }
      }
    }
    catch (Exception e) {
      errorMessage = "AddRow Error!\nMessage:" + e.getMessage();
    }
  }

  public void del_row(String tblname, String whereField, String whereValue) {
    clsField tempfld;
    clsTable temptbl;
    String out = new String();
    try {
      if (existsTable(tblname) == true
          && existsField(tblname, whereField) == true) {
        int tempRowIndex;
        for (int i = 0; i < table.size(); i++) {
          temptbl = (clsTable) table.get(i);
          if (temptbl.getName().equals(tblname)) {
            for (int j = 0; j < temptbl.field.size(); j++) {
              tempfld = (clsField) temptbl.field.get(j);
              if (tempfld.getName().equals(whereField)) {
                for (int m = 0; m < tempfld.value.size(); m++) {
                  String tempFieldValue = new String();
                  tempFieldValue = (String) tempfld.value
                      .get(m);
                  if (tempFieldValue.equals(whereValue)) {
                    tempRowIndex = m;
                    for (int k = 0; k < temptbl.field
                         .size(); k++) {
                      clsField tempdelrowfld;
                      tempdelrowfld = (clsField) temptbl.field
                          .get(k);
                      tempdelrowfld.value
                          .remove(tempRowIndex);
                      temptbl.field.set(k, tempdelrowfld);
                    }
                    table.set(i, temptbl);
                    break;
                  }
                }
              }
            }
          }
        }
      }
      else {
        errorMessage = "(del_row)Can't find this table or field!";
      }
    }
    catch (Exception e) {
      errorMessage = "DelRow Error!\nMessage:" + e;
    }
    finally {

    }
  }

  public String return_all() {
    String out = new String();
    try {
      out = out + "\n    Database:" + name + "    ";
      out = out + "\n==========START==========";
      for (int i = 0; i < table.size(); i++) {
        clsTable temp = (clsTable) table.get(i);
        clsField tempfld;
        out = out + "\n  Table:" + temp.getName();
        if (temp.field.size() != 0) {
          clsField tempfield = (clsField) temp.field.get(0);
          for (int m = 0; m < tempfield.value.size(); m++) {
            for (int j = 0; j < temp.field.size(); j++) {
              tempfld = (clsField) temp.field.get(j);
              out = out + "\n    " + tempfld.getName() + ":";
              out = out + (String) tempfld.value.get(m);
            }
            out = out + "\n------------------------------";
          }
        }
      }
      out = out + "\n===========END===========\n";
    }
    catch (Exception e) {

    }
    return out;
  }

  public void openDB(String filename) {
    try {
      table.clear();
      setName("");
      if (!filename.equals("") || !new File(filename).exists() == false) {
        File f = new File(filename);
        System.out.println("Loading Database.....");
        SAXParserFactory saxpf = SAXParserFactory.newInstance();
        SAXParser saxparser = saxpf.newSAXParser();
        SaxParser parser1 = new SaxParser(false);
        saxparser.parse(f, parser1);
        SaxParser parser2 = new SaxParser(true);
        saxparser.parse(f, parser2);
        System.out.println("Finish Load Database!");
      }
      else {
        errorMessage = "(OpenDB)File Name Error!";
      }
    }
    catch (Exception e) {
      errorMessage = "OpenDB Error!\nMessage:" + e + "";
    }
    finally {

    }
  }

  public void saveDB(String filename) {
    String headerString = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n";
    String root = "<Database dbname=\"" + getName() + "\">\n";
    String rootEnd = "</Database>\n";
    String element;
    String elementEnd;
    try {
      File file = new File(filename);
      if (file.exists() == false) {
        file.createNewFile();
      }
      FileWriter fwriter = new FileWriter(file);
      fwriter.write(headerString);
      fwriter.write(root);
      clsTable temptbl;
      clsField tempfld;
      for (int i = 0; i < table.size(); i++) {
        temptbl = retTableRef(i);
        fwriter.write("  <Table tblname=\"" + temptbl.getName()
                      + "\">\n");
        if (temptbl.field.size() > 0
            && ( (clsField) temptbl.field.get(0)).value.size() > 0) {
          for (int j = 0; j < temptbl.field.size(); j++) {
            tempfld = retFieldRef(i, j);
            fwriter.write("    <Field fldname=\"" + tempfld.getName()
                          + "\">\n");
            for (int k = 0; k < ( (clsField) temptbl.field.get(0)).value
                 .size(); k++) {
              fwriter.write("      <Value " + "val=\"" +
                            (tempfld.value.get(k)).toString() + "\" />\n");
            }
            fwriter.write("    </Field>\n");
          }
        }
        fwriter.write("  </Table>\n");
      }
      fwriter.write(rootEnd);
      fwriter.close();
    }
    catch (Exception e) {
      System.out.println("SaveDB Error!!!\nMessage:" + e);
    }
    finally {

    }
  }

  public void setTableName(String oldtblname, String newtblname) {
    try {
      clsTable temptbl;
      if (table.size() > 0 && existsTable(oldtblname)) {
        for (int i = 0; i < table.size(); i++) {
          temptbl = (clsTable) table.get(i);
          if (temptbl.getName() == oldtblname) {
            temptbl.setName(newtblname);
            table.set(i, temptbl);
            break;
          }
        }
      }
      else {
        errorMessage =
            "(setTableName)Can't find this table or haven't any table!";
      }
    }
    catch (Exception e) {
      errorMessage = "SetTableName Error!\nMessage:" + e;
    }
    finally {

    }
  }

  public String getFieldName(String tblname, int fldIndex) {
    String out = new String();
    try {
      clsField tempfld;
      tempfld = retFieldRef(tblname, fldIndex);
      out = tempfld.getName();
    }
    catch (Exception e) {
      errorMessage = "GetFieldName Error!\nMessage:" + e;
    }
    finally {

    }
    return out;
  }

  public boolean RowCountLargeThanOne(String tblname) {
    boolean out = false;
    try {
      clsTable temptbl;
      clsField tempfld;
      if (existsTable(tblname)) {
        temptbl = retTableRef(tblname);
        if (FieldCountLargeThanOne(tblname) == true) {
          tempfld = (clsField) temptbl.field.get(0);
          if (tempfld.value.size() == 0) {
            out = false;
          }
          else {
            out = true;
          }
        }
        else {
          out = false;
        }
      }
      else {
        errorMessage = "(RowCountLargeThanOne)Can't find this table!";
      }
    }
    catch (Exception e) {
      errorMessage = "RetRowCount Error!\nMessage:" + e;
    }
    finally {

    }
    return out;
  }

  public boolean TableCountLargeThanOne() {
    return (table.size() > 0);
  }

  public boolean FieldCountLargeThanOne(String tblname) {
    boolean out = false;
    try {
      if (existsTable(tblname) == true) {
        for (int i = 0; i < table.size(); i++) {
          clsTable temptbl = (clsTable) table.get(i);
          if (temptbl.field.size() > 0) {
            out = true;
            break;
          }
        }
      }
      else {
        errorMessage = "(FieldCountLargeThanOne)Can't find this table!";
      }
    }
    catch (Exception e) {
      errorMessage = "FieldCount Error!\nMessage:" + e;
    }
    return out;
  }

  public int retTableIndex(String tblname) {
    clsTable temptbl;
    int out = -1;
    try {
      if (existsTable(tblname)) {
        for (int i = 0; i < table.size(); i++) {
          temptbl = (clsTable) table.get(i);
          if (temptbl.getName().equals(tblname)) {
            out = i;
            break;
          }
        }
      }
      else {
        errorMessage = "(retTableIndex)Can't find this table!";
      }
    }
    catch (Exception e) {
      errorMessage = "getTableIndex Error!\nMessage:" + e;
    }
    return out;
  }

  public int retFieldIndex(String tblname, String fldname) {

    clsTable temptbl;
    int out = -1;
    try {
      temptbl = retTableRef(tblname);
      clsField tempfld;
      if (existsTable(tblname) && existsField(tblname, fldname)) {
        for (int i = 0; i < temptbl.field.size(); i++) {
          tempfld = (clsField) temptbl.field.get(i);
          if (tempfld.getName().equals(fldname)) {
            out = i;
            break;
          }
        }
      }
      else {
        errorMessage = "(getFieldIndex)Can't find this table or field!";
      }
    }
    catch (Exception e) {
      errorMessage = "getFieldIndex Error!\nMessage:" + e;
    }
    return out;
  }

  public String getErrorMessage() {
    if (!errorMessage.equals("") &&
        errorMessage.lastIndexOf("Index") == -1) {
      return errorMessage;
    }
    else {
      return (new String());
    }
  }

  public void setErrorMessageEmpty() {
    errorMessage = null;
    errorMessage = new String();
  }

  public boolean existsTable(String tblname) {
    clsTable temptbl = null;
    boolean out = false;
    try {
      for (int i = 0; i < table.size(); i++) {
        temptbl = (clsTable) table.get(i);
        if (temptbl.getName().equals(tblname)) {
          out = true;
          break;
        }
      }
    }
    catch (Exception e) {
      errorMessage = "FindTableExists Error!!!";
    }
    return out;
  }

  public boolean existsField(String tblname, String fldname) {
    clsTable temptbl = null;
    clsField tempfld = null;
    boolean out = false;
    try {
      for (int i = 0; i < table.size(); i++) {
        temptbl = (clsTable) table.get(i);
        if (temptbl.getName().equals(tblname)) {
          for (int j = 0; j < temptbl.field.size(); j++) {
            tempfld = (clsField) temptbl.field.get(j);
            if (tempfld.getName().equals(fldname)) {
              out = true;
              break;
            }
          }
        }
      }
    }
    catch (Exception e) {
      errorMessage = "FindFieldExists Error!!!";
    }
    return out;
  }

  public clsTable retTableRef(int tblIndex) {
    clsTable ret = null;
    try {
      if (tblIndex < table.size()) {
        ret = (clsTable) table.get(tblIndex);
      }
      else {
        errorMessage = "(retTableRef)Error Index!";
      }
    }
    catch (Exception e) {
      errorMessage = "getTableRef Error!!!\nMessage:" + e;
    }
    return ret;
  }

  public clsTable retTableRef(String tblname) {
    clsTable ret = null;
    try {
      if (existsTable(tblname)) {
        for (int i = 0; i < table.size(); i++) {
          ret = (clsTable) table.get(i);
          if (ret.getName().equals(tblname)) {
            break;
          }
        }
      }
      else {
        errorMessage = "(retTableRef)Can't find this table!";
      }
    }
    catch (Exception e) {
      errorMessage = "getTableRef Error!!!\nMessage:" + e;
    }
    return ret;
  }

  public clsField retFieldRef(String tblname, String fldname) {
    clsTable temptbl = null;
    clsField ret = null;
    try {
      if (existsField(tblname, fldname)) {
        temptbl = retTableRef(tblname);
        for (int i = 0; i < temptbl.field.size(); i++) {
          ret = (clsField) temptbl.field.get(i);
          if (ret.getName().equals(fldname)) {
            break;
          }
        }
      }
      else {
        errorMessage = "(retFieldRef)Can't find this field!";
      }
    }
    catch (Exception e) {
      errorMessage = "getFieldRef Error!\nMessage:" + e;
    }
    return ret;
  }

  public clsField retFieldRef(String tblname, int fldIndex) {
    clsTable temptbl = null;
    clsField ret = null;
    try {
      if (existsTable(tblname)) {
        temptbl = retTableRef(tblname);
        if (fldIndex < temptbl.field.size()) {
          ret = (clsField) temptbl.field.get(fldIndex);
        }
        else {
          errorMessage = "(retFieldRef)Error Index!";
        }
      }
      else {
        errorMessage = "(retFieldRef)Can't find this table!";
      }
    }
    catch (Exception e) {
      errorMessage = "getFieldRef Error!\nMessage:" + e;
    }
    return ret;
  }

  public clsField retFieldRef(int tblIndex, int fldIndex) {
    clsTable temptbl = null;
    clsField ret = null;
    try {
      if (tblIndex < table.size()) {
        temptbl = retTableRef(tblIndex);
        if (fldIndex < temptbl.field.size()) {
          ret = (clsField) temptbl.field.get(fldIndex);
        }
      }
      else {
        errorMessage = "(retFieldRef)Error Index!";
      }
    }
    catch (Exception e) {
      errorMessage = "getFieldRef Error!\nMessage:" + e;
    }
    return ret;
  }

  public Object[] query(String tblname, String fldname, String fldvalue) {
    LinkedList out = new LinkedList();
    clsField tempfld = retFieldRef(tblname, fldname);
    try {
      if (existsTable(tblname) == true
          && existsField(tblname, fldname) == true) {
        for (int i = 0; i < tempfld.value.size(); i++) {
          if ( ( (String) tempfld.value.get(i)).equals(fldvalue)) {
            out.add( (String) tempfld.value.get(i));
          }
        }
      }
      else {
        errorMessage = "(query)Can't find this table or field!";
      }
    }
    catch (Exception e) {
      errorMessage = "Query Error!\nMessage:" + e;
    }
    return out.toArray();
  }

  public class SaxParser
      extends DefaultHandler {
    private boolean fldstart = false;
    private boolean dbstart = false;
    private boolean tblstart = false;
    private String temptblname = new String();
    private LinkedList fldval = new LinkedList();
    private String tempfldname = new String();
    private boolean ParseValue = false;
    public SaxParser(boolean isParseValue) {
      ParseValue = isParseValue;
    }

    public void startElement(String nsuri, String localName, String qName,
                             Attributes attributes) {
      if (ParseValue == false) {
        if (qName.equals("Database")) {
          dbstart = true;
          setName(attributes.getValue(0));
          System.out.println("Find a Database."
                             + attributes.getValue(0));
        }
        else if (qName.equals("Table")) {
          tblstart = true;
          temptblname = attributes.getValue(0);
          add_table(attributes.getValue(0));
          System.out.println("Find a Table."
                             + attributes.getValue(0));
        }
        else if (qName.equals("Field")) {
          fldstart = true;
          add_field(temptblname, attributes.getValue(0));
          tempfldname = attributes.getValue(0);
          System.out.println("Find a Field."
                             + attributes.getValue(0));
        }
      }
      else {
        if (qName.equals("Table")) {
          tblstart = true;
          temptblname = attributes.getValue(0);
          System.out.println("Find a Table."
                             + attributes.getValue(0));
        }
        else if (qName.equals("Field")) {
          fldstart = true;
          tempfldname = attributes.getValue(0);
          System.out.println("Find a Field."
                             + attributes.getValue(0));
        }
        else if (qName.equals("Value")) {
          if (tblstart == true && fldstart == true) {
            clsTable temptbl = null;
            clsField tempfld = null;
            for (int i = 0; i < table.size(); i++) {
              temptbl = (clsTable) table.get(i);
              if (temptbl.getName().equals(temptblname)) {
                for (int j = 0; j < temptbl.field.size(); j++) {
                  tempfld = (clsField) temptbl.field.get(j);
                  if (tempfld.getName().equals(tempfldname)) {
                    tempfld.value.add(attributes.getValue(0));
                    table.set(i, temptbl);
                    break;
                  }
                }
                break;
              }
            }
            System.out.println("Find a Field Value.");
          }
        }
      }
    }

    public void characters(char[] cha, int start, int length) {

    }

    public void endElement(String nsuri, String localName, String qName) {
      if (qName.equals("Table")) {
        tblstart = false;
        temptblname = "";
        tempfldname = "";
      }
      if (qName.equals("Field")) {
        fldstart = false;
        tempfldname = "";
      }
    }
  }

  protected void finalize() {
    name = null;
    table.clear();
    table = null;
    errorMessage = null;
    System.gc();
    Runtime.getRuntime().gc();
    try {
      super.finalize();
    }
    catch (Throwable ex) {
    }
  }
}
public class ghzDB {
  clsDatabase db = new clsDatabase();
  public static void main(String args[]) {
    ghzDB ghzdb = new ghzDB();
    System.out.println("\n*****************************");
    System.out.println("*      ghzDatabase V1.0     *");
    System.out.println("*      Author:Guohouzuo     *");
    System.out.println("*****************************");
    System.out.println("\nQQ:394816808,E-mail:guohouzuo@vip.0451.com");
    System.out.println("\n\nYou can input 'help' to get help\n\n");
    String usrinp = new String();
    String temp = new String();
    BufferedReader input = new BufferedReader(new InputStreamReader(
        System.in));

    while (!usrinp.equals("exit")) {
      System.out.print("GHZDB->");
      try {
        usrinp = input.readLine();
        usrinp = usrinp.toLowerCase();
        if (usrinp.equals("exit")) {
          System.exit(0);
        }
        else if (usrinp.equals("modifydbname")) {
          System.out.println("Input new Database name:");
          temp = input.readLine();
          ghzdb.db.setName(temp);
        }
        else if (usrinp.equals("addtable")) {
          System.out.println("Input new table name:");
          temp = input.readLine();
          ghzdb.db.add_table(temp);
        }
        else if (usrinp.equals("deltable")) {
          System.out.println("Input delete table name:");
          temp = input.readLine();
          ghzdb.db.del_table(temp);
        }
        else if (usrinp.equals("addfield")) {
          System.out.println("Input table name:");
          temp = input.readLine();
          String temp2 = new String();
          System.out.println("Input new field name:");
          temp2 = input.readLine();
          ghzdb.db.add_field(temp, temp2);
        }
        else if (usrinp.equals("delfield")) {
          System.out.println("Input table name:");
          temp = input.readLine();
          String temp2 = new String();
          System.out.println("Input delete field name:");
          temp2 = input.readLine();
          ghzdb.db.del_field(temp, temp2);
        }
        else if (usrinp.equals("printall")) {
          System.out.println(ghzdb.db.return_all());
        }
        else if (usrinp.equals("modifytblname")) {
          System.out.println("Input old table name:");
          temp = input.readLine();
          String temp2 = new String();
        }
        else if (usrinp.equals("addrow")) {
          System.out.println("Input table name:");
          temp = input.readLine();
          String addrowval[] = new String[ghzdb.db
              .getFieldCount(temp)];
          for (int i = 0; i < ghzdb.db.getFieldCount(temp); i++) {
            System.out.println("Input field "
                               + ghzdb.db.getFieldName(temp, i) + " value:");
            addrowval[i] = input.readLine();
          }
          ghzdb.db.add_row(temp, addrowval);
        }
        else if (usrinp.equals("delrow")) {
          System.out.println("Input table name:");
          temp = input.readLine();
          String temp2, temp3;
          System.out.println("Input field name");
          temp2 = input.readLine();
          System.out.println("Input field value");
          temp3 = input.readLine();
          ghzdb.db.del_row(temp, temp2, temp3);
        }
        else if (usrinp.equals("savedb")) {
          System.out.println("Input file name:");
          temp = input.readLine();
          ghzdb.db.saveDB(temp);
        }
        else if (usrinp.equals("opendb")) {
          System.out.println("Input file name:");
          temp = input.readLine();
          File file = new File(temp);
          if (file.exists() == true) {
            ghzdb.db.openDB(temp);
          }
          else {
            System.out.println("Can't find this file!!!");
          }
        }
        else if (usrinp.equals("help")) {
          System.out.println("************************************");
          System.out.println("*          Command List            *");
          System.out.println("************************************");
          System.out.println("*modifydbname:modify database name *");
          System.out.println("************************************");
          System.out.println("*     addtable:add a table         *");
          System.out.println("************************************");
          System.out.println("*     deltable:delete a table      *");
          System.out.println("************************************");
          System.out.println("*      addfield:add a field        *");
          System.out.println("************************************");
          System.out.println("*     delfield:delete a field      *");
          System.out.println("************************************");
          System.out.println("*         addrow:add a row         *");
          System.out.println("************************************");
          System.out.println("*         delrow:del a row         *");
          System.out.println("************************************");
          System.out.println("* mdoifytblname:modify table name  *");
          System.out.println("************************************");
          System.out.println("*printall:print all table,field,row*");
          System.out.println("************************************");
          System.out.println("*   savedb:save database to file   *");
          System.out.println("************************************");
          System.out.println("*  opendb:open database from file  *");
          System.out.println("************************************");
          System.out.println("*  getmemfree:get memory free size *");
          System.out.println("************************************");
          System.out.println("\n");
        }
        else if (usrinp.equals("getmemfree")) {
          System.out.println(Runtime.getRuntime().freeMemory()
                             + " Program Memory free");
        }
        else if (usrinp.equals("")) {
        }
        else {
          System.out.println("Unknow Command!!!");
        }
      }
      catch (Exception e) {

      }
      finally {
        if (ghzdb.db.getErrorMessage().equals("")) {
          System.out.println("Finish this work!!!");
        }
        else if (ghzdb.db.getErrorMessage().indexOf("Index") == -1) {
          System.out.println(ghzdb.db.getErrorMessage());
          ghzdb.db.setErrorMessageEmpty();
        }
      }
    }
  }
}
[em1][em1][em1][em2][em2][em2][em11][em11][em11][em12][em12][em12]

回复列表 (共41个回复)

21 楼

你写的什么东西哦!注释也没有,那算是好东西吗?
说不准你是难写一堆代码
JAVA也没那么难学!……

22 楼

阿哦,不明白您的意思,目前注释还没写呢,不过我会尽快写的
引用:说不准你是难写一堆代码
什么意思呢?病句吧?

23 楼

数据库是基于XML的吧……
改用二进制流吧,读写XML相当于读写文本文件,速度很慢的;此外,XML的结构特点比较适合发展网状数据库(当然网状数据库采用二进制流等速度更快)。

另外,我好像没看见数据库索引……
做个索引,不然数据库大了以后是很慢的。

还有,要注意内存的使用。

以上谨个人建议,供楼主参考。
我没有Java的IDE,所以看楼主的代码的时候可能有些代码没看着。

24 楼

:)3Q!!!

25 楼

我当时也是因为着急(但不是交作业)没用二进制
曾经想用DOM、文件来保存、打开数据库,都没成功(记得耗费了1个月多时间),然后突然发现了《j2ee参考大全》上边的一个例子,所以改成这个样子了:)
您qq多少?请教几个问题。
我这个数据库本来就非常慢,LinkedList类的get函数就是顺序查找,效率就非常慢,再加上各个函数基本都搜索了一遍每一个表,速度更慢,所以。。。加上索引也没用,到目前为止,我还没发现太好的办法

26 楼

有很多方面都是可以优化的。

我的联系信息都写在“个人资料”里面了。不过我现在开学了,不能经常上网。

27 楼

很厉害啊,楼主很了不起啊!!!不过我没有学过JAVA ,还是看不懂???

28 楼

很厉害啊,楼主很了不起啊!!!不过我没有学过JAVA ,还是看不懂???

29 楼

还是不看的好

30 楼

还是不看的好

我来回复

您尚未登录,请登录后再回复。点此登录或注册