userdbdbm.java

来自「codebook!」· Java 代码 · 共 51 行

JAVA
51
字号
package jabadot;import java.io.*;import java.util.*;import java.sql.SQLException;/** A trivial "database" for User objects, stored in a flat file. * <P> * Since this is exected to be used heavily, and to avoid the overhead * of re-reading the file, the "Singleton" Design Pattern is used * to ensure that there is only ever one instance of this class. */public class UserDBDBM extends UserDB {	protected final static String DEF_NAME = 		"/home/ian/src/jabadot/jabadb";		// It appends .pag	protected DBM db;	/** Default Constructor */	protected UserDBDBM() throws IOException,SQLException {		this(DEF_NAME);	}	/** Constructor */	protected UserDBDBM(String fn) throws IOException,SQLException {		super();				db = new DBM(fn);		String k;		Object o;		// Iterate through contents of DBM, adding into list.		for (o=db.firstkeyObject(); o!=null; o=db.nextkey(o)) {			// firstkey/nextkey give Key as Object, cast to String.			k = (String)o;			o = db.fetch(k);	// Get corresponding Value (a User)			users.add((User)o);	// Add to list.		}	}	/** Add one user to the list, both in-memory and on disk. */	public synchronized void addUser(User nu) throws IOException, SQLException {		// Add it to the in-memory list		super.addUser(nu);		// Add it to the on-disk version: store in DB with		// key = nickname, value = object.		db.store(nu.getName(), nu);	}}

⌨️ 快捷键说明

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