FileDocCategorySizeDatePackage
JDAdmin.javaAPI DocExample7977Fri Oct 12 20:02:16 BST 2001jabadot

JDAdmin

public class JDAdmin extends JFrame
A User Database Administrator program This does NOT use the UserDB interface as it needs to be able to do ANYTHING to the database, to go beyond, and to repair any errors introduced by bugs in the UserDB code and/or queries. :-) If using InstantDB, therefore, you MUST NOT RUN THIS PROGRAM while users have access to the system, or the database will get worse instead of better!
version
$Id: JDAdmin.java,v 1.6 2001/10/12 23:02:16 ian Exp $

Fields Summary
protected ArrayList
userList
the list of users
protected Connection
conn
The database connection
protected PreparedStatement
listUsersStatement
A Statement for listing users
protected PreparedStatement
deleteUserStatement
A Statement for deleting users
protected PreparedStatement
setPasswordStatement
A Statement for resetting passwords for forgetful users
protected JTable
theTable
The main table
Constructors Summary
public JDAdmin()
Constructor

		super("JabaDotAdmin");

		// INIT THE DB 
		// Do this before the GUI, since JDBC does more-delayed
		// type checking than Swing...
		
		String dbDriver = JDConstants.getProperty("jabadot.jabadb.driver");
		try {
			Class.forName(dbDriver);
		} catch (ClassNotFoundException ex) {
			JOptionPane.showMessageDialog(this,
				"JDBC Driver Failure:\n" + ex, "Error",
				JOptionPane.ERROR_MESSAGE);
		}
		conn = DriverManager.getConnection(
			JDConstants.getProperty("jabadot.jabadb.url"));
		listUsersStatement = conn.prepareStatement("select * from users");
		deleteUserStatement = 
			conn.prepareStatement("delete from users where name = ?");
		setPasswordStatement = conn.prepareStatement(
			"update users SET password = ? where name = ?");

		// INIT THE GUI
		Container cp = getContentPane();
		cp.setLayout(new BorderLayout());
		cp.add(new JScrollPane(theTable = new JTable(new MyTableModel())),
			BorderLayout.CENTER);
		JPanel bp = new JPanel();
		JButton x;
		bp.add(x = new JButton("Delete"));
		x.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ex) {
					int r = theTable.getSelectedRow();
					if (r == -1) {
						JOptionPane.showMessageDialog(JDAdmin.this,
							"Please select a user to delete", "Error", 
							JOptionPane.ERROR_MESSAGE);
						return;
					}
					int i = JOptionPane.showConfirmDialog(JDAdmin.this,
							"Really delete user?", "Confirm", 
							JOptionPane.YES_NO_OPTION);
					switch(i) {
						case 0:
							try {
								delete(r);
							} catch (SQLException e) {
								JOptionPane.showMessageDialog(JDAdmin.this,
								"SQL Error:\n" + e, "Error", 
								JOptionPane.ERROR_MESSAGE);
							}
							break;
						case 1:
							// nothing to do.
							break;
						default:
							System.err.println("showConfirm: unex ret " + i);
					}
			}
		});
		bp.add(x = new JButton("List"));
		x.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ex) {
				try {
					populate();
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(JDAdmin.this,
					"SQL Error:\n" + e, "Error", 
					JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		bp.add(x = new JButton("Exit"));
		x.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ex) {
				System.exit(0);
			}
		});
	
		cp.add(bp, BorderLayout.SOUTH);

	
Methods Summary
public voiddelete(int x)
Delete the given user, by row number (row number in the display == index into the ArrayList). Use a JDBC PreparedStatement; if it succeeds, then also remove the user object from the ArrayList.

		User u = (User)userList.get(x);
		String nick = u.getName();
		deleteUserStatement.setString(1, nick);
		int n;
		switch (n = deleteUserStatement.executeUpdate()) {
			case 0:
				// no match!
				JOptionPane.showMessageDialog(this,
					"No match for user " + nick, "Error",
					JOptionPane.ERROR_MESSAGE);
				break;
			case 1:
				// OK
				JOptionPane.showMessageDialog(this,
					"User " + nick + " deleted.", "Done",
					JOptionPane.INFORMATION_MESSAGE);
				userList.remove(x);
				break;
			default:
				// Ulp! Deleted too many! -- n
				JOptionPane.showMessageDialog(this,
					"Oops, we deleted " + n + " users!!", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		theTable.repaint();
	
public static voidmain(java.lang.String[] av)
Main program -- driver


	     
	       
		JDAdmin aFrame = new JDAdmin();
		aFrame.populate();
		// aFrame.pack();
		aFrame.setSize(600,450);
		aFrame.setVisible(true);
	
public voidpopulate()
Get the current list of users from the database into the ArrayList, so the display will be up-to-date after any major change.

		ResultSet rs = listUsersStatement.executeQuery();
		userList.clear();
		while (rs.next()) {
			String nick = rs.getString(1);
			// System.out.println("Adding " + nick);
			User u = new User(nick, rs.getString(UserDB.PASSWORD),
				rs.getString(UserDB.FULLNAME),
				rs.getString(UserDB.EMAIL), 
				rs.getString(UserDB.CITY),
				rs.getString(UserDB.PROVINCE),
				rs.getString(UserDB.COUNTRY),
				rs.getInt(UserDB.PRIVS));
			userList.add(u);
		}
		rs.close();
		theTable.repaint();