nwafu

学生信息系统界面的实现 - JDBC

writer:pprp

登录界面的实现:

分为两个部分:

1、LoginFrame.java :

用windowbuilder进行快速搭建界面,构建好登录的界面,并用LogConnection类构建对象,进行处理,增加监听器。

2、LogConnection.java:

用基本的JDBC步骤进行处理,其中多加了一个判断用户名和密码匹配问题。

注意的问题:

1、只有在ResultSet对象使用完毕以后才能关掉Connection对象,否则会影响数据的传输,最后将连接关闭。

2、另外要注意在使用ResultSet进行结果查询的时候,需要加入一个判断,if(!rs.next())return ;否则会报错。

3、注意close函数应该在LogConnection中写,在LoginFrame的监听器中调用关闭。

LoginFrame.java

package work2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import work3.StudentFrame;

@SuppressWarnings("serial")
public class LoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField userFiled;
	private JPasswordField pwd;
	private LogConnection lc = null;

	public static void main(String[] args) {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		LoginFrame frame = new LoginFrame();
		frame.setVisible(true);
	}

	public LoginFrame() {
		setTitle("学生信息管理系统");
		lc = new LogConnection();
		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);

		JLabel lblNewLabel = new JLabel("u7528u6237u540DuFF1A");
		lblNewLabel.setBounds(63, 68, 54, 15);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("u5BC6  u7801uFF1A");
		lblNewLabel_1.setBounds(63, 128, 54, 15);
		contentPane.add(lblNewLabel_1);

		userFiled = new JTextField();
		userFiled.setBounds(127, 65, 224, 23);
		contentPane.add(userFiled);
//		userFiled.setColumns(10);

		pwd = new JPasswordField();
		pwd.setBounds(127, 124, 224, 23);
		contentPane.add(pwd);

		JButton loginBtn = new JButton("u786Eu5B9A");
		loginBtn.setBounds(63, 204, 93, 23);
		contentPane.add(loginBtn);

		JButton cancelBtn = new JButton("u53D6u6D88");
		cancelBtn.setBounds(268, 204, 93, 23);
		contentPane.add(cancelBtn);

		loginBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String name = userFiled.getText().trim();
				String pwdText = String.valueOf(pwd.getPassword()).trim();
				if (lc.Judge(name, pwdText)) {
					JOptionPane.showMessageDialog(LoginFrame.this, "欢迎您,"
							+ name);
					@SuppressWarnings("unused")		
					StudentFrame tmp = new StudentFrame();
					tmp.setVisible(true);
				} else {
					JOptionPane.showMessageDialog(LoginFrame.this, "用 户名或密码错误");
				}
			}

		});
		cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(0);
				lc.close();
			}

		});
		JOptionPane.showMessageDialog(LoginFrame.this, "欢迎登陆学生信息管理系统,请输入账号密码");
	}
}

LogConnection.java

