?? droptableframe.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 + -