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

📄 addstudentframe.java

📁 java+sql学生信息管理 该源码实现了学生信息的添加
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import UI.StudentUI;
public class AddStudentFrame extends JFrame{
	private StudentUI userInterface;
	private JButton clearButton,writeButton;
	
	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;
	
	String names[]={"学号","姓名","性别","年龄","所在系"};
	public AddStudentFrame()
	{
		super("Add a record of students");
		initialize();
		
		userInterface=new StudentUI(names);
		getContentPane().add(userInterface,BorderLayout.CENTER);
		
		writeButton=userInterface.getDoTask1Button();
		writeButton.setText("保存");
		
		writeButton.addActionListener(
			new  ActionListener(){
				public void actionPerformed(ActionEvent event)
				{addRecord();}
			});
		clearButton=userInterface.getDoTask2Button();
		clearButton.setText("清除");
		clearButton.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent event)
				{
					userInterface.clearFields();
				}
			}	);
		addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent event)
					{terminate();}
				});
		setSize(300,200);setVisible(true);
	}
	
	private 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 addRecord(){
		String fieldValues[]=userInterface.getFieldValues();
		if(!fieldValues[StudentUI.SNO].equals("")){
			try{
				int numberAge=Integer.parseInt(fieldValues[StudentUI.SAGE]);
				String sqlInsert="INSERT INTO student VALUES('"+fieldValues[0]+"','"+fieldValues[1]+"','"+fieldValues[2]+"','"+numberAge+"','"+fieldValues[4]+"')";
				int result=statement.executeUpdate(sqlInsert);
				if(result!=0)
					{
						userInterface.clearFields();
						JOptionPane.showMessageDialog(this, "Inserted sucess!","InsertResult",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);
				}
		}
		else
			JOptionPane.showMessageDialog(this, "Bad sno number", "Invalid Number Format", JOptionPane.ERROR_MESSAGE);
	}
		public static void main(String args[])
		{
			new AddStudentFrame();
		}
						
	
	}

⌨️ 快捷键说明

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