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

📄 databaseserver.java

📁 用自己写的j2me rms引擎写的电话本 功能是: 1.添加 2.查询 3.列出所有的资料 1.修改 2.删除 3.排序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				 
				
			} catch (InvalidRecordIDException e) {
				//e.printStackTrace();
			} catch (RecordStoreException e) {			 
				//e.printStackTrace();
			} catch (IOException e) {
				 
				//e.printStackTrace();
			}
		}
		return data;
	}

	//查找数据的实现
	private static Hashtable findDataImpl(String key, String value, MetaData meta, RecordStore table) throws SQLException {
		int keyIndex = 0;
		java.util.Enumeration em = meta.getColumn().elements();
		String[] ma = new String[meta.getColumn().size()];
		int size=0;
		while(em.hasMoreElements()){
			String s = (String) em.nextElement();
			ma[size]=s;
			//logger.debug("KEY:" + s);
			//logger.debug("key:" + key);
			if(s.equals(key)){
				keyIndex = size;
			} 
			size++;	
		}

		int num=0;
		try {
			num = table.getNumRecords();
		} catch (RecordStoreNotOpenException e) {
			
		}
	
		Hashtable data = new Hashtable();
		
		int dataIndex=0;
		for(int i=1; i <= num; i++){	
			try {
			 
				String datastring = read(table, i);
				
;				String[] ms = StringUtil.split(datastring, DataCore.SQIL);
				//logger.debug("keyIndex=" + keyIndex);
				if(keyIndex>=ms.length){
					throw SQLError.createSQLException("Unkonw error", "Sql State : " + SQLError.SQL_STATE_OHTER); 
				}
			//	logger.debug("DataString: " + datastring);
				//logger.debug(ms[keyIndex] + "=" + value);
				if(ms[keyIndex].equals(value)){
					
					logger.debug("DataString: " + datastring);
					Hashtable row = new Hashtable();
					
					//一个非常重要的索引
					row.put(DataCore.ROW_INDEX, new Integer(i).toString());
					for(int j=0; j<ms.length; j++){
						row.put(ma[j], ms[j]);
					}
					data.put(new Integer(dataIndex), row);
					dataIndex++;
				}
				
			} catch (InvalidRecordIDException e) {
				//e.printStackTrace();
			} catch (RecordStoreException e) {			 
				//e.printStackTrace();
			} catch (IOException e) {
				 
				//e.printStackTrace();
			}
		}
		return data;
	}

	/**
	 * 个人感觉这样读取数据实在是太不好了
	 * 应该采用枚举的方式读取
	 * @param table
	 * @param index
	 * @return
	 * @throws IOException
	 * @throws RecordStoreNotOpenException
	 * @throws InvalidRecordIDException
	 * @throws RecordStoreException
	 */
	private static String read(RecordStore table, int index) throws IOException, RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException {
		byte[] data = table.getRecord(index);
		
		return readStringFromBytes(data);
	}

	/**
	 * 分析元数据,并保存到散列表中,
	 * 不知道为什么会突然多了个比较怪的符号.
	 * 是不是写进入RMS的时候出错了那
	 * 比如写的时候没有#符号的,读取出来的时候就多了个#,好郁闷
	 * @param bytes
	 * @throws RmsAccessException 
	 */
	private static final void putMetaToHashtable(byte[] bytes) throws RmsAccessException{	
		checkNull(hashmeta);
	 
		MetaData metadata = new MetaData(bytes);	
		logger.debug("MetaData: " + metadata.toString());
		hashmeta.put(metadata.getName(), metadata);		
		table.put(metadata.getName(), createTable(metadata.getName()));
	}
	
	/**
	 * 通过表名称查询此表格元数据是否存在
	 * @param name -- 表名
	 * @return
	 */
	public static final MetaData findTableMetadataByName(String name){
		if(hashmeta == null)
			return null;
		 
		return (MetaData) hashmeta.get(name);
	}
	
	/**
	 * 创建一个表对象,即是一个表对于一个RecordStore
	 * @param name
	 * @return
	 * @throws RmsAccessException --创建表出错时候抛出异常
	 */
	public static final RecordStore createTable(String name) throws RmsAccessException{
		try {
			return  RecordStore.openRecordStore(name, true);
		} catch (RecordStoreFullException e) {
			throw new RmsAccessException("存储空间已慢");
			 
		} catch (RecordStoreNotFoundException e) {
			throw new RmsAccessException("数据库没有找到");
		} catch (RecordStoreException e) {
			throw new RmsAccessException("访问RMS异常");
			 
		}
	}
	
	/**
	 * 删除数据表格
	 * @param name
	 * @throws SQLException
	 */
	public static final void delete(String name) throws SQLException{
		 
			try {
				 
				RecordStore.deleteRecordStore(name);
			} catch (RecordStoreNotFoundException e) {
				e.printStackTrace();
				throw SQLError.createSQLException("Table : " + name + " is not exists!", "Sql State : " + SQLError.SQL_STATE_BASE_TABLE_NOT_FOUND);
			} catch (RecordStoreException e) {
				 
				e.printStackTrace();
				throw SQLError.createSQLException("Table : " + name + " is not exists!", "Sql State : " + SQLError.SQL_STATE_BASE_TABLE_NOT_FOUND);
			}
 
	}
	/**
	 * 提供对数据的插入
	 * @param object
	 * @throws SQLException 
	 */
	public static final void store(String tableName, byte [] values) throws SQLException{
		try {	
			findTable(tableName).addRecord(values, 0, values.length);
		} catch (RecordStoreNotOpenException e) {		 
			e.printStackTrace();
			throw SQLError.createSQLException("Table " + tableName + " Not Open!", "Sql State : " + SQLError.SQL_STATE_OHTER);
		} catch (RecordStoreFullException e) {	 
			e.printStackTrace();
			throw SQLError.createSQLException("Table : " + tableName + " is FULL!", "Sql State : " + SQLError.SQL_STATE_OHTER);
		} catch (RecordStoreException e) { 
			e.printStackTrace();
			throw SQLError.createSQLException("Table : " + tableName + " is Error!", "Sql State : " + SQLError.SQL_STATE_OHTER);
		}  
	}
	
	public static final byte[] getBytes(String value) throws IOException{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		 
		dos.writeUTF(value.toString());
		byte[] bytes = baos.toByteArray();	
		dos.close();
		baos.close();
		return bytes;
	}
	 
	/**
	 * 通过表name查找RecordStore
	 * @param name
	 * @return
	 */
	public static final RecordStore findTable(String name) throws SQLException{
		checkNull(table);
		RecordStore rs = (RecordStore) table.get(name);
		if(rs == null)
			throw SQLError.createSQLException("Table : " + name + " is not exists!", "Sql State : " + SQLError.SQL_STATE_BASE_TABLE_NOT_FOUND);
		 
		return rs;
	}
	private static final void checkNull(Object object){
		if(object == null)
			throw new NullPointerException("对象为空!");
	}
	
	
	public static final void colse(){
		Enumeration es = table.elements();
		while(es.hasMoreElements()){
			RecordStore rs = (RecordStore) es.nextElement();
			try {
				rs.closeRecordStore();
			} catch (RecordStoreNotOpenException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			} catch (RecordStoreException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
	}
	
	public static final void closeTable(String name) throws SQLException{
		//为什么要关两次呢?
		try {
			findTable(name).closeRecordStore();
			findTable(name).closeRecordStore(); 
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
			throw SQLError.createSQLException("Table : " + name + " is not exists!", "Sql State : " + SQLError.SQL_STATE_BASE_TABLE_NOT_FOUND);
		} catch (RecordStoreException e) {
			 
			e.printStackTrace();
			throw SQLError.createSQLException("Table : " + name + " is not exists!", "Sql State : " + SQLError.SQL_STATE_BASE_TABLE_NOT_FOUND);
		}
	}
	
	public static final String readStringFromBytes(byte[] bytes)  {
		ByteArrayInputStream bais = new	ByteArrayInputStream(bytes);   //通过canddidate创建一个字节输入流
		DataInputStream dis = new  DataInputStream(bais);
		String string = null;
		try {
			string = dis.readUTF();
			bais.close();
			dis.close();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		
		return string;
	}
	
}

⌨️ 快捷键说明

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