public class DAOException extends RuntimeException { static final long serialVersionUID = -1881205326938716446L; public DAOException(String message) { super(message); } public DAOException(Throwable cause) { super(cause); } public DAOException(String message, Throwable cause) { super(message, cause); } }
以上是数据库连接所需方法。然后根据数据库中表的结构写下所需的方法类
Admin.java
import java.io.Serializable; public class Admin implements Serializable { static final long serialVersionUID = 103844514947365244L;
private int id; private String username; private String userpwd;
public Admin() {
}
public Admin(int id,String username,String userpwd) { this.id = id; this.username=username; this.userpwd=userpwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; }
public List getAdmins() throws DAOException { List list = new ArrayList(); Connection c = null; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM admin ORDER BY id"); while (rs.next()) { list.add(new Admin(rs.getInt("id"), rs.getString("username"), rs.getString("userpwd"))); } } catch (SQLException e) { e.printStackTrace(); throw new DAOException(e); } finally { ConnectionHelper.close(c); } return list; } }