java的数据库应用——学生信息管理系统(swing界面)

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import java.awt.*;
public class xueshengshuju extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField name_text;
	private JTextField xuehao_text;
	private JTextField chengji_text;
	private JTextArea textPane;
	private String sql;
	private String name;
	private String xuehao;
	private int chengji;
	private Connection con; // 数据库连接
	private Statement stmt; // Statement
	private ResultSet rs; // 查询结果集
	JButton button_1;
	JButton button_2;
	JButton button_3;
	JButton button_4;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					xueshengshuju frame = new xueshengshuju();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public xueshengshuju() {
		super("学生信息管理系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		textPane = new JTextArea();
		textPane.setBounds(10, 109, 426, 154);
		
		contentPane.add(textPane);
		
		JLabel numbutton = new JLabel("\u59D3\u540D");
		numbutton.setBounds(10, 10, 36, 23);
		contentPane.add(numbutton);
		
		JLabel xuehaobutton = new JLabel("\u5B66\u53F7");
		xuehaobutton.setBounds(10, 43, 36, 23);
		contentPane.add(xuehaobutton);
		
		JLabel chengjibutton = new JLabel("\u6210\u7EE9");
		chengjibutton.setBounds(10, 76, 29, 23);
		contentPane.add(chengjibutton);
		
		name_text = new JTextField();
		name_text.setBounds(56, 11, 97, 22);
		contentPane.add(name_text);
		name_text.setColumns(10);
		
		xuehao_text = new JTextField();
		xuehao_text.setBounds(56, 44, 97, 21);
		contentPane.add(xuehao_text);
		xuehao_text.setColumns(10);
		
		chengji_text = new JTextField();
		chengji_text.setBounds(56, 75, 97, 21);
		contentPane.add(chengji_text);
		chengji_text.setColumns(10);
		
		button_1 = new JButton("\u663E\u793A");
		button_1.setBounds(202, 10, 84, 23);
		contentPane.add(button_1);
		
		button_2 = new JButton("\u5220\u9664");
		button_2.setBounds(202, 43, 84, 23);
		contentPane.add(button_2);
		
		button_3 = new JButton("\u6DFB\u5165");
		button_3.setBounds(307, 10, 84, 23);
		contentPane.add(button_3);
		
		button_4 = new JButton("\u4FEE\u6539");
		button_4.setBounds(307, 43, 84, 23);
		contentPane.add(button_4);
		
		
		
		

		button_1.addActionListener(this);
		button_2.addActionListener(this);
		button_3.addActionListener(this);
		button_4.addActionListener(this);
		textPane.setForeground(Color.red);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				clearMemoryCloseWidowAndExit(e.getWindow());
			}
			}  );
		try {
			Class.forName("org.sqlite.JDBC");
			String url = "jdbc:sqlite:/C:\\Users\\asus\\Documents\\Tencent Files\\1729616683\\FileRecv\\BookPrice.db";
			con = DriverManager.getConnection(url, "", "");
			stmt = con.createStatement();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	void clearMemoryCloseWidowAndExit(Window wnd) {
		//参数:wnd--要关闭的窗体
				int option = JOptionPane.showConfirmDialog(wnd, "确定要退出应用吗?", "系统提示", JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (option != JOptionPane.OK_OPTION)
					return;
				wnd.dispose(); // 销毁window
				try {
					stmt.close(); // 关闭Statement对象
					con.close(); // 关闭数据库链接
				} catch (Exception e) {
					System.out.println(e.toString());
				}
				System.exit(0);
			}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		if (e.getSource() ==button_1) {
			try {
				sql="SELECT 姓名, 学号,成绩 FROM BookInf";
				rs=stmt.executeQuery(sql);
				textPane.setText("姓名\t\t学号\t\t成绩\n");
				while(rs.next()) {
					
					name=rs.getString("姓名");
					xuehao=rs.getString("学号");
					chengji=rs.getInt("成绩");
					String s = String.format("%s\t\t%s\t\t%d\n",name,xuehao,chengji);
					textPane.append(s);
				
					
				}
				
				
			} catch (SQLException e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
		else if(e.getSource()==button_2) {
			String str = name_text.getText().trim();
			if (!str.isEmpty()) {
				int option = JOptionPane.showConfirmDialog(this, "确定要删除该记录吗?", "系统提示", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
				if (option == JOptionPane.YES_OPTION) {
	
						try {
							String strDel = "DELETE FROM BookInf WHERE 姓名 ='" + str + "'";
							stmt.executeUpdate(strDel);
							JOptionPane.showMessageDialog(this, "删除成功!", "好消息", JOptionPane.INFORMATION_MESSAGE);
						} catch (HeadlessException e1) {
							// TODO 自动生成的 catch 块
							e1.printStackTrace();
							JOptionPane.showMessageDialog(this, "删除失败!", "坏消息", JOptionPane.INFORMATION_MESSAGE);
						} catch (SQLException e1) {
							// TODO 自动生成的 catch 块
							e1.printStackTrace();
							JOptionPane.showMessageDialog(this, "删除失败!", "坏消息", JOptionPane.INFORMATION_MESSAGE);
						}
						}
			}
		}
		else if(e.getSource()==button_3) {
			try {
				String str1 = name_text.getText().trim();
				String str2 = xuehao_text.getText().trim();
				String str3 = chengji_text.getText().trim();
				String strInc = "INSERT INTO BookInf(姓名,成绩,学号)Values('" + str1 + "','" + str2 + "','" + str3 + "')";
				stmt.executeUpdate(strInc);
				JOptionPane.showMessageDialog(this, "添加完成!", "好消息", JOptionPane.INFORMATION_MESSAGE);
			} catch (SQLException e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
				JOptionPane.showMessageDialog(this, "添加失败!", "坏消息", JOptionPane.ERROR_MESSAGE);
			}
		}
		else if(e.getSource()== button_4){
			try {
				String str1 = name_text.getText().trim();
				String str2 = chengji_text.getText().trim();
				String strUpd = "UPDATE BookInf SET 成绩 = '" + str2 + "'WHERE 姓名 = '" + str1 + "'";
				stmt.executeUpdate(strUpd);
				JOptionPane.showMessageDialog(this, "修改完成!", "好消息", JOptionPane.INFORMATION_MESSAGE);
			} catch (Exception e3) {
				System.out.println(e3.toString());
				JOptionPane.showMessageDialog(this, "修改失败!", "坏消息", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值