建立一个 Derby 日历,第 3 部分: 使用事务和锁定(下)
2010-04-19 00:00:00 来源:WEB开发网这些常量让您很容易在 Derby 数据库的嵌入形式和网络形式之间选择,而 getConnection() 和 closeConnection() 方法的作用就像名称所暗示的那样。createTableIfNotExists() 方法尝试创建表,如果已经存在则什么也不做。
创建框架和连接
我们来创建 GUI 的各个部分。总体目标是包含三部分的一个框架(参见 清单 21):左侧两个,右侧一个。
清单 21. 创建框架
...
public class CalendarFrame extends JFrame implements PropertyChangeListener {
private Connection conn;
private JCalendar calendarPicker;
private EventTableModel eventModel;
public CalendarFrame() {
super();
this.setTitle("Derby Calendar");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
try {
this.conn = Database.getConnection(Database.NETWORKED);
this.conn.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(layoutMenuBar(), BorderLayout.NORTH);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerLocation(0.6);
splitPane.setLeftComponent(layoutLeftPanel());
splitPane.setRightComponent(layoutRightPanel());
this.getContentPane().add(splitPane, BorderLayout.CENTER);
}
private JComponent layoutLeftPanel() {
JSplitPane viewSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
return viewSplitPane;
}
private JPanel layoutRightPanel() {
JPanel tablePanel = new JPanel(new BorderLayout());
return tablePanel;
}
private JComponent layoutMenuBar() {
JMenuBar menuBar = new JMenuBar();
return menuBar;
}
public static void main(String args[]) {
Database.createTableIfNotExists();
// Set the SQL lock time out to just 1 second (default is 60)
System.setProperty("derby.locks.waitTimeout", "1");
CalendarFrame w = new CalendarFrame();
w.setSize(750, 550);
w.setVisible(true);
}
public void propertyChange(PropertyChangeEvent evt) {
}
}
更多精彩
赞助商链接