基于网络小型文件型数据管理系统的构建功能具体如下:
(1)支持定长记录。
(2)支持使用标准的SQL 语句进行数据操纵,例如:INSERT、SELECT、DELETE 和UPDATE。
(3)支持条件范围查询和多条件查询。
支持 DELETE 和 UPDATE 的批量处理。
该系统实现小型关系数据库管理和文件管理的基本操作。包括数据库的创建、数据库中文件的基本操作,增加、删除、修改数据,对SQL语句Create database、Create table、Update table、Delete、Insert、Select、Drop database、Alter等的实现。
DataBase.java
package DataBase;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
public class DataBase {
public String databasename = "DBS";
public String prefix = "DataBase\\";
public File child;
public File record;
public Table table;
public Result result;
public DataBase() {}
public DataBase(Result result) {
this.result = result;
try {
File dbn = new File("DataBase\\" + "DataBaseName.txt");
if (!dbn.exists()) {
RandomAccessFile random = new RandomAccessFile("DataBase\\" + "DataBaseName.txt", "rw");
random.writeInt(0);
random.writeBytes("\r\n");
random.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getDataBase() {
StringBuffer strdbn = new StringBuffer("");
try {
int x = 0;
String s = null;
RandomAccessFile ran = new RandomAccessFile("DataBase\\" + "DataBaseName.txt", "rw");
x = ran.readInt();
ran.readLine();
strdbn.append(x);
strdbn.append("|");
while ((s = ran.readLine()) != null){
strdbn.append(s);
strdbn.append("|");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strdbn.toString();
}
public void optDataBase(String databasename) { //构建数据库对象
this.databasename = databasename;
child = new File(prefix + this.databasename);
record = new File(prefix + this.databasename + "\\" + "Record.txt");
if(!child.exists()) {
//若该数据库不存在,执行相应操作
result.result = "数据库不存在";
}
}
public void newDataBase(String databasename) { //创建新数据库
child = new File(prefix + databasename);
record = new File(prefix + databasename + "\\" + "Record.txt");
if(child.exists()) {
//若数据库存在,执行相应操作
result.result = "数据库已存在";
return;
}
else {
child.mkdir();
try {
RandomAccessFile ran = new RandomAccessFile(prefix + databasename + "\\" + "Record.txt","rw");
ran.close();
RandomAccessFile rans = new RandomAccessFile(prefix + databasename + "\\" + "TableName.txt","rw");
rans.writeInt(0);
rans.writeBytes("\r\n");
rans.close();
RandomAccessFile random = new RandomAccessFile(prefix + "DataBaseName.txt", "rw");
random.seek(random.length());
random.writeBytes(databasename + "\r\n");
random.seek(0);
int x = random.readInt();
random.seek(0);
random.writeInt(x + 1);
random.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
result.result = "操作成功";
}
public void deleteDataBase(String databasename) { //删除数据库
child = new File(prefix + databasename);
if(child.exists()) {
File f = null;
f = new File(prefix + databasename + "\\" + "Record.txt");
if (f.exists()) {
System.out.println("删除Record");
f.delete();
}
RandomAccessFile ran;
try {
ran = new RandomAccessFile(prefix + databasename + "\\" + "TableName.txt", "rw");
ran.readLine();
String ss = null;
while((ss = ran.readLine()) != null) {
f = new File(prefix + databasename+ "\\" + ss + ".txt");
if (f.exists()) {
f.delete();
}
}
ran.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
f = new File(prefix + databasename + "\\" + "TableName.txt");
if (f.exists()) {
f.delete();
}
child.delete();
try {
RandomAccessFile random = new RandomAccessFile(prefix + "DataBaseName.txt", "rw");
random.seek(0);
int x = random.readInt();
random.seek(0);
random.writeInt(x - 1);
random.readLine();
random.seek(0);
String s = null;
StringBuffer str = new StringBuffer("");
while((s = random.readLine())!=null) {
if (!s.equals(databasename)) {
str.append(s + "\r\n");
}
}
random.close();
FileWriter fw = new FileWriter(prefix + "DataBaseName.txt");
fw.write(str.toString());
fw.close();
result.result = "数据库已删除";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
//若数据库不存在,执行相应操作
result.result = "数据库不存在";
}
}
public String judge(String str) {
int start = 0;
int end = 0;
StringBuffer stb = new StringBuffer("");
str = str.replace("=", " ");
str = str.replace("'", " ");
str = str.replace("\r", " ");
str = str.replace("\n", " ");
str = str.replace("\t", " ");
while (start < str.length()) {
for (int i = start; i < str.length(); i++) {
if (i == str.length() - 1) {
start = i;
break;
}
if (str.charAt(i) == ";".charAt(0)) {
start = i;
break;
}
if(str.charAt(i) != " ".charAt(0)) {
if (str.charAt(i) == "(".charAt(0)) {
int count = 0;
for (int j = i; j < str.length(); j++){
if (str.charAt(j) == "(".charAt(0)) {
count++;
}
if (str.charAt(j) == ")".charAt(0)) {
count--;
}
if (count == 0) {
stb.append(str.substring(i, j + 1));
//stb.append("|");
start = j + 1;
break;
}
}
break;
}
start = i;
break;
}
}
if (start != 0) {
if (str.charAt(start) == ")".charAt(0)) {
start ++;
continue;
}
}
if (start == str.length() - 1) {
break;
}
if (str.charAt(start) == ";".charAt(0)) {
break;
}
for(int j = start; j < str.length(); j++) {
if (str.charAt(j) == " ".charAt(0)) {
stb.append(str.substring(start, j));
stb.append("|");
start = j ;
break;
}
end = j;
}
if (end == str.length() - 1) {
stb.append(str.substring(start, end));
stb.append("|");
break;
}
}
return stb.toString();
}
public void option(String str) {
String opt1 = null;
String opt2 = null;
int start = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
opt1 = str.substring(0, i);
start = i + 1;
break;
}
}
if (opt1.toUpperCase().equals("CREATE")) {
System.out.println(opt1);
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
opt2 = str.substring(start, i);
start = i + 1;
break;
}
}
System.out.println(opt2);
if (opt2.toUpperCase().equals("DATABASE")) { //DATABASE
String DBName = null;
DBName = str.substring(start, str.length() - 1);
System.out.println(DBName);
this.newDataBase(DBName);
System.out.println(this.result.result);
}
else { //TABLE
String TableName = null;
String attri = null;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
TableName = str.substring(start, i) + ".txt";
start = i + 1;
break;
}
}
attri = str.substring(start, str.length() - 1);
attri = attri.substring(1, attri.length());
System.out.println(TableName);
System.out.println(attri);
boolean flag1 = false;//有无该表 有表true 无表false
this.table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName);
if (flag1) {
this.result.result = "该表已存在";
}
else {
table.newTable(attri);
}
}
return;
}
if (opt1.toUpperCase().equals("DROP")) {
System.out.println(opt1);
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
opt2 = str.substring(start, i);
start = i + 1;
break;
}
}
System.out.println(opt2);
if (opt2.toUpperCase().equals("DATABASE")) { //DATABASE
String DBName = null;
DBName = str.substring(start, str.length() - 1);
System.out.println(DBName);
//this.optDataBase(DBName);
this.deleteDataBase(DBName);
}
else { //TABLE
String TableName = null;
TableName = str.substring(start, str.length() - 1) + ".txt";
System.out.println(TableName);
boolean flag1 = false;//有无该表 有表true 无表false
this.table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName );
if (flag1) {
this.table.deleteTable();
}
else {
this.result.result = "该表不存在";
}
}
return;
}
if (opt1.toUpperCase().equals("ALTER")) {
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
String TableName = null;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
TableName = str.substring(start, i) + ".txt";
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
opt2 = str.substring(start, i);
start = i + 1;
break;
}
}
System.out.println(TableName);
System.out.println(opt2);
if (opt2.toUpperCase().equals("ADD")) {
String attri = null;
String type = null;
String key = null;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
attri = str.substring(start, i);
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
type = str.substring(start, i);
start = i + 1;
if(i == str.length() - 1) {
key = "NULL";
start = i + 1;
}
break;
}
}
if (start < str.length()) {
key = str.substring(start, str.length() - 1);
}
System.out.println(attri);
System.out.println(type);
System.out.println(key);
boolean flag1 = false;
boolean flag2 = false;
table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName);
flag2 = table.existTuple();
if (flag1) {
if(flag2) {
}
else {
table.addAttribute(attri, type, key);
}
}
else {
result.result = "该表不存在";
}
}
else {
String attri;
attri = str.substring(start, str.length() - 1);
System.out.println(attri);
boolean flag1 = false;
boolean flag2 = false;
table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName);
flag2 = table.existTuple();
if (flag1) {
if(flag2) {
}
else {
table.deleteAttribute(attri);
}
}
else {
result.result = "该表不存在";
}
}
return;
}
if (opt1.toUpperCase().equals("INSERT")) {
String TableName = null;
String attris = null;
String values = null;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
TableName = str.substring(start, i) + ".txt";
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
attris = str.substring(start + 1, i - 1).replace(" ", "");
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
values = str.substring(start + 1, str.length() - 1).replace(" ", "");
System.out.println(TableName);
System.out.println(attris);
System.out.println(values);
boolean flag1 = false;
table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName);
if (flag1) {
table.optTuple();
table.insertTuple(attris, values);
}
else {
result.result = "该表不存在";
}
return;
}
if (opt1.toUpperCase().equals("UPDATE")) {
String TableName = null;
String attri1 = null;
String value1 = null;
String attri2 = null;
String value2 = null;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
TableName = str.substring(start, i) + ".txt";
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
attri1 = str.substring(start, i);
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
value1 = str.substring(start, i);
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
attri2 = str.substring(start, i);
start = i + 1;
break;
}
}
value2 = str.substring(start, str.length() - 1);
System.out.println(TableName);
System.out.println(attri1);
System.out.println(value1);
System.out.println(attri2);
System.out.println(value2);
boolean flag1 = false;
boolean flag2 = false;
table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName);
flag2 = table.existTuple();
if (flag1) {
if(flag2) {
table.optTuple();
table.updateTuple(attri1, value1, attri2, value2);
}
else {
}
}
else {
result.result = "该表不存在";
}
return;
}
if (opt1.toUpperCase().equals("SELECT")) {
String TableName = null;
String attri = null;
String value = null;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
TableName = str.substring(start, i) + ".txt";
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
attri = str.substring(start, i);
start = i + 1;
break;
}
}
value = str.substring(start, str.length() - 1);
System.out.println(TableName);
System.out.println(attri);
System.out.println(value);
boolean flag1 = false;
boolean flag2 = false;
table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName);
flag2 = table.existTuple();
if (flag1) {
if(flag2) {
table.optTuple();
table.selectTuple(attri, value);
}
else {
}
}
else {
result.result = "该表不存在";
}
return;
}
if (opt1.toUpperCase().equals("DELETE")) {
String TableName = null;
String attri = null;
String value = null;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
TableName = str.substring(start, i) + ".txt";
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
start = i + 1;
break;
}
}
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == "|".charAt(0)) {
attri = str.substring(start, i);
start = i + 1;
break;
}
}
value = str.substring(start, str.length() - 1);
System.out.println(TableName);
System.out.println(attri);
System.out.println(value);
boolean flag1 = false;
boolean flag2 = false;
table = new Table(this.result);
table.tablename = TableName;
flag1 = table.existTable(prefix + databasename + "\\", TableName);
flag2 = table.existTuple();
if (flag1) {
if(flag2) {
table.optTuple();
table.deleteTuple(attri, value);
}
else {
}
}
else {
result.result = "该表不存在";
}
return;
}
result.result = "输入错误";
return;
}
}
HashMaps.java
package DataBase;
import java.util.HashMap;
import java.util.Map;
public class HashMaps {
Map<String, String> attriandtype = null;
Map<String, Integer> attriandlength = null;
Map<String, Integer> attriandstart = null;
Map<String, Boolean> attriandkey = null;
Map<String, Boolean> attriandnull = null;
public HashMaps(){ //初始化
attriandtype = new HashMap<String, String>();
attriandlength = new HashMap<String, Integer>();
attriandstart = new HashMap<String, Integer>();
attriandkey = new HashMap<String, Boolean>();
attriandnull = new HashMap<String, Boolean>();
}
public void putAttriAndType(String key, String value){ //添加新成员
attriandtype.put(key, value);
}
public void putAttriAndLength(String key, int value){ //添加新成员
Integer v = new Integer(value);
attriandlength.put(key, v);
}
public void putAttriAndStart(String key, int value){ //添加新成员
Integer v = new Integer(value);
attriandstart.put(key, v);
}
public void putAttriAndkey(String key, boolean value){ //添加新成员
attriandkey.put(key, value);
}
public void putAttriAndnull(String key, boolean value){ //添加新成员
attriandnull.put(key, value);
}
public String getType(String key){ //通过key获取value
return attriandtype.get(key);
}
public int getLength(String key){ //通过key获取value
return attriandlength.get(key).intValue();
}
public int getStart(String key){ //通过key获取value
return attriandstart.get(key).intValue();
}
public boolean getkey(String key){ //通过key获取value
return attriandkey.get(key);
}
public boolean getnull(String key){ //通过key获取value
return attriandnull.get(key);
}
}
judge.java
package DataBase;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import java.awt.*;
//import test.Server;
//import java.lang.*;
public class judge {
public static String judge(String str){
String str1=new String();
String str2=new String();
String str3=new String();
String str4=new String();
String str5 = new String();
String str6 = new String();
String str7 = new String();
String str8 = new String();
String str9 = new String();
String str10 = new String();
String str11 = new String();
String str12 = new String();
String str13 = new String();
String str14 = new String();
String str15 = new String();
int x=0;
int y=0;
int z=0;
int w=0;
String[] sourceStrArray = str.split("\r\n");//分割出来的字符数组
for (int i = 0; i < sourceStrArray.length; i++) {
//System.out.println(sourceStrArray[i]);
str4=str4+sourceStrArray[i];
}
str14 = str4.substring(str4.indexOf("("));
//System.out.println(str14);
// str8=str4.substring(0,str4.indexOf("("));
// System.out.print(str8);
String[] sourceStrArray2 = str4.split(" ");
for(int i =0;i<sourceStrArray2.length;i++) {
str5 = str5+sourceStrArray2[i];
}
String[] sourceStrArray3 = str5.split("\t");
//sourceStrArray3[0] = sourceStrArray3[0].toUpperCase();
for(int i =0;i<sourceStrArray3.length;i++) {
str6 = str6+sourceStrArray3[i];
}
String[] sourceStrArray4 = str6.split("'");
// sourceStrArray4[0] = sourceStrArray4[0].toUpperCase();
for(int i =0;i<sourceStrArray4.length;i++) {
str7 = str7+sourceStrArray4[i];
}
String[] sourceStrArray5 = str7.split("=");
sourceStrArray4[0] = sourceStrArray4[0].toUpperCase();
for(int i = 0;i<sourceStrArray5.length;i++) {
str3 = str3+sourceStrArray5[i];
}
str1= sourceStrArray2[0];
str2= sourceStrArray2[1];
if(str2.equals("DATABASE")) {
if(str1.equals("CREATE")) {//创建数据库
str3="|"+str3.substring(14);
System.out.println(str3);
}
if(str1.equals("DROP")) {//删除数据库
str3="|"+str3.substring(12);
System.out.println(str3);
}
}
if(str2.equals("TABLE")) {//创建表格
if(str1.equals("CREATE")) {
x=str3.indexOf("TABLE");
y=str14.indexOf("(");
z=str3.indexOf("(");
str3="|"+str3.substring(x+5,z)+"|"+str14.substring(y);
System.out.println(str3);
}
if(str1.equals("ALTER")) {//修改表
str9= sourceStrArray2[3];
if(str9.equals("ADD")) {
x=str3.indexOf("ADD");
str3="|"+str3.substring(10,x)+"|"+str3.substring(x+3);
System.out.println(str3);}
if(str9.equals("DROP")) {
x=str3.indexOf("DROP");
str3="|"+str3.substring(10,x)+"|"+str3.substring(x+4);
System.out.println(str3);
}
}
if(str1.equals("DROP")) {//删除表
str3="|"+str3.substring(9);
System.out.println(str3);
}
}
if(str1.equals("INSERT")) {//插入记录
x=str3.indexOf("VALUES");
y=str3.indexOf("INTO");
z=str3.indexOf("(");
str3="|"+str3.substring(y+4,z)+"|"+str3.substring(z,x)+"|"+str3.substring(x+6);
System.out.println(str3);
}
if(str1.equals("UPDATE")) {//修改记录
str11=sourceStrArray2[2];
str12=sourceStrArray2[5];
x=str3.indexOf("SET");
y=str3.indexOf("WHERE");
z=str3.indexOf(str11);
w=str3.indexOf(str12);
str3="|"+str3.substring(6,x)+"|"+str3.substring(x+3,z+str11.length())+"|"+str3.substring(z+str11.length(),y)+"|"+str3.substring(y+5,w+str12.length())+"|"+str3.substring(w+str12.length());
System.out.println(str3);
}
if(str1.equals("SELECT")) {//查找记录
str10= sourceStrArray2[4];
x=str3.indexOf("FROM");
y=str3.indexOf("WHERE");
z=str3.indexOf(str10);
str3="|"+str3.substring(6,x)+"|"+str3.substring(x+4,y)+"|"+str3.substring(y+str10.length(),z+str10.length())+"|"+str3.substring(y+10);
System.out.println(str3);
}
if(str1.equals("DELETE")) {//删除记录
str13=sourceStrArray2[3];
x=str3.indexOf("WHERE");
y=str3.indexOf(str13);
str3="|"+str3.substring(10,x)+"|"+str3.substring(x+5,y+str13.length())+"|"+str3.substring(y+str13.length());
System.out.println(str3);
}
if(!str2.equals("TABLE")&&!str2.equals("DATABASE")&&str1.equals("INSERT")&&str1.equals("UPDATE")&&str1.equals("SELECT")&&str1.equals("DELETE")) {
System.out.println("无效输入!");
}
return str3;
}
public static void main(String[] args) {
String sourceStr1 = "INSERT INTO Student (Sno, Sname, Ssex, Sdept, Sage) VALUES ('201215128', '陈东', '男', 'IS', 18);";
String str1 = judge(sourceStr1);
}
}
RandomAccessFiles.java
package DataBase;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
public class RandomAccessFiles {
public String fileprefix; //数据库文件路径前缀,例:"DataBase\\DBS\\"
public String filename; //文件名
public RandomAccessFile raf;
public HashMaps hashmap; //名称-类型, 名称-长度,名称-起始地址
public boolean flag;
public String[] attributes; //属性名称
public String[] types; //属性类型
public int[] length; //属性定义的字节长度
boolean[] key; //关键字
boolean[] isnull; //是否可以为空
public int num = 0; //属性个数
public int sum = 0; //所有属性字节总数
public String keyword;
public void RandomAccessTable(String fileprefix, String filename) throws IOException{ //操作Table文件
this.filename = filename;
this.fileprefix = fileprefix;
raf = new RandomAccessFile(fileprefix + filename, "rw");
RandomAccessFile rafs = new RandomAccessFile(fileprefix + "Record.txt", "rw");
//RandomAccessFile rafs = new RandomAccessFile("DataBase\\DBS\\Record.txt", "rw");
hashmap = new HashMaps();
byte[] m = new byte[20];
byte[] n = new byte[10];
while(true) {
String str= rafs.readLine().trim();
if (str.equals(filename.substring(0, filename.length() - 4))) {
flag = rafs.readBoolean();
rafs.readLine();
num = rafs.readInt();
rafs.readLine();
attributes = new String [num];
types = new String [num];
length = new int [num];
key = new boolean [num];
isnull = new boolean [num];
for (int i = 0; i < num; i++) {
rafs.read(m, 0, 20);
attributes[i] = new String(m,"GB2312").trim();
}
rafs.readLine();
for (int i = 0; i < num; i++) {
rafs.read(n, 0, 10);
types[i] = new String(n,"GB2312").trim();
}
rafs.readLine();
for (int i = 0; i < num; i++) {
length[i] = rafs.readInt();
}
rafs.readLine();
for (int i = 0; i < num; i++) {
key[i] = rafs.readBoolean();
if (key[i]) {
keyword = attributes[i];
System.out.println(keyword);
}
}
rafs.readLine();
for (int i = 0; i < num; i++) {
isnull[i] = rafs.readBoolean();
}
rafs.readLine();
sum = 0;
for (int i = 0; i < num; i++) {
hashmap.putAttriAndType(attributes[i], types[i]);
hashmap.putAttriAndLength(attributes[i], length[i]);
hashmap.putAttriAndStart(attributes[i], sum);
hashmap.putAttriAndkey(attributes[i], key[i]);
hashmap.putAttriAndnull(attributes[i], isnull[i]);
sum += length[i];
}
sum += 2;
break;
}
}
}
public String readALine() { //读取一行记录,调用之前,文件指针必须在一行的开始位置
StringBuffer str = new StringBuffer("");
for(int i = 0; i < num; i++) {
if(types[i].equals("CHAR")) {
int x = hashmap.attriandlength.get(attributes[i]);
byte[] b = new byte[x];// = new byte[x]
try {
raf.read(b, 0, x);
str.append(new String(b, "GB2312").trim() + "\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("INT")) {
try {
str.append(raf.readInt());
str.append("\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("LONG")) {
try {
str.append(raf.readLong());
str.append("\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("FLOAT")) {
try {
str.append(raf.readFloat());
str.append("\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("DOUBLE")) {
try {
str.append(raf.readDouble());
str.append("\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("BOOLEAN")) {
try {
str.append(raf.readBoolean());
str.append("\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
raf.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
str.append("\r\n");
return str.toString();
}
public boolean compAAttri(String attri, String value) { //比较
boolean flag = false;
if(hashmap.attriandtype.get(attri).equals("CHAR")) {
int x = hashmap.attriandlength.get(attri);
byte[] b = new byte[x];
try {
raf.read(b, 0, x);
if (value.equals(new String(b, "GB2312").trim())){
flag = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(hashmap.attriandtype.get(attri).equals("INT")) {
try {
if(Integer.valueOf(value) == raf.readInt()) {
flag = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(hashmap.attriandtype.get(attri).equals("LONG")) {
try {
if(Long.valueOf(value) == raf.readLong()) {
flag = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(hashmap.attriandtype.get(attri).equals("FLOAT")) {
try {
if(Float.valueOf(value) == raf.readFloat()) {
flag = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(hashmap.attriandtype.get(attri).equals("DOUBLE")) {
try {
if(Double.valueOf(value) == raf.readDouble()) {
flag = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(hashmap.attriandtype.get(attri).equals("BOOLEAN")) {
try {
if(Boolean.parseBoolean(value.toLowerCase()) == raf.readBoolean()) {
flag = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
public void writeALine() { //写入一行记录,写入时,文件指针在文件末尾,写入默认
for (int i = 0; i < num; i++) {
if(types[i].equals("CHAR")) {
byte[] b = format("1",hashmap.attriandlength.get(attributes[i]), 1).getBytes();
try {
raf.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("INT")) {
try {
raf.writeInt(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("LONG")) {
try {
raf.writeLong(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("FLOAT")) {
try {
raf.writeFloat(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("DOUBLE")) {
try {
raf.writeDouble(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("BOOLEAN")) {
boolean y = false;
try {
raf.writeBoolean(y);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
raf.write("\r\n".getBytes("GB2312"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void updateAAttri(String attri, String value) { //修改某一属性,修改前必须更改好文件指针
try {
if (hashmap.attriandtype.get(attri).equals("CHAR")) {
String str = format(value, hashmap.attriandlength.get(attri), value.length());
raf.write(str.getBytes());
}
if (hashmap.attriandtype.get(attri).equals("INT")) {
int x = Integer.valueOf(value);
raf.writeInt(x);
}
if (hashmap.attriandtype.get(attri).equals("LONG")) {
Long x = Long.valueOf(value);
raf.writeLong(x);
}
if (hashmap.attriandtype.get(attri).equals("FLOAT")) {
float x = Float.valueOf(value);
raf.writeFloat(x);
}
if (hashmap.attriandtype.get(attri).equals("DOUBLE")) {
double x = Double.valueOf(value);
raf.writeDouble(x);
}
if (hashmap.attriandtype.get(attri).equals("BOOLEAN")) {
boolean x = false;
if(value.equals("true")) {
x = true;
}
raf.writeBoolean(x);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteALine(long start, long end) { //start:被删除的行,end:最后一行
try {
for (int i = 0; i < num; i++) {
if(types[i].equals("CHAR")) {
int x = hashmap.attriandlength.get(attributes[i]);
byte[] b = new byte[x];// = new byte[x]
try {
raf.seek(end + hashmap.attriandstart.get(attributes[i]));
raf.read(b, 0, x);
raf.seek(start + hashmap.attriandstart.get(attributes[i]));
raf.write(b, 0, x);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("INT")) {
try {
raf.seek(end + hashmap.attriandstart.get(attributes[i]));
int x = raf.readInt();
raf.seek(start + hashmap.attriandstart.get(attributes[i]));
raf.writeInt(x);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("LONG")) {
try {
raf.seek(end + hashmap.attriandstart.get(attributes[i]));
long x = raf.readLong();
raf.seek(start + hashmap.attriandstart.get(attributes[i]));
raf.writeLong(x);
//System.out.println(raf.readLong());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("FLOAT")) {
try {
raf.seek(end + hashmap.attriandstart.get(attributes[i]));
float x = raf.readFloat();
raf.seek(start + hashmap.attriandstart.get(attributes[i]));
raf.writeFloat(x);
//System.out.println(raf.readLong());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("DOUBLE")) {
try {
raf.seek(end + hashmap.attriandstart.get(attributes[i]));
double x = raf.readDouble();
raf.seek(start + hashmap.attriandstart.get(attributes[i]));
raf.writeDouble(x);
//System.out.println(raf.readDouble());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(types[i].equals("BOOLEAN")) {
try {
raf.seek(end + hashmap.attriandstart.get(attributes[i]));
boolean x = raf.readBoolean();
raf.seek(start + hashmap.attriandstart.get(attributes[i]));
raf.writeBoolean(x);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
raf.setLength(end);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String format(String str, long x, long y) { //规范字符串格式,x:规定的字节数,y:str的实际字节数
for (int i = 0; i < x - y; i++) {
str = str +" ";
}
return str;
}
public void writeNextLine() { //末尾加换行符
try {
raf.write("\r\n".getBytes("GB2312"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close() { //关闭RandomAccessFile
try {
raf.close();
}
catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Result.java
package DataBase;
public class Result {
public String result;
public boolean flag;
public Result() {
result = null;
flag = false;
}
}
Table.java
package DataBase;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Table {
public String tablename;
public String fileprefix;
public File table;
public RandomAccessFiles randomaccessfile;
public Result result;
public Table(Result result) {
this.result = result;
}
public boolean existTable(String fileprefix,String tablename) {
boolean flag = false;
this.tablename = tablename;
this.fileprefix = fileprefix;
table = new File(fileprefix + tablename);
if(!table.exists()) {
//若该表格不存在,执行相应操作
result.result = "该表不存在";
System.out.println("该表不存在");
flag = false;
}else {
//该表存在
flag = true;
}
return flag;
}
public boolean existTuple() {
boolean flag = false;
try {
RandomAccessFile random = new RandomAccessFile(fileprefix + "record.txt", "r");
String s = null;
s = random.readLine();
while(!s.equals(tablename.substring(0, (int) (tablename.length() - 4)))) {
s = random.readLine();
}
flag = random.readBoolean();
if (flag) {
result.result = "该表已有数据,不允许修改";
System.out.println("该表已有数据,不允许修改");
}
random.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public void optTuple() {
randomaccessfile = new RandomAccessFiles();
try {
randomaccessfile.RandomAccessTable(fileprefix, tablename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void newTable(String str) { //新建表格 str = "Sno CHAR(9) KEY,Sname CHAR(20) UNIQUE,Ssex CHAR(2),Sage INT UNIQUE,Sdept CHAR(20)"
System.out.println(str);
try {
//先根据","算出有多少属性
int num = 1;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ",".charAt(0)) {
num ++;
}
}
String[] attris = new String[num];
String[] types = new String[num];
int[] length = new int[num];
boolean[] key = new boolean[num];
boolean[] isnull = new boolean[num];
int start = 0;
int end = 0;
int j = start;
int count = 0;
for (int i = 0; i < num; i++) {
while (str.charAt(j) != " ".charAt(0)) {
j++;
}
while (str.charAt(j) != " ".charAt(0)) {
j++;
}
end = j;
attris[i] = str.substring(start, end).trim(); //根据第一个空格找到属性
start = end + 1;
//根据"("得到CHAR
while (str.charAt(j) != "(".charAt(0)) {
j++;
//若果是","或" ",例如"Sage INT UNIQUE"或"Sage INT"得到非CHAR的类型
if (j == str.length()) {
break;
}
if (str.charAt(j) == ",".charAt(0) || str.charAt(j) == " ".charAt(0)) {
break;
}
}
end = j;
types[i] = str.substring(start, end).trim().toUpperCase();
start = end + 1;
if (types[i].equals("CHAR")) {
while ( str.charAt(j) != ")".charAt(0)) {
j++;
}
end = j;
length[i] = Integer.valueOf(str.substring(start, end).trim());
start = end + 1;
j++;
}
else {
if (types[i].equals("INT")) {
length[i] = 4;
}
if (types[i].equals("LONG")) {
length[i] = 8;
}
if (types[i].equals("FLOAT")) {
length[i] = 4;
}
if (types[i].equals("DOUBLE")) {
length[i] = 8;
}
if (types[i].equals("BOOLEAN")) {
length[i] = 1;
}
}
if (j == str.length()) { //判断是否读到末尾
key[i] = false;
isnull[i] = false;
break;
}
if (str.charAt(j) == ",".charAt(0)) {
j ++;
start = j;
key[i] = false;
isnull[i] = false;
continue;
}
while (str.charAt(j) != ",".charAt(0)) {
j ++;
if (j == str.length()) {
break;
}
}
end = j;
if (str.substring(start, end).trim().equals("")) continue;
String string = str.substring(start, end).trim().toUpperCase();
if (string.equals("KEY")) {
key[i] = true;
isnull[i] = true;
}
else {
key[i] = false;
if (string.equals("NOTNULL")) {
isnull[i] = true;
}
else {
isnull[i] = false;
}
}
start = end + 1;
}
for (int i = 0; i < num; i++) {
if (key[i]) {
count ++;
}
if (count > 1) {
result.result = "只能有一个关键字";
System.out.println("只能有一个关键字");
return;
}
}
for (int i = 0; i < num; i++) {
System.out.println(attris[i]);
System.out.println(types[i]);
System.out.println(length[i]);
System.out.println(key[i]);
System.out.println(isnull[i]);
}
RandomAccessFile random = new RandomAccessFile(fileprefix + tablename, "rw");
random.close();
RandomAccessFile randoms = new RandomAccessFile(fileprefix + "record.txt", "rw");
randoms.seek(randoms.length());
randoms.writeBytes(tablename.substring(0, tablename.length() - 4) + "\r\n");
randoms.writeBoolean(false);
randoms.writeBytes("\r\n");
randoms.writeInt(num);
randoms.writeBytes("\r\n");
for (int i = 0; i < num; i++) {
randoms.writeBytes(RandomAccessFiles.format(attris[i], 20, attris[i].length()));
}
randoms.write("\r\n".getBytes());
for (int i = 0; i < num; i++) {
randoms.writeBytes(RandomAccessFiles.format(types[i], 10, types[i].length()));
}
randoms.write("\r\n".getBytes());
for (int i = 0; i < num; i++) {
randoms.writeInt(length[i]);
}
randoms.write("\r\n".getBytes());
for (int i = 0; i < num; i++) {
randoms.writeBoolean(key[i]);
}
randoms.write("\r\n".getBytes());
for (int i = 0; i < num; i++) {
randoms.writeBoolean(isnull[i]);
}
randoms.write("\r\n".getBytes());
randoms.close();
File name = new File(fileprefix + "TableName.txt");
if (name.exists()){
RandomAccessFile ran = new RandomAccessFile(fileprefix + "TableName.txt", "rw");
ran.seek(0);
int x = ran.readInt();
ran.seek(0);
ran.writeInt(x + 1);
ran.seek(ran.length());
ran.writeBytes(tablename.substring(0, (int) (tablename.length() - 4)) + "\r\n");
ran.close();
}
else {
RandomAccessFile ran = new RandomAccessFile(fileprefix + "TableName.txt", "rw");
ran.writeInt(0);
ran.writeBytes("\r\n");
ran.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "操作成功";
}
public void deleteTable() { //删除表格
try {
table = new File(fileprefix + tablename);
if (table.exists()) {
table.delete();
}
StringBuffer value = new StringBuffer("");
String s = null;
RandomAccessFile random;
random = new RandomAccessFile(fileprefix + "record.txt", "rw");
random.seek(0);
s = random.readLine();
System.out.println(tablename);
while(!s.equals(tablename.substring(0, (int) (tablename.length() - 4)))) {
value.append(s + "\r\n");
s = random.readLine();
}
for (int i = 0; i < 7; i++) {
random.readLine();
}
while((s = random.readLine())!=null) {
value.append(s + "\r\n");
}
random.close();
FileWriter fw=new FileWriter(new File(fileprefix + "record.txt"));
fw.write(value.toString());
fw.close();
value = new StringBuffer("");
RandomAccessFile ran = new RandomAccessFile(fileprefix + "TableName.txt", "rw");
ran.seek(0);
int x = ran.readInt();
ran.seek(0);
ran.writeInt(x - 1);
ran.readLine();
s = ran.readLine();
while(!s.equals(tablename.substring(0, (int) (tablename.length() - 4)))) {
value.append(s + "\r\n");
s = ran.readLine();
}
while((s = ran.readLine())!=null) {
value.append(s + "\r\n");
}
ran.close();
FileWriter fww=new FileWriter(new File(fileprefix + "TableName.txt"));
fww.write(value.toString());
fww.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "操作成功";
}
public void addAttribute(String attri, String type, String str) { //增加一个属性
try {
int start = 0;
int end = 0;
int swps = 0;
String t = null;
String num = null;
String keystr = null;
String notnull = null;
String swp = null;
String numstr = null;
t = type.toUpperCase();
str = str.toUpperCase();
if(!type.equals("INT") || !type.equals("LONG") || !type.equals("FLOAT") || !type.equals("DOUBLE") || !type.equals("BOOLEAN")) {
for (int i = 0; i < type.length(); i++) {
if (type.charAt(i) == "(".charAt(0)) {
end = i ;
t = type.substring(start, end);
start = i;
}
if (type.charAt(i) == ")".charAt(0)) {
end = i ;
num = type.substring(start + 1, end);
}
}
}
type = t;
int y = 0;
if (num != null) {
y = Integer.valueOf(num);
}
if (type.equals("INT")) {
y = 4;
}
if (type.equals("LONG")) {
y = 8;
}
if (type.equals("FLOAT")) {
y = 4;
}
if (type.equals("DOUBLE")) {
y = 8;
}
if (type.equals("BOOLEAN")) {
y = 1;
}
long x = 0;
StringBuffer value = null;
String val = null;
RandomAccessFile random = new RandomAccessFile(fileprefix + "record.txt", "rw");
String s = null;
s = random.readLine();
while (!s.equals(tablename.substring(0, (int) (tablename.length() - 4)))) {
s = random.readLine();
}
x = random.getFilePointer();
random.readLine();
if (str.equals("KEY")) {
int count = random.readInt();
boolean flag = false;
for (int i = 0; i < 4; i++) {
random.readLine();
}
for (int i = 0; i < count; i++) {
if (random.readBoolean()) {
flag = true;
}
}
if (flag) {
result.result = "已存在关键字";
System.out.println("已存在关键字");
random.close();
return;
}
}
random.seek(x);
swp = random.readLine();
if (str.equals("KEY")) {
random.seek(x);
random.writeBoolean(true);
random.seek(x);
keystr = random.readLine();
notnull = keystr;
}else {
random.seek(x);
random.writeBoolean(false);
random.seek(x);
keystr = random.readLine();
if (str.equals("NOTNULL")) {
random.seek(x);
random.writeBoolean(true);
random.seek(x);
notnull = random.readLine();
}
else {
random.seek(x);
random.writeBoolean(false);
random.seek(x);
notnull = random.readLine();
}
}
random.seek(x);
random.writeBytes(swp + "\r\n");
x = random.getFilePointer();
swps = random.readInt();
random.seek(x);
random.writeInt(y);
random.seek(x);
numstr = random.readLine();
random.seek(x);
random.writeInt(swps);
random.seek(x);
int m = random.readInt();
random.seek(x);
random.writeInt(m + 1);
random.readLine();
x = random.getFilePointer();
value = new StringBuffer(random.readLine());
value.append(RandomAccessFiles.format(attri, 20, attri.length()) + "\r\n");
val = random.readLine();
value.append(val + RandomAccessFiles.format(type, 10, type.length()) + "\r\n");
val = random.readLine();
value.append(val + numstr + "\r\n");
val = random.readLine();
value.append(val + keystr + "\r\n");
val = random.readLine();
value.append(val + notnull + "\r\n");
while((val = random.readLine())!=null) {
value.append(val + "\r\n");
}
random.seek(x);
random.writeBytes(value.toString());
random.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "操作成功";
}
public void deleteAttribute(String attri) { //删除一个属性
try {
StringBuffer value = new StringBuffer("");
String s = null;
RandomAccessFile random = new RandomAccessFile(fileprefix + "record.txt", "rw");
s = random.readLine();
while(!s.equals(tablename.substring(0, (int) (tablename.length() - 4)))) {
value.append(s + "\r\n");
s = random.readLine();
}
value.append(s + "\r\n");
s = random.readLine();
value.append(s + "\r\n");
long start = 0;
start = random.getFilePointer();
int num = random.readInt();
random.seek(start);
random.writeInt(num - 1);
random.seek(start);
s = random.readLine();
value.append(s + "\r\n");
start = random.getFilePointer();
int x = 0;
byte[] b = new byte[20];
for (int i = 0; i < num; i++) {
random.read(b, 0, 20);
if (new String(b).trim().equals(attri)) {
x = i;
}else {
value.append(new String(b));
}
}
random.readLine();
value.append("\r\n");
s = random.readLine();
s = s.substring(0 ,x*10) + s.substring((x + 1)*10, s.length());
value.append(s + "\r\n");
s = random.readLine();
s = s.substring(0 ,x*4) + s.substring((x + 1)*4, s.length());
value.append(s + "\r\n");
s = random.readLine();
s = s.substring(0, x) + s.substring(x + 1, s.length());
value.append(s + "\r\n");
s = random.readLine();
s = s.substring(0, x) + s.substring(x + 1, s.length());
value.append(s + "\r\n");
while((s = random.readLine())!=null) {
value.append(s + "\r\n");
}
random.close();
FileWriter fw=new FileWriter(new File(fileprefix + "record.txt"));
fw.write(value.toString());
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "操作成功";
}
public void insertTuple(String attris, String values) { //插入一行
//输入:attris:(Sno,Sname,Ssex,Sdept,Sage)
// values:(201215128,陈东,男,IS,18)
//功能:将value中的值写入表格文件中
//需要解决的问题:1.attri里的属性顺序可能和文件里的属性顺序不同,写入的顺序和位置需要解决,需要用到每个属性的字节数
// 2.文件操作
//输出:无
System.out.println("insert");
System.out.println(randomaccessfile.keyword);
if (randomaccessfile.keyword == null) {
result.result = "该表无关键字";
System.out.println("该表无关键字");
return;
}
try {
String key = null;
String value = null;
int start1 = 0, end1;
int start2 = 0, end2;
long start = randomaccessfile.raf.length();
randomaccessfile.raf.seek(start);
randomaccessfile.writeALine();
for (int j = 0; j < randomaccessfile.num; j++) {
for (int i = start1; i < attris.length(); i++) {
if (attris.charAt(i) == ",".charAt(0)) {
end1 = i;
key = attris.substring(start1, end1).trim();
start1 = end1 + 1;
break;
}
if(i + 1 == attris.length()) {
end1 = i + 1;
key = attris.substring(start1, end1).trim();
break;
}
}
for (int m = start2; m < values.length(); m++) {
if (values.charAt(m) == ",".charAt(0)) {
end2 = m;
value = values.substring(start2, end2).trim();
start2 = end2 + 1;
break;
}
if(m + 1 == values.length()) {
end2 = m + 1;
value = values.substring(start2, end2).trim();
break;
}
}
if (randomaccessfile.hashmap.getnull(key) && value.toUpperCase().equals("NULL")) {
result.result = key + "不允许为空";
System.out.println(key + "不允许为空");
randomaccessfile.raf.setLength(start);
randomaccessfile.close();
return;
}
else {
if (!randomaccessfile.hashmap.getType(key).equals("CHAR") && value.toUpperCase().equals("NULL")) {
value = "0";
}
}
if (key.equals(randomaccessfile.keyword)) {
boolean flag = false;
long sum = 0;
long line0 = 0;
long line = 0;
try {
randomaccessfile.raf.seek(0);
sum = randomaccessfile.raf.length();
line = randomaccessfile.hashmap.getStart(key);
while (line0 < sum) {
randomaccessfile.raf.seek(line0 + line);
if (randomaccessfile.compAAttri(key, value)) {
flag = true;
break;
}
line0 += randomaccessfile.sum;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (flag) {
result.result = "关键字重复";
System.out.println("关键字重复");
randomaccessfile.raf.setLength(start);
randomaccessfile.close();
return;
}
}
randomaccessfile.raf.seek(start + randomaccessfile.hashmap.attriandstart.get(key));
randomaccessfile.updateAAttri(key, value);
}
randomaccessfile.close();
RandomAccessFile random = new RandomAccessFile(fileprefix + "record.txt", "rw");
String s = null;
s = random.readLine();
while(!s.equals(tablename.substring(0, (int) (tablename.length() - 4)))) {
s = random.readLine();
}
random.writeBoolean(true);
random.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "操作成功";
}
public void updateTuple(String attri1, String value1, String attri2, String value2) { //更新符合条件的元组中的属性值
//输入:attri1:Sage 需要更新的属性
// value1:22 设置的值
// attri2:Sno 查找的条件属性
// value2:201215121 符合该条件属性的值
//功能:查找所有满足Sno = 201215121条件的元组,修改元组中的属性值Sage = 22
//需要解决的问题:1.如何对属性Sno = 201215121定位
// 2.定位之后如何修改元组中的属性值
// 3.此处可以选对列进行操作,可以用seek指针加上每一行字节数,然后对满足条件的行进行操作
//输出:无
if (randomaccessfile.hashmap.getnull(attri1) && value1.toUpperCase().equals("NULL")) {
result.result = attri1 + "不允许为空";
System.out.println(attri1 + "不允许为空");
randomaccessfile.close();
return;
}
else {
if (!randomaccessfile.hashmap.getType(attri1).equals("CHAR") && value1.toUpperCase().equals("NULL")) {
if (randomaccessfile.hashmap.getType(attri1).equals("BOOLEAN")) {
value1 = "false";
}
else {
value1 = "0";
}
}
}
if (attri1.equals(randomaccessfile.keyword) && !attri2.equals(randomaccessfile.keyword)) {
result.result = "修改关键字请使用:UPDATE TABLE SET KEY = VALUE1 WHERE KEY = VALUE2;";
System.out.println("修改关键字请使用:UPDATE TABLE SET KEY = VALUE1 WHERE KEY = VALUE2;");
randomaccessfile.close();
return;
}
if (attri1.equals(randomaccessfile.keyword) && attri2.equals(randomaccessfile.keyword)) {
boolean flag = false;
long sum = 0;
long line0 = 0;
long line = 0;
try {
randomaccessfile.raf.seek(0);
sum = randomaccessfile.raf.length();
line = randomaccessfile.hashmap.getStart(attri1);
while (line0 < sum) {
randomaccessfile.raf.seek(line0 + line);
if (randomaccessfile.compAAttri(attri1, value1)) {
flag = true;
break;
}
line0 += randomaccessfile.sum;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (flag) {
result.result = "关键字重复";
System.out.println("关键字重复");
randomaccessfile.close();
return;
}
try {
randomaccessfile.raf.seek(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long sum = 0;
long line0, line1, line2;
try {
sum = randomaccessfile.raf.length();
line0 = 0;
line1 = randomaccessfile.hashmap.attriandstart.get(attri1);
line2 = randomaccessfile.hashmap.attriandstart.get(attri2);
while (line0 < sum) {
randomaccessfile.raf.seek(line0 + line2);
if (randomaccessfile.compAAttri(attri2, value2)) {
randomaccessfile.raf.seek(line0 + line1);
randomaccessfile.updateAAttri(attri1, value1);
}
line0 += randomaccessfile.sum;
}
randomaccessfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "操作成功";
}
public void selectTuple(String attri, String value) { //选择查找
//输入:attris:Sno, Sname
// attri:Sdept
// value:cs
//功能:选出所有满足Sdept = cs的元组,每一元组中只有Sno和Sname两个属性
//需要解决的问题:1.创建一个新表,将所有满足条件的元组加入表中
//输出:一张新表
StringBuffer str = new StringBuffer("");
long line = 0;
long sum = 0;
try {
sum = randomaccessfile.raf.length();
while (line < sum) {
randomaccessfile.raf.seek(line + randomaccessfile.hashmap.attriandstart.get(attri));
if (randomaccessfile.compAAttri(attri, value)) {
randomaccessfile.raf.seek(line);
str.append(randomaccessfile.readALine());
}
line += randomaccessfile.sum;
}
randomaccessfile.close();
result.flag = true;
result.result = str.toString();
System.out.println(str.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteTuple(String attri, String value) { //删除符合条件的元组
//输入:attri:Sno
// value:201215121
//功能:删除所有属性Sno = 201215121的元组
//需要解决的问题:1.如何对属性Sno = 201215121的元组定位
// 2.此处只需要对某一列进行操作,可以用seek指针加上每一行字节数
//输出:无
long line = 0;
long sum = 0;
long num = 0;
try {
sum = randomaccessfile.raf.length();
while (line < sum) {
randomaccessfile.raf.seek(line + randomaccessfile.hashmap.attriandstart.get(attri));
if (randomaccessfile.compAAttri(attri, value)) {
if (line == sum - randomaccessfile.sum ) {
randomaccessfile.raf.setLength(sum - randomaccessfile.sum);
sum = randomaccessfile.raf.length();
}
else {
randomaccessfile.deleteALine(line, sum - randomaccessfile.sum);
sum = randomaccessfile.raf.length();
}
}
else {
line += randomaccessfile.sum;
}
}
num = randomaccessfile.raf.length();
randomaccessfile.close();
if (num == 0) {
RandomAccessFile random = new RandomAccessFile(fileprefix + "record.txt", "rw");
String s = null;
s = random.readLine();
while(!s.equals(tablename.substring(0, (int) (tablename.length() - 4)))) {
s = random.readLine();
}
random.writeBoolean(false);
random.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "操作成功";
}
}
User.java
package DataBase;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class User {
String username;
String passward;
public RandomAccessFile raf;
String prefix = "List.txt";//String prefix = "DataBase\\List.txt";
public Result result;
public User(Result result) {
this.result = result;
}
public boolean newUser(String username, String passward){
//新用户,将用户名和密码写入List.txt
this.username = username;
this.passward = passward;
try {
raf = new RandomAccessFile("List.txt", "rw");
raf.seek(0);
String s = null;
while((s = raf.readLine())!=null) {
if (s.equals(username)) {
result.result = "该用户名已存在";
return false;
}
}
raf.writeBytes(username+"\r\n");
raf.writeBytes(passward+"\r\n");
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "用户创建成功";
return true;
}
public boolean oldUser(String username, String passward){
//已注册过的用户,在List.txt中查找,确认用户名和密码是否正确
this.username = username;
this.passward = passward;
String line = null;
try {
raf = new RandomAccessFile("List.txt", "rw");
raf.seek(0);
while ((line = raf.readLine()) != null && line.length() > 0) {
if(line.trim().equals(username)){
line = raf.readLine().trim();
if (line.equals(passward)) {
return true;
}
else {
result.result = "密码错误";
return false;
}
}
}
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.result = "该用户不存在";
return false;
}
public void close() { //关闭RandomAccessFile
try {
raf.close();
}
catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}