java模拟数据库sql操作源码,适合作中间件

  • O9_540569
    了解作者
  • 4.2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-31 04:17
    上传日期
积分不多了,赚个分,大家海涵 ,这个源码学习用最佳,还可以做中间件,不用配数据库,直接可以看成一个数据库的源码,当然效率很低!有很多不足大家一起努力更正!!~~
com.rar
  • com
  • database
  • JavaDataBase.java
    15KB
  • DBrun.java
    1.7KB
内容介绍
package com.database; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Vector; public class JavaDataBase { private DatabaseTool dbt = new DatabaseTool(); /** * execute the sql progress * @param sql * @return Vector<Vector<String>> result */ public Vector<Vector<String>> execute(String sql){ Vector<Vector<String>> result = new Vector<Vector<String>>(); String[] keyword = sql.split(" "); if (keyword.length<3) { System.out.println("sql语法错误:"+sql); System.exit(-1); }else if(keyword[0].equals("create")){ String tableName = keyword[1]; dbt.createTable(tableName, getKeyWord(keyword[2])); }else if(keyword[0].equals("insert")){ String tableName = keyword[1]; dbt.insertTable(tableName, getKeyWord(keyword[2])); }else if(keyword[0].equals("update")){ String tableName = keyword[1]; Vector<String> headLineOrder = vecHeadOrder(tableName, keyword[4]); Vector<String> setLineOrder = vecHeadOrder(tableName, keyword[2]); Vector<Integer> updateLineNum = dbt.getLineNum(tableName, headLineOrder); dbt.updateTable(tableName, setLineOrder, updateLineNum); }else if(keyword[0].equals("select")){ Vector<String> vecHead; Vector<String> vecCondition; String tableName = keyword[3]; vecHead = selectUseVec(tableName, keyword[1]); if (keyword.length==4) { vecCondition= new Vector<String>(); for (int i = 0; i < dbt.getTableHead(tableName).size(); i++) { vecCondition.add("null"); } }else { vecCondition = selectUseVec(tableName, keyword[5]); } result = dbt.selectTableContent(tableName, vecHead, vecCondition); }else if(keyword[0].equals("delete")){ String tableName = keyword[2]; Vector<String> vecCondition; vecCondition = vecHeadOrder(tableName, keyword[4]); Vector<Integer> deleteLineNum = dbt.getLineNum(tableName, vecCondition); if (deleteLineNum.size()!=0) { dbt.deleteTableLine(tableName, deleteLineNum); }else { System.out.println("没有合适删除条件的对象:"+sql); } }else { System.out.println("无法判断sql语句:"+sql); System.exit(-1); } return result; } /** * get the select use key * @param tableName * @param selectKey * @return */ private Vector<String> selectUseVec(String tableName,String selectKey) { // TODO Auto-generated method stub Vector<String> output = new Vector<String>(); if (selectKey.equals("*")) { output = dbt.getTableHead(tableName); }else { output = vecHeadOrder(tableName, selectKey); } return output; } /** * get the line order of the database with the condition * @param tableName * @param conditionWord (age=126,id=99) * @return */ private Vector<String> vecHeadOrder(String tableName,String conditionWord) { // TODO Auto-generated method stub Vector<String> conditionKey = getKeyWord(conditionWord); Vector<String> tableHead = dbt.getTableHead(tableName); Vector<String> headOrderKey = new Vector<String>(); for (int i = 0; i < tableHead.size(); i++) { String strAdd = "null"; for (int j = 0; j < conditionKey.size(); j++) { String strTemp = conditionKey.elementAt(j); if (strTemp.contains("=")) { String[] strSplit = strTemp.split("="); if (strSplit[0].equals(tableHead.elementAt(i))) { strAdd = transForWord(strSplit[1]); } }else { if (strTemp.equals(tableHead.elementAt(i))) { strAdd = strTemp; } } } headOrderKey.add(strAdd); } int adoptDEC = 0; for (int i = 0; i < headOrderKey.size(); i++) { if (!headOrderKey.elementAt(i).equals("null")) { adoptDEC++; } } if (adoptDEC!=conditionKey.size()) { System.out.println("属性匹配出错,可能是单词拼写错误:"+conditionWord); System.exit(-1); } return headOrderKey; } /** * get key word for sql as the flash (id,name,age=13) * @param keyword */ private Vector<String> getKeyWord(String keyword) { // TODO Auto-generated method stub Vector<String> output = new Vector<String>(); String strTemp = keyword.substring(1, keyword.length()-1); String[] key; if (strTemp.contains(",")) { key = strTemp.split(","); }else{ key = new String[1]; key[0] = strTemp; } for (int i = 0; i < key.length; i++) output.add(transForWord(key[i])); return output; } /** * transfor word to other whichever you want to get * @param input * @return */ private String transForWord(String input) { // TODO Auto-generated method stub String output = ""; if (input.equals("''")) { output = " "; }else { output = input; } return output; } } class DatabaseTool { FileRandW frw = new FileRandW(); public DatabaseTool() { // TODO Auto-generated constructor stub } /** * create database table * @param tableName */ void createTable(String tableName) { // TODO Auto-generated method stub frw.createFile(tableName+".csv"); } void createTable(String tableName,Vector<String> tableHead) { String strTemp = ""; frw.createFile(tableName+".csv"); if (tableHead.size()==0) { System.out.println("表头发生错误:DatabaseTool.createTable(String tableName,Vector<String> tableHead)"); System.exit(-1); } for (int i = 0; i < tableHead.size(); i++) { if (i==0) { strTemp = tableHead.elementAt(0); }else { strTemp += (","+tableHead.elementAt(i)); } } frw.pRINTLN(strTemp); } /** * delete database table(未完成,待定) * @param tableName */ void deleteTable(String tableName){ } /** * insert content to table * @param tableName * @param tableContent */ void insertTable(String tableName, Vector<String> tableContent){ frw.selsectFile(tableName+".csv"); frw.createFile(tableName+".csv"); if(frw.getInformation(0)=="empty"){ System.out.println("插入的数据无表头:DatabaseTool.insertTable(String tableName, Vector<String> tableContent)"); frw.pRINTLN(); System.exit(-1); }else { String strTemp = ""; int headLength = frw.getInformation(0).split(",").length; if (headLength!= tableContent.size()) { System.out.println("插入数据与表头不符:DatabaseTool.insertTable(String tableName, Vector<String> tableContent)"); frw.pRINTLN(); System.exit(-1); }else { for (int i = 0; i < headLength; i++) { if (i==0) { strTemp = tableContent.elementAt(0); }else { strTemp += (","+tableContent.elementAt(i)); } } frw.pRINTLN(strTemp); } } } /** * update the table content * @param tableName * @param tableContent * @param updateLine */ void updateTable(String tableName, Vector<String> tableContent, Vector<Integer> updateLine) { // TODO Auto-generated method stub frw.selsectFile(tableName+".csv"); frw.createFile(tableName+".csv"); if(frw.getInformation(0)=="empty"){ System.out.println("更新数据无表头:DatabaseTool.updateTable(String tableName, Vector<String> tableContent, int updateLine)"); frw.pRINTLN(); System.exit(-1); }else { Vector<String> vecUpdate = new Vector<String>(); Vector<String> vecTemp = frw.getInformation(updateLine); for (int i = 0; i < vecTemp.size(); i++) { String strTemp = ""; String[] strUpdateUse = vecTemp.elementAt(i).split(","); for (int j = 0; j < tableContent.size(); j++) { if (tableContent.elementAt(j).equals("null")) { strTemp += strUpdateUse[j]+","; }else { strTemp += tableContent.elementAt(j)+","; } } strTemp = strTemp.substring(0, strTemp.length()-1); vecUpdate.add(strTemp); } frw.pRINTLN(vecUpdate, updateLine); } } /** * delete som
评论
    相关推荐