package createtable;
import com.borland.jbcl.layout.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
/**
* Title: 创建数据库中的表程序
* Description: 教学示范
* Copyright: Copyright (c) 2003
* Company: 北京师范大学计算机系
* @author 孙一林
* @version 1.0
*/
public class CreateTableFrame extends JFrame {
private XYLayout xYLayout1 = new XYLayout(); // 构造XYLayout布局管理器
private JPanel contentPane;
// 创建显示信息使用的组件
private Label label1 = new Label();
private TextField tablenameField = new TextField();
private Label label2 = new Label();
private TextField columnname1Field = new TextField();
private TextField columnname2Field = new TextField();
private TextField columnname3Field = new TextField();
private Label label3 = new Label();
private Choice column1typechoice = new Choice();
private Choice column2typechoice = new Choice();
private Choice column3typechoice = new Choice();
private Label label4 = new Label();
private Checkbox checkbox1 = new Checkbox();
private Checkbox checkbox2 = new Checkbox();
private Checkbox checkbox3 = new Checkbox();
private CheckboxGroup checkboxGroup1 = new CheckboxGroup();
private Button creattableButton = new Button();
Connection connection = null; // 声明Connection接口对象connection
Statement statement = null; // 定义查询数据库的Statement对象
// 定义字段类型字符串变量
String column1type;
String column2type;
String column3type;
String sql; // 定义SQL语句字符串变量
public CreateTableFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
// 初始化组件
label1.setText("创建表名称:");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("创建数据库中的表");
label2.setText("字段名:");
label3.setText("字段类型:");
label4.setText("主 键:");
checkbox1.setCheckboxGroup(checkboxGroup1);
checkbox1.setLabel("字段1");
checkbox1.addItemListener(new java.awt.event.ItemListener() { // checkbox1的事件监听方法
public void itemStateChanged(ItemEvent e) {
checkbox1_itemStateChanged(e);
}
});
checkbox2.setCheckboxGroup(checkboxGroup1);
checkbox2.setLabel("字段2");
checkbox2.addItemListener(new java.awt.event.ItemListener() { // checkbox2的事件监听方法
public void itemStateChanged(ItemEvent e) {
checkbox2_itemStateChanged(e);
}
});
checkbox3.setCheckboxGroup(checkboxGroup1);
checkbox3.setLabel("字段3");
checkbox3.addItemListener(new java.awt.event.ItemListener() { // checkbox3的事件监听方法
public void itemStateChanged(ItemEvent e) {
checkbox3_itemStateChanged(e);
}
});
checkboxGroup1.setSelectedCheckbox(null);
creattableButton.setLabel("创建表");
creattableButton.addActionListener(new java.awt.event.ActionListener() { // creattableButton的事件监听方法
public void actionPerformed(ActionEvent e) {
creattableButton_actionPerformed(e);
}
});
column1typechoice.addItemListener(new java.awt.event.ItemListener() { // column1typechoice的事件监听方法
public void itemStateChanged(ItemEvent e) {
column1typechoice_itemStateChanged(e);
}
});
column2typechoice.addItemListener(new java.awt.event.ItemListener() { // column2typechoice的事件监听方法
public void itemStateChanged(ItemEvent e) {
column2typechoice_itemStateChanged(e);
}
});
column3typechoice.addItemListener(new java.awt.event.ItemListener() { // column3typechoice的事件监听方法
public void itemStateChanged(ItemEvent e) {
column3typechoice_itemStateChanged(e);
}
});
contentPane.add(tablenameField, new XYConstraints(156, 35, 88, 29));
contentPane.add(columnname2Field, new XYConstraints(204, 90, 70, 28));
contentPane.add(label2, new XYConstraints(47, 88, 69, 28));
contentPane.add(columnname1Field, new XYConstraints(121, 90, 70, 28));
contentPane.add(columnname3Field, new XYConstraints(286, 90, 70, 28));
contentPane.add(column2typechoice, new XYConstraints(206, 139, 73, 27));
contentPane.add(label3, new XYConstraints(47, 142, 64, 24));
contentPane.add(column1typechoice, new XYConstraints(121, 139, 73, 27));
contentPane.add(column3typechoice, new XYConstraints(290, 139, 73, 27));
contentPane.add(checkbox2, new XYConstraints(214, 182, 76, 30));
contentPane.add(label4, new XYConstraints(47, 184, 45, 30));
contentPane.add(checkbox1, new XYConstraints(124, 182, 76, 30));
contentPane.add(checkbox3, new XYConstraints(303, 182, 76, 30));
contentPane.add(creattableButton, new XYConstraints(153, 216, 94, 34));
contentPane.add(label1, new XYConstraints(47, 36, 81, 24));
// 向choice控件中添加项
column2typechoice.insert(" ",0) ;
column3typechoice.insert(" ",0) ;
column1typechoice.insert(" ",0) ;
column1typechoice.insert("文本",1) ;
column2typechoice.insert("文本",1) ;
column3typechoice.insert("文本",1) ;
column1typechoice.insert("数字",2) ;
column2typechoice.insert("数字",2) ;
column3typechoice.insert("数字",2) ;
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void checkbox1_itemStateChanged(ItemEvent e) { // 处理checkbox1事件
sql="create table "+tablenameField.getText()+ "("+columnname1Field.getText()+" "+column1type+" constraint "+columnname1Field.getText()+" primary key, "+columnname2Field.getText()+" "+column2type+", "+columnname3Field.getText()+" "+column3type+")" ;
}
void checkbox2_itemStateChanged(ItemEvent e) { // 处理checkbox2事件
sql="create table "+tablenameField.getText()+ "("+columnname1Field.getText()+" "+column1type+", "+columnname2Field.getText()+" "+column2type+" constraint "+columnname2Field.getText()+" primary key, "+columnname3Field.getText()+" "+column3type+")" ;
}
void checkbox3_itemStateChanged(ItemEvent e) { // 处理checkbox3事件
sql="create table "+tablenameField.getText()+ "("+columnname1Field.getText()+" "+column1type+", "+columnname2Field.getText()+" "+column2type+", "+columnname3Field.getText()+" "+column3type+" constraint "+columnname1Field.getText()+" primary key "+")" ;
}
void creattableButton_actionPerformed(ActionEvent e) { // 处理creattableButton事件
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 实例化JDBC-ODBC桥的驱动
String url = "jdbc:odbc:TestDbStu"; // 设置连接字符串
connection = DriverManager.getConnection(url); // 连接数据库
statement = connection.createStatement();
statement.executeUpdate(sql); // 执行创建数据库表语句
// 清空文本框
tablenameField.setText("");
columnname1Field.setText("");
columnname2Field.setText("");
columnname3Field.setText("");
}
catch(SQLException ex){ // 捕捉异常
System.out.println("\nERROR:----- SQLException -----\n");
while (ex != null) {
System.out.println("Message: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("ErrorCode: " + ex.getErrorCode());
ex = ex.getNextException();
}
}
catch(Exception ex ) {
ex.printStackTrace();
}
finally {
try {
if(statement != null) {
statement.close(); // 关闭Statement接口实例
}
if(connection != null) {