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

📄 deletestudentframe.java

📁 java+sql学生信息管理 该源码实现了学生信息的添加
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

import UI.StudentUI;
import javax.swing.border.*;
public class DeleteStudentFrame extends JFrame{
	private StudentUI userInterface1,userInterface2;
	private JButton firstButton1,secondButton1,firstButton2,secondButton2;
	String snoUpdate;
	static final String JDBC_DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
	static final String DATABASE_URL="jdbc:odbc:studyDSN";
	private Connection connection;
	private Statement statement;
	String sqlString;
	public DeleteStudentFrame()
	{
		super("Delete records from student");
		initialize();
		String names1[]={"请输入要删除的学号:"};
		userInterface1=new StudentUI(names1);
		String names2[]={"学号","姓名","性别","年龄","所在系"};
		userInterface2=new StudentUI(names2);
		Container c=getContentPane();
		Box box=Box.createVerticalBox();
		box.add(userInterface1);
		box.add(userInterface2);
		c.add(box);
		firstButton1=userInterface1.getDoTask1Button();
		firstButton1.setText("确认");
		firstButton1.addActionListener( 
		new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{DisplayRecord();}
		}		
		);
		secondButton1=userInterface1.getDoTask2Button();
		secondButton1.setText("清除");
		secondButton1.addActionListener( 
		new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{userInterface1.clearFields();}
		}
		);
		firstButton2=userInterface2.getDoTask1Button();
		firstButton2.setText("确然删除");
		firstButton2.addActionListener( 
		new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{UpdateRecord();}
		}		
		);
		secondButton2=userInterface2.getDoTask2Button();
		secondButton2.setText("放弃");
		secondButton2.addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{userInterface2.clearFields();}
		}		
		);		
		addWindowListener(
		new WindowAdapter(){
			public void windowClosing(WindowEvent event)
			{terminate();}
		}		
		);
		setSize(400,260);setVisible(true);
	}
		public void initialize(){
			try{
				Class.forName(JDBC_DRIVER);
				String login="sa";
	    		String password="123456";
				connection=DriverManager.getConnection(DATABASE_URL,login,password);
				statement=connection.createStatement();
			}
			catch(SQLException sqlException){
				JOptionPane.showMessageDialog(null, sqlException.getMessage(),"Database Error",JOptionPane.ERROR_MESSAGE);
				System.exit(1);
			}
			catch(ClassNotFoundException classNotFound){
				JOptionPane.showMessageDialog(null,classNotFound.getMessage(),"Driver Not Found",JOptionPane.ERROR_MESSAGE);
				System.exit(1);
			}
		}
		public void terminate(){
			try{
				statement.close();
				connection.close();
			}
			catch(SQLException sqlException){
				JOptionPane.showMessageDialog(null,sqlException.getMessage(),"Database Error",JOptionPane.ERROR_MESSAGE);
				System.exit(1);
			}
		}
		public void DisplayRecord(){
			String fieldValues1[]=userInterface1.getFieldValues();
			String fieldValues2[]=new String[5];
			if(!fieldValues1[StudentUI.SNO].equals("")){
				snoUpdate=fieldValues1[0];
				try{
					int numberAge=Integer.parseInt(fieldValues1[StudentUI.SNO]);
					String sqlString="select * from student where sno='"+fieldValues1[0]+"'";
					ResultSet resultSet=statement.executeQuery(sqlString);
					ResultSetMetaData metaData=resultSet.getMetaData();
					int numberOfColumns=metaData.getColumnCount();
					if(resultSet.next()){
						fieldValues2[0]=resultSet.getString(1);
						fieldValues2[1]=resultSet.getString(2);
						fieldValues2[2]=resultSet.getString(3);
						fieldValues2[3]=String.valueOf(resultSet.getString(4));
						fieldValues2[4]=resultSet.getString(5);
						
						userInterface2.setFieldValues(fieldValues2);
					}
					else{
						userInterface2.clearFields();
						JOptionPane.showMessageDialog(this, "Not fund this record!","Find Result",JOptionPane.INFORMATION_MESSAGE);
					}
				}
				catch(NumberFormatException formatException){
					JOptionPane.showMessageDialog(this, "Bad age number", "Invalid Number Format", JOptionPane.ERROR_MESSAGE);
				}
				catch(SQLException ee)
				{
					System.out.println(ee);
				}
			}
		}
		public void UpdateRecord()
		{
			String fieldValues[]=userInterface2.getFieldValues();
			if(!fieldValues[StudentUI.SNO].equals("")){
				try{
					int numberAge=Integer.parseInt(fieldValues[StudentUI.SNO]);
					String sqlString="delete from student where sno='"+snoUpdate+"'";
					int result=statement.executeUpdate(sqlString);
					if(result!=0)
					{JOptionPane.showMessageDialog(this, "Deleted sucess!","Delete Result",JOptionPane.INFORMATION_MESSAGE);
					}
				}
				catch(NumberFormatException formatException){
					JOptionPane.showMessageDialog(this, "Bad age number", "Invalid Number Format", JOptionPane.ERROR_MESSAGE);
				}
				catch(SQLException ee)
				{
					System.out.println(ee);
				}
			}
		}
		public static void main(String args[])
		{new DeleteStudentFrame();}
	
}

⌨️ 快捷键说明

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