全部博文(18)
分类: Java
2008-05-07 13:24:09
public class DBTable {
protected Connection conn;
protected tableName;
public DBTable(String tableName) {
this.tableName = tableName;
this.conn = ...;
}
public void clear() {
PreparedStatement st = conn.prepareStatement("DELETE FROM "+tableName);
try {
st.executeUpdate();
}finally{
st.close();
}
}
public int getCount() {
PreparedStatement st = conn.prepareStatement("SELECT COUNT(*) FROM"+tableName);
try {
ResultSet rs = st.executeQuery();
rs.next();
return rs.getInt(1);
}finally{
st.close();
}
}
}
public class ParticipantsInDB extends DBTable {
public ParticipantsInDB() {
super("participants");
}
public void addParticipant(Participant part) {
...
}
public void deleteParticipant(String participantId) {
setDeleteFlag(participantId, true);
}
public void restoreParticipant(String participantId) {
setDeleteFlag(participantId, false);
}
private void setDeleteFlag(String participantId, boolean b) {
...
}
public void reallyDelete() {
PreparedStatement st = conn.prepareStatement(
"DELETE FROM "+
tableName+
" WHERE deleteFlag=true");
try {
st.executeUpdate();
}finally{
st.close();
}
}
public int countParticipants() {
PreparedStatement st = conn.prepareStatement(
"SELECT COUNT(*) FROM "+
tableName+
" WHERE deleteFlag=false");
try {
ResultSet rs = st.executeQuery();
rs.next();
return rs.getInt(1);
}finally{
st.close();
}
}
}
ParticipantsInDB partsInDB = ...;
Participant kent = new Participant(...);
Participant paul = new Participant(...);
partsInDB.clear();
partsInDB.addParticipant(kent);
partsInDB.addParticipant(paul);
partsInDB.deleteParticipant(kent.getId());
System.out.println("There are "+partsInDB.getCount()+ "participants");
public class DBTable {
private Connection conn;
private String tableName;
public DBTable(String tableName) {
this.tableName = tableName;
this.conn = ...;
}
public void clear() {
PreparedStatement st = conn.prepareStatement("DELETE FROM "+tableName);
try {
st.executeUpdate();
}finally{
st.close();
}
}
public int getCount() {
PreparedStatement st = conn.prepareStatement("SELECT COUNT(*) FROM "+tableName);
try {
ResultSet rs = st.executeQuery();
rs.next();
return rs.getInt(1);
}finally{
st.close();
}
}
public String getTableName() {
return tableName;
}
public Connection getConn() {
return conn;
}
}
public class ParticipantsInDB {
private DBTable table;
public ParticipantsInDB() {
table = new DBTable("participants");
}
public void addParticipant(Participant part) {
...
}
public void deleteParticipant(String participantId) {
setDeleteFlag(participantId, true);
}
public void restoreParticipant(String participantId) {
setDeleteFlag(participantId, false);
}
private void setDeleteFlag(String participantId, boolean b) {
...
}
public void reallyDelete() {
PreparedStatement st = table.getConn().prepareStatement(
"DELETE FROM "+
table.getTableName()+
" WHERE deleteFlag=true");
try {
st.executeUpdate();
}finally{
st.close();
}
}
public void clear() {
table.clear();
}
public int countParticipants() {
PreparedStatement st = table.getConn().prepareStatement(
"SELECT COUNT(*) FROM "+
table.getTableName()+
" WHERE deleteFlag=false");
try {
ResultSet rs = st.executeQuery();
rs.next();
return rs.getInt(1);
}finally{
st.close();
}
}
}
ParticipantsInDB partsInDB = ...;
Participant kent = new Participant(...);
Participant paul = new Participant(...);
partsInDB.clear();
partsInDB.addParticipant(kent);
partsInDB.addParticipant(paul);
partsInDB.deleteParticipant(kent.getId());
//编译出错:因为在ParticipantsInDB里面已经没有getCount这个方法了!
System.out.println("There are "+partsInDB.getCount()+ "participants");
abstract class Component {
boolean isVisible;
int posXInContainer;
int posYInContainer;
int width;
int height;
...
abstract void paint(Graphics graphics);
void setWidth(int newWidth) {
...
}
void setHeight(int newHeight) {
...
}
}
class Button extends Component {
ActionListener listeners[];
...
void paint(Graphics graphics) {
...
}
}
class Container {
Component components[];
void add(Component component) {
...
}
}
class ClockComponent extends Component {
...
void paint(Graphics graphics) {
//根据时间绘制当前的钟表图形
}
}
abstract class Component {
boolean isVisible;
int posXInContainer;
int posYInContainer;
...
abstract void paint(Graphics graphics);
}
abstract class RectangularComponent extends Component {
int width;
int height;
void setWidth(int newWidth) {
...
}
void setHeight(int newHeight) {
...
}
}
class Button extends RectangularComponent {
ActionListener listeners[];
...
void paint(Graphics graphics) {
...
}
}
class ClockComponent extends Component {
...
void paint(Graphics graphics) {
//根据时间绘制当前的钟表图形
}
}
abstract class Component {
boolean isVisible;
int posXInContainer;
int posYInContainer;
...
abstract void paint(Graphics graphics);
}
class RectangularDimension {
int width;
int height;
void setWidth(int newWidth) {
...
}
void setHeight(int newHeight) {
...
}
}
class Button extends Component {
ActionListener listeners[];
RectangularDimension dim;
...
void paint(Graphics graphics) {
...
}
void setWidth(int newWidth) {
dim.setWidth(newWidth);
}
void setHeight(int newHeight) {
dim.setHeight(newHeight);
}
}
class ClockComponent extends Component {
...
void paint(Graphics graphics) {
//根据时间绘制当前的钟表图形
}
}