主题:[原创]都来看看啊!我编写了一个基本功能比较完整的数据库系统!
我编写了一个模拟数据库功能的程序,基本功能已经实现
保存、打开数据库;
添加、删除表;
添加、删除行;
添加、删除列;
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]
保存、打开数据库;
添加、删除表;
添加、删除行;
添加、删除列;
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]