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

📄 yoursqlstatement.java

📁 原创的DBMS 数据库! 功能很简单
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package code;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.ResultSet;
import java.sql.Connection;
public class yourSQLStatement implements java.sql.Statement {

	private yourSQLConnection connection;
	public RandomAccessFile randomAccessFileq=null;
	private static boolean debug=false;
	 public yourSQLStatement(yourSQLConnection conn) {

		    connection = conn;
		    if ( debug) System.out.println("connection is " + connection.toString());

		  }
	 
	public void addBatch(String arg0) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	public void cancel() throws SQLException {
		// TODO Auto-generated method stub
		
	}

	public void clearBatch() throws SQLException {
		// TODO Auto-generated method stub
		
	}

	public void clearWarnings() throws SQLException {
		// TODO Auto-generated method stub
		
	}

	public void close() throws SQLException {
		// TODO Auto-generated method stub
//		try {
//			randomAccessFileq.close();
//			System.out.println(" Q  randomAccessFileq.close();");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	public boolean execute(String arg0) throws SQLException {
		// TODO Auto-generated method stub
		
		return false;
	}

	public boolean execute(String arg0, int arg1) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean execute(String arg0, int[] arg1) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean execute(String arg0, String[] arg1) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	public int[] executeBatch() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public ResultSet executeQuery(String sql) throws SQLException {
		// TODO Auto-generated method stub
		//查询!!!
		if(sql.startsWith("select"))
		{
			int snum=0,fnum=0;
			int fromflag=sql.indexOf("from");
			String [] colname=new String[10];
			String strtemp="";
			int colnamenum=0;
			snum=7;
			fnum=sql.indexOf(" ",snum);
			strtemp=sql.substring(snum,fnum);
			if(!strtemp.equals("*"))
			{
				while(true)
				{
				colname[colnamenum]=strtemp;
				colnamenum++;
				snum=fnum+1;
				fnum=sql.indexOf(" ",snum);
				if(fnum>=fromflag)
					break;
				}
			}
			snum=fromflag+5;
			fnum=sql.indexOf(" ",snum);
			String tablename=sql.substring(snum,fnum);//获取表的名称
			int whereflag=sql.indexOf("where");
			String property="";
			String value="";
			if(whereflag>0)
			{
				snum=whereflag+6;
				fnum=sql.indexOf("=",snum);
				property=sql.substring(snum,fnum);
				snum=sql.indexOf("'")+1;
				fnum=sql.indexOf("'",snum);
				value=sql.substring(snum,fnum);
			}
			int[]resultindex=new int[1000];
			String name[]=new String[10];
			String type[]=new String[10];
			int propertyflag=0;
			int proindex=0;
			int ptemp=0;
			int resultnum=0;
			yourSQLResultSet ySQLrs=new yourSQLResultSet();
			ySQLrs.colname=colname;
			ySQLrs.colnamenum=colnamenum;
			//开始打开文件查询
			File file=new File("E:/"+tablename+".dbf");
			try {
				 randomAccessFileq = new RandomAccessFile(file, "r");
				int headlongth=randomAccessFileq.readInt();
				int headnum=randomAccessFileq.readInt();
				int filelongth=randomAccessFileq.readInt();
				int filenum=randomAccessFileq.readInt();
		
				ySQLrs.setRandomAccessFile(randomAccessFileq);
				ySQLrs.headnum=headnum;
				if(filenum>0)
				{
				if(whereflag>0)
				{
					for(int k=0;k<headnum;k++)
					{
						 char[] namece = new char[100]; 
						 char[] typece=new char[100];
					        for(int i = 0; i < namece.length; i++) 
					             namece[i] = randomAccessFileq.readChar(); 
					        for(int i = 0; i < typece.length; i++) 
					        	typece[i] = randomAccessFileq.readChar(); 
					        
					        name[k]=new String(namece).replace('\0', ' ');
					        type[k]=new String(typece).replace('\0', ' ');
					        if(name[k].startsWith(property))
						       {
						    	   propertyflag=ptemp;
						    	   proindex=k;
						       }
					        if(type[k].startsWith("int"))
					        	ptemp+=4;
					        if(type[k].startsWith("string"))
					        	ptemp+=200;
					        if(type[k].startsWith("double"))
					        	ptemp+=8;
					        
					       
					}
					String valueget="";
					for(int i=0;i<filenum;i++)
					{
						randomAccessFileq.seek(headlongth+i*filelongth+propertyflag);
						if(type[proindex].startsWith("int"))
						{
							valueget=Integer.toString(randomAccessFileq.readInt());
						}
						if(type[proindex].startsWith("string"))
						{
							char[] vtemp=new char[100];
							for(int g=0;g<vtemp.length;g++)
							{
								vtemp[g]=randomAccessFileq.readChar(); 
							}
							valueget=new String(vtemp).replace('\0', ' ');
						}
						if(type[proindex].startsWith("double"))
						{
							valueget=Double.toString(randomAccessFileq.readDouble());
						}
		//	System.out.print("valueget:"+valueget+"!"+value+"!");
						if(valueget.startsWith(value))
						{
							resultindex[resultnum]=headlongth+i*filelongth;
							resultnum++;
						}
					}
						ySQLrs.propertyflag=propertyflag;
						ySQLrs.name=name;
						ySQLrs.type=type;
					if(resultnum>0)
					{
						ySQLrs.resultnum=resultnum;
						ySQLrs.resultindex=resultindex;
					}
				}else
				{
					for(int k=0;k<headnum;k++)
					{
						 char[] namece = new char[100]; 
						 char[] typece=new char[100];
					        for(int i = 0; i < namece.length; i++) 
					             namece[i] = randomAccessFileq.readChar(); 
					        for(int i = 0; i < typece.length; i++) 
					        	typece[i] = randomAccessFileq.readChar(); 
					        
					        name[k]=new String(namece).replace('\0', ' ');
					        type[k]=new String(typece).replace('\0', ' ');
					  }
					System.out.println("file longth  "+filelongth);
					for(int i=0;i<filenum;i++)
					{
						resultindex[i]=headlongth+i*filelongth;
						System.out.println(" 1  "+resultindex[i]);
					}
					resultnum=filenum;
					ySQLrs.name=name;
					ySQLrs.type=type;
					ySQLrs.resultnum=resultnum;
					ySQLrs.resultindex=resultindex;
				}
//				System.out.println(resultindex[0]);
//				System.out.println(resultindex[1]);
				
			}
				
				return ySQLrs;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}

	public int executeUpdate(String str) throws SQLException {
		// TODO Auto-generated method stub
	//	System.out.println(" executeUpdate is here !!!!!!!!!!!!!!");
		//return 1:Sucess    return 0: Fail!!!!
		//CREATE TABLE IS HERE!!!!!!!!!!!!!!
		if(str.startsWith("create table"))
		{
			int snum=0,fnum=0;
			//int headnum=0,headlongth=0;
			int strend=str.indexOf(")");
			Tablefile table=new Tablefile();
			String stemp="";
			snum=13;
			fnum=str.indexOf("(",snum);
			stemp=str.substring(snum,fnum);
			System.out.println("TABLE NAME :"+stemp);
			table.tablename=stemp;
			while(true)
			{
				snum=fnum+1;
				fnum=str.indexOf(" ",snum);
				String tname=str.substring(snum,fnum);
				snum=fnum+1;
				fnum=str.indexOf(",",snum);
				if(fnum==-1)
				{
					fnum=strend;
				String ttype=str.substring(snum,fnum);
				table.cols[table.headnum].setName(tname);
				table.cols[table.headnum].setType(ttype);
				table.headnum++;
				break;
				}else
				{
					String ttype=str.substring(snum,fnum);
					table.cols[table.headnum].setName(tname);
					table.cols[table.headnum].setType(ttype);
					table.headnum++;
				}
			}
			table.headlongth=16+400*table.headnum;
			for(int i=0;i<table.headnum;i++)
			{
				if(table.cols[i].type.startsWith("int"))
					table.filelongth+=4;
				if(table.cols[i].type.startsWith("string"))
					table.filelongth+=200;
				if(table.cols[i].type.startsWith("double"))
					table.filelongth+=8;
			}
			//System.out.println("Table filelongth :"+table.filelongth);
			 File file = new File("E:/"+table.tablename+".dbf"); 
	           // 建立RandomAccessFile实例?K以读写模式打开
			 try {
				RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
				randomAccessFile.writeInt(table.headlongth);
				randomAccessFile.writeInt(table.headnum);
				randomAccessFile.writeInt(table.filelongth);
				randomAccessFile.writeInt(table.filenum);
				for(int j=0;j<table.headnum;j++)
				{
					randomAccessFile.writeChars(table.cols[j].name);
					randomAccessFile.writeChars(table.cols[j].type);
					
				}
				randomAccessFile.close();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("Error in create");
			} 
			//测试读取!!!!
			try {
				RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
				System.out.println("HEADLONGTH :"+randomAccessFile.readInt());
				int headnumce=randomAccessFile.readInt();
				System.out.println("HEADNUM :"+headnumce);
				System.out.println("FILELONGTH :"+randomAccessFile.readInt());
				System.out.println("FILENUM :"+randomAccessFile.readInt());
				for(int k=0;k<headnumce;k++)
				{
					 char[] namece = new char[100]; 
					 char[] typece=new char[100];
				        for(int i = 0; i < namece.length; i++) 
				             namece[i] = randomAccessFile.readChar(); 
				        for(int i = 0; i < typece.length; i++) 
				        	typece[i] = randomAccessFile.readChar(); 
				        
				         System.out.println("name :"+ new String(namece).replace('\0', ' ')); //去掉空白字符
				         System.out.println("type :"+ new String(typece).replace('\0', ' '));
				}
				randomAccessFile.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//测试结束
		}
		else if(str.startsWith("insert into"))
		{
			int snum=0,fnum=0;
			int filenum=0;
			int strend=str.indexOf(")");
//			Tablefile table=new Tablefile();
			String stemp="";
			snum=12;
			fnum=str.indexOf("(",snum);
			stemp=str.substring(snum,fnum);
			System.out.println("TABLE NAME :"+stemp);
//			table.tablename=stemp;
			File file = new File("E:/"+stemp+".dbf");
			try {
				RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
				
				
				
				
				System.out.println("HEADLONGTH :"+randomAccessFile.readInt());
				int headnumce=randomAccessFile.readInt();
				System.out.println("HEADNUM :"+headnumce);
				System.out.println("FILELONGTH :"+randomAccessFile.readInt());
				
				filenum=randomAccessFile.readInt()+1;
				System.out.println("FILENUM :"+filenum);
				randomAccessFile.seek(12);
				randomAccessFile.writeInt(filenum);
				String type[]=new String [headnumce];
				for(int k=0;k<headnumce;k++)
				{
					 char[] namece = new char[100]; 
					 char[] typece=new char[100];
				        for(int i = 0; i < namece.length; i++) 
				             namece[i] = randomAccessFile.readChar(); 
				        for(int i = 0; i < typece.length; i++) 
				        	typece[i] = randomAccessFile.readChar(); 
				    type[k]=new String(typece).replace('\0', ' ');    
				         
				}
				
				int length = (int)randomAccessFile.length();
				randomAccessFile.seek(length);
//				int numofcol = (length-16)/400;
				
//				length=numofcol*200+16;
				System.out.println("TABLE COL :"+headnumce);
				System.out.println("TABLE LENGTH BEFOR INSERT :"+length);
				
				String valueofcol[] = new String[headnumce];
				String temp;
				int i=0;
				
				while(true)
				{
				snum=fnum+1;
				fnum=str.indexOf(",",snum);
				if(fnum==-1){
					fnum=strend;
					temp=str.substring(snum,fnum);
					valueofcol[headnumce-1]=temp;
					break;
				}				
				
				temp = str.substring(snum,fnum);
				valueofcol[i]=temp;
				i++;
				}
				
				String value;
				StringBuilder builder = null; 
			       
			       
				for(int k=0;k<headnumce;k++)
				{
					if(type[k].startsWith("string")){
						if(valueofcol[k] != null) 
					           builder = new StringBuilder(valueofcol[k]); 
					    else    
					           builder = new StringBuilder(100); 
					    builder.setLength(100); 
					    value = builder.toString(); 
					    randomAccessFile.writeChars(value);
					}
					else if(type[k].startsWith("int")){
						randomAccessFile.writeInt(Integer.parseInt(valueofcol[k]));
					}
					else if(type[k].startsWith("double")){
						randomAccessFile.writeDouble(Double.parseDouble(valueofcol[k]));
					}
					      
				}
				
				//测试是否能读出数据来来================
//				randomAccessFile.seek(randomAccessFile.length()-200);
//				char[] typece=new char[100];
//				for( i = 0; i < typece.length; i++) 
//		        	typece[i] = randomAccessFile.readChar(); 
//					//type[k]=new String(typece).replace('\0', ' ');
//				System.out.println("HEADLONGTHdadadsa :"+new String(typece).replace('\0', ' '));

				length = (int)randomAccessFile.length();
				System.out.println("TABLE LENGTH AFTER INSERT :"+length);
				randomAccessFile.close();
				//System.out.println("TABLE LENGTH :"+length);
			}  catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(str.startsWith("update"))
		{
			int snum=0,fnum=0;
			snum=7;
			fnum=str.indexOf(" ",snum);
			String tablename=str.substring(snum,fnum);
			snum=str.indexOf("set")+4;
			fnum=str.indexOf("=",snum);
			String propertyset=str.substring(snum,fnum);
			snum=str.indexOf("'")+1;
			fnum=str.indexOf("'",snum);
			String propertysetval=str.substring(snum,fnum);
			snum=str.indexOf("where")+6;
			fnum=str.indexOf("=",snum);
			String property=str.substring(snum,fnum);
			snum=str.indexOf("'",fnum)+1;
			fnum=str.indexOf("'",snum);
			String propertyval=str.substring(snum,fnum);
			
			File file=new File("E:/"+tablename+".dbf");
			try {
				RandomAccessFile randomAccessFile=new RandomAccessFile(file,"rw");
				int headlongth=randomAccessFile.readInt();
				int headnum=randomAccessFile.readInt();
				int filelongth=randomAccessFile.readInt();
				int filenum=randomAccessFile.readInt();
				String []name=new String[10];
				String []type=new String[10];
				int ptemp=0;

⌨️ 快捷键说明

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