package work2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LogConnection {
	Connection con = null;
	Statement sql = null;

	public LogConnection() {
		// 1 drive
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("驱动加载成功");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动加载失败");
			e.printStackTrace();
		}
		// 2 connect

		String url = "jdbc:mysql://localhost/test?useSSL=true";
		String user = "root";
		String password = "root";
		try {
			con = DriverManager.getConnection(url, user, password);
			System.out.println("链接成功");
		} catch (SQLException e) {
			System.out.println("链接失败");
			e.printStackTrace();
		}
		// 3 statement object
		try {
			sql = con.createStatement();
			System.out.println("成功生成statement对象");
		} catch (SQLException e) {
			System.out.println("生成statement对象失败");
			e.printStackTrace();
		}

	}

	public boolean Judge(String name, String passwd) {
		ResultSet rs = null;
		String str = "select * from log where logname = '" + name + "'";
		try {
			rs = sql.executeQuery(str);
			if (!rs.next())
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		try {
			String cmpPwd = rs.getString(3);
			if (cmpPwd.equals(passwd))
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public void close(){
		try {
			con.close();
			System.out.println("关闭成功");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("关闭失败");
		}
	}
}

查询界面的实现:

要求:

设计学生信息管理系统。

使用Navicat的test数据库,创建Student表,包含学生的学号、姓名、年龄信息。根据以下的功能,编写相应的函数:

① 根据学号,可以查询到学生的姓名和年龄;

② 给定学生的学号、姓名、年龄,在表中追加一行信息;

③ 给定学生的学号,可以从表中删除该学生的信息;
使用图形界面实现任务三的学生信息管理系统设计。

分析:

用到上一个部分的登录界面,在登录界面的ActionListener中处理,如果成功登录,则打开查询、更新、删除界面进行操作。

Student.java: 其中是关于JDBC编程内容,为三个需求设计了三个函数,分别实现三个功能要求。

StudentFrame.java: 界面的设计,为四个按钮注册监听器,综合实现三种功能。

知识点:

1、用到的SQL语句比较多,要注意使用的时候需要在适当的时候加双引号,例如:
"insert into Student(sname,age)
values('"+name+"',"+age+")"
其中name和age都是变量。

2、注意数据库中编号是从1开始的,不是从0开始。

3、executeUpdate语句:是执行增删改的操作。
executeQuery语句:是执行查询操作的。

StudentFrame.java

package work3;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import work2.LoginFrame;

@SuppressWarnings("serial")
public class StudentFrame extends JFrame {

	private JPanel contentPane;
	private JTextField searchStuNo;
	private JTextField NameTextField;
	private JTextField AgeTextField;
	LoginFrame lf = null;
	Student stu = null;

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e){
			e.printStackTrace();
		}
		StudentFrame frame = new StudentFrame();	
		frame.setVisible(true);
	}

	public StudentFrame() {
		stu = new Student();
		
		setTitle("学生信息查询系统");
		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);
		contentPane.setBackground(Color.PINK);

		JLabel label = new JLabel("u67E5u8BE2u7684u5B66u53F7uFF1A");
		label.setBounds(25, 29, 100, 27);
		contentPane.add(label);

		searchStuNo = new JTextField();
		searchStuNo.setBounds(135, 32, 218, 24);
		contentPane.add(searchStuNo);
		searchStuNo.setColumns(10);

		JButton SearchBtn = new JButton("u67E5u8BE2");
		SearchBtn.setBounds(94, 66, 77, 23);
		contentPane.add(SearchBtn);

		JButton DeleteBtn = new JButton("u5220u9664");
		DeleteBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		DeleteBtn.setBounds(276, 66, 77, 23);
		contentPane.add(DeleteBtn);

		JLabel label_1 = new JLabel(
				"u52A0u5165u5B66u751Fu4FE1u606FuFF1A");
		label_1.setBounds(25, 138, 100, 37);
		contentPane.add(label_1);

		NameTextField = new JTextField();
		NameTextField.setBounds(177, 146, 58, 21);
		contentPane.add(NameTextField);
		NameTextField.setColumns(10);

		AgeTextField = new JTextField();
		AgeTextField.setBounds(286, 146, 58, 21);
		contentPane.add(AgeTextField);
		AgeTextField.setColumns(10);

		JLabel label_2 = new JLabel("u59D3u540D:");
		label_2.setBounds(135, 149, 36, 15);
		contentPane.add(label_2);

		JLabel label_3 = new JLabel("u5E74u9F84:");
		label_3.setBounds(251, 149, 54, 15);
		contentPane.add(label_3);

		JButton SureBtn = new JButton("u786Eu5B9A");
		SureBtn.setBounds(94, 203, 77, 23);
		contentPane.add(SureBtn);

		JButton ExitBtn = new JButton("u9000u51FA");
		ExitBtn.setBounds(276, 203, 77, 23);
		contentPane.add(ExitBtn);

		SearchBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int ssno = Integer.parseInt(searchStuNo.getText());
				String tmp = stu.selectNameAge(ssno);
				JOptionPane.showMessageDialog(StudentFrame.this, tmp);
				searchStuNo.setText("");
			}
		});
		DeleteBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int ssno = Integer.parseInt(searchStuNo.getText());
				if(stu.DeleteInf(ssno)){
					JOptionPane.showMessageDialog(StudentFrame.this, "删除成功!");
				}else {
					JOptionPane.showMessageDialog(StudentFrame.this, "删除失败!");
				}
				searchStuNo.setText("");
			}
		});

		ExitBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		SureBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String ssname = NameTextField.getText();
				int sage = Integer.parseInt(AgeTextField.getText());
				stu.add(ssname,sage);
				NameTextField.setText("");
				AgeTextField.setText("");
				JOptionPane.showMessageDialog(StudentFrame.this, "插入成功!");
			}
		});
	}
}


Student.java

package work3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Student {
	Connection con = null;
	Statement sql = null;
	ResultSet rs = null;

	public Student() {
		// 1 drive
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("驱动加载成功");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动加载失败");
			e.printStackTrace();
		}
		// 2 connect
		String url = "jdbc:mysql://localhost/test?useSSL=true";
		String user = "root";
		String password = "root";
		try {
			con = DriverManager.getConnection(url, user, password);
			System.out.println("链接成功");
		} catch (SQLException e) {
			System.out.println("链接失败");
			e.printStackTrace();
		}
		// 3 statement object
		try {
			sql = con.createStatement();
			System.out.println("成功生成statement对象");
		} catch (SQLException e) {
			System.out.println("生成statement对象失败");
			e.printStackTrace();
		}
	}
	
	public String selectNameAge(int sno){
		
		String sqls = "select sname,age from student where sno = '"+String.valueOf(sno)+"'";
		try {
			 rs = sql.executeQuery(sqls);
			if(!rs.next()){
				return ("不存在!");
			}
			System.out.println("查询成功");
			String stuname = rs.getString(1);
			int stuage = rs.getInt(2);
			return  ("学号为"+sno+"的学生的姓名为:"+stuname+" ,年龄为:" + stuage);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("查询失败");
			e.printStackTrace();
		}	
		return "不存在";
	}
	
	public void add(String name,int age){
		String str = "insert into Student(sname,age) values('"+name+"',"+age+")";
		try {
			sql.executeUpdate(str);
			System.out.println("插入成功");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("插入失败");
			e.printStackTrace();
		}
	}
	
	public boolean DeleteInf(int no){
		String str = "delete from Student where sno = "+no+"";
		try {
			sql.executeUpdate(str);
			System.out.println("删除成功");
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("删除失败");
			e.printStackTrace();
		}
		return false;
	}
}


如果感觉有帮助,请点个赞
原文地址:https://www.cnblogs.com/pprp/p/8085510.html