Java数据库系统项目开发实践2这本书的所有代码
源代码在线查看: delete.java~22~
package sfms; import java.awt.*; import javax.swing.*; import java.sql.*; import java.awt.event.*; public class delete extends JFrame implements ActionListener{ JButton yesB,cancelB; JTextField text1; String SNO; public delete(){ Container c=this.getContentPane(); c.setLayout(new GridLayout(3,1)); c.setFont(new Font("true",Font.TRUETYPE_FONT,13)); JPanel p1=new JPanel(); JPanel p2=new JPanel(); //first,add the label JLabel label1=new JLabel("删除学生档案",SwingConstants.CENTER); label1.setFont(new Font("TRUE",Font.TRUETYPE_FONT,20)); label1.setForeground(Color.blue); c.add(label1); //set the input dialog frame JLabel label2=new JLabel("请输入学生号"); text1=new JTextField(10); p1.add(label2); p1.add(text1); c.add(p1); //set the buttons yesB=new JButton("确定"); cancelB=new JButton("退出"); p2.add(yesB); p2.add(cancelB); c.add(p2); //register frames as listener for button events yesB.addActionListener(this); cancelB.addActionListener(this); this.setSize(400,300); this.setTitle("删除学生档案"); this.setVisible(true); //create anonymous inner class to handle window closing event this.addWindowListener(new WindowAdapter() { public void windowClosing( WindowEvent event) {shutDown();} } ); } //action performed when a Button is invoked public void actionPerformed(ActionEvent e){ if(e.getSource()==yesB){ del(); } if(e.getSource()==cancelB){ shutDown(); } } private void del(){ SNO=text1.getText(); try{ Class.forName("oracle.jdbc.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:SFMS"; Connection conn=DriverManager.getConnection(url,"df","1234"); String sql="DELETE FROM DF.FILEINFO WHERE STNO='"+ SNO +"'"; PreparedStatement ps=conn.prepareStatement(sql); ps.executeUpdate(sql); conn.close(); JOptionPane.showMessageDialog(this,"删除成功!"); } catch(SQLException e){ System.out.println("Error Code: "+ e.getErrorCode()); System.out.println("Message="+e.getMessage()); } catch(Exception e){ e.printStackTrace(); } } private void shutDown(){ System.exit(0); } public static void main(String args[]){ delete dd=new delete(); } }