⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 droptableframe.java

📁 Java数据库编程实例(4)
💻 JAVA
字号:
package droptable;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 DropTableFrame extends JFrame {  private JPanel contentPane;  private XYLayout xYLayout1 = new XYLayout();       // 构造XYLayout布局管理器  // 创建显示信息使用的组件  private Label label1 = new Label();  private TextField droptablenameField = new TextField();  private Button droptableButton = new Button();  Connection connection = null;                      // 声明Connection接口对象connection  Statement statement = null;                        // 定义查询数据库的Statement对象  public DropTableFrame() {    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("删除数据库中的表");    droptableButton.setLabel("删除表");    droptableButton.addActionListener(new java.awt.event.ActionListener() { // droptableButton的事件监听方法      public void actionPerformed(ActionEvent e) {        droptableButton_actionPerformed(e);      }    });    contentPane.add(label1, new XYConstraints(56, 73, 85, 33));    contentPane.add(droptablenameField, new XYConstraints(164, 73, 108, 35));    contentPane.add(droptableButton, new XYConstraints(164, 178, 102, 37));  }  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  void droptableButton_actionPerformed(ActionEvent e) {// 处理droptableButton事件    try {      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  // 实例化JDBC-ODBC桥的驱动      String url = "jdbc:odbc:TestDbStu";             // 设置连接字符串      connection = DriverManager.getConnection(url);  // 连接数据库      statement = connection.createStatement();      String sql="drop table  "+droptablenameField.getText() ;      statement.executeUpdate(sql);                   // 执行删除数据库中的表语句      droptablenameField.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) {          connection.close();                                 // 关闭Connection接口实例        }      }      catch (SQLException ex)  {        System.out.println("\nERROR:----- SQLException -----\n");        System.out.println("Message:   " + ex.getMessage( ));        System.out.println("SQLState:  " + ex.getSQLState());        System.out.println("ErrorCode: " + ex.getErrorCode());      }    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -