Java《网络编程》课程设计——基于C/S的学生基本信息管理系统

一、系统功能要求

实现学生的基本信息的管理,如添加、修改、删除、浏览和查询等操作。基本信息主要包括学号、姓名、性别、出生日期、专业、班级、入学年份、家庭住址、联系电话等;在进行信息查询时要提供基于学号/姓名/班级等的单条件或多条件的查询操作;设计一个可供个人查询个人信息的客户端程序;利用MySQL数据库实现信息存储。

二、代码

这是一个简易版,大体功能都实现了,只是不详细。
里面的ip地址需要根据自己的ip改一下,数据库建库的可以跟着代码建一下。

1.客户端

ClientMain.java

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataOutputStream;

import java.io.IOException;
import java.io.OutputStream;

import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class ClientMain extends JFrame {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClientSend.connectServer();
		new ClientMain();
	}

	private JPanel contentPane = new JPanel();
	private JTable table = new JTable();
	private DefaultTableModel tablemode=new DefaultTableModel();
	private JScrollPane scrollPane = new JScrollPane();
	JTextField id_TextField=new JTextField();

	public ClientMain() {

		setBounds(250, 50, 1000, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout());

		setTitle("学生信息管理系统");
		JSplitPane splitPane1 = new JSplitPane();

		splitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		splitPane1.setDividerLocation(600);
		splitPane1.setTopComponent(scrollPane);	

		/查询面板
		JPanel pane2=new JPanel();
		pane2.setLayout(null);

		JLabel id_label_2 = new JLabel("学号");
		id_label_2.setFont(new Font("宋体", Font.PLAIN, 16));
		id_label_2.setBounds(20, 20, 36, 16);
		pane2.add(id_label_2);

		id_TextField.setBounds(70, 14, 100, 31);
		pane2.add(id_TextField);

		JButton selectbutton = new JButton("查询");
		selectbutton.setFont(new Font("宋体", Font.PLAIN, 16));
		selectbutton.setBounds(65, 150, 93, 23);
		selectbutton.addActionListener(new selectAction());
		pane2.add(selectbutton);
		splitPane1.setRightComponent(pane2);

		///表格
		String[] tabletitle={"学号","姓名","性别","专业","电话"};
		table.setModel(tablemode);
		for(int j=0;j<5;j++) {
			tablemode.addColumn(tabletitle[j]);	
		}
		
		ClientSend.ss(tablemode);
		
		scrollPane.setViewportView(table);

		contentPane.add(splitPane1);//切分窗格
		setVisible(true);
	}

	//查询
	class selectAction implements ActionListener{

		public void actionPerformed(ActionEvent e) {

			String name=id_TextField.getText();
			if(name.isEmpty()) {
				return;
			}else {
				String mess="查询"+"##"+name;
				ClientSend.sending(mess);
			}
		}
	}
}

ClientRecive.java

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import javax.swing.table.DefaultTableModel;

public class ClientRecive extends Thread{
	Socket sock;
	DefaultTableModel tablemode;
	ClientRecive(Socket sock){
		this.sock=sock;
	}
	ClientRecive(Socket sock,DefaultTableModel tablemode){
		this.sock=sock;
		this.tablemode=tablemode;
	}

	public void run() {
		try {
			InputStream is=sock.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			while(true) {
				String mess=dis.readUTF();
				String message=sock.getInetAddress().toString();
				System.out.println(mess);
				String reciveSelect[]=mess.split("##");
				tablemode.getDataVector().clear();
				System.out.println(reciveSelect.length);
				ClientStudent select_stu;
				for(int i=0;i<((int)reciveSelect.length/5);i++) {
					
					select_stu = new ClientStudent(reciveSelect[1+(i*5)],reciveSelect[2+(i*5)],
							reciveSelect[3+(i*5)],reciveSelect[4+(i*5)],
							reciveSelect[5+(i*5)]);
					
					tablemode.addRow(select_stu.toVector());
				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//			Server.socks.remove(sock);
		}
	}
}

ClientSend.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.table.DefaultTableModel;

public class ClientSend {
	static Socket sock;
	public static void connectServer() {
		
		try {
			System.out.println("客户端连接......");
			sock= new Socket("192.168.56.1",50009);
			System.out.println("客户端连接成功咯");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//建立连接
		
	}
	
	
	public static void ss(DefaultTableModel tablemode) {
		ClientRecive myThread=new ClientRecive(sock,tablemode); //创建客户端线程
		myThread.start();//开始运行线程
	}

	public static void sending(String allmessage) {
		try {			
			
			OutputStream os =sock.getOutputStream();//sock的输出流
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(allmessage);
			System.out.println("数据发送完毕!");
			
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}

ClientStudent.java

import java.util.Vector;

public class ClientStudent {

	private String name;
	private String id;
	private String sex;
	private String dep;
	private String phone;

	public ClientStudent(String name, String id, String sex, String dep, String phone) {
		super();
		this.name = name;
		this.id = id;
		this.sex = sex;
		this.dep = dep;
		this.phone = phone;
	}


	public Vector<Object> toVector() {
		// TODO Auto-generated method stub
		Vector<Object> vect=new Vector();
		vect.add(name);
		vect.add(id);
		vect.add(sex);
		vect.add(dep);
		vect.add(phone);
	
		return vect;
	}
	
}

2.服务器

ServerMain.java

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class ServerMain extends JFrame{

	private JPanel contentPane = new JPanel();
	private JScrollPane scrollPane = new JScrollPane();

	static JTextField name_TextField=new JTextField();
	static JTextField price_JTextField=new JTextField();
	static JTextField kind_TextField=new JTextField();
	JTextField name_TextField_2=new JTextField();

	JTabbedPane downpane=new JTabbedPane();
	JPopupMenu jpm = new JPopupMenu();

	int selindex=0;
	int selcolum=0;

	static JTextField kuncun_TextField=new JTextField();
	static JTextField com_TextField=new JTextField();

	JTextField phone_TextField=new JTextField();

	static List<Socket> socks=Collections.synchronizedList(new ArrayList<Socket>());//ip列表
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("服务器开始运行了!");
			ServerSQL.connectSQL();
			ServerSocket ss = new ServerSocket(50009);  
			
			create();
			
			while(true) {
				Socket sock=ss.accept();   
				System.out.println(sock.getInetAddress()+"连接成功"+sock.getPort());
				socks.add(sock);	//将连接的客户端记录到list里
				ServerThread st=new ServerThread(sock); //接收线程   创建对象的时候调用构造方法
				st.start();         //启动
				System.out.println("1个线程已启动");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void create() {
		JFrame pane=new JFrame();
		
		
		JPanel pane1 = new JPanel();
		pane1.setLayout(null);

		JLabel name_label = new JLabel("姓名");
		name_label.setFont(new Font("宋体", Font.PLAIN, 16));
		name_label.setBounds(20, 70, 36, 16);
		pane1.add(name_label);

		name_TextField.setBounds(70, 64, 100, 31);
		pane1.add(name_TextField);

		JLabel kind_label = new JLabel("学号");
		kind_label.setFont(new Font("宋体", Font.PLAIN, 16));
		kind_label.setBounds(20, 20, 36, 16);
		pane1.add(kind_label);

		kind_TextField.setBounds(70, 14, 100, 31);
		pane1.add(kind_TextField);

		JLabel com_Label=new JLabel("性别");
		com_Label.setBounds(20,120,36,16);
		com_Label.setFont(new Font("宋体", Font.PLAIN, 16));
		pane1.add(com_Label);

		com_TextField.setBounds(70, 114, 100, 31);
		pane1.add(com_TextField);

		JLabel price_Label = new JLabel("专业");
		price_Label.setFont(new Font("宋体", Font.PLAIN, 16));
		price_Label.setBounds(20, 170, 36, 16);
		pane1.add(price_Label);

		price_JTextField.setBounds(70, 164, 100, 31);
		pane1.add(price_JTextField);

		JLabel kucun_Label = new JLabel("电话");
		kucun_Label.setFont(new Font("宋体", Font.PLAIN, 16));
		kucun_Label.setBounds(20, 220, 36, 16);
		pane1.add(kucun_Label);

		kuncun_TextField.setBounds(70, 214, 100, 31);
		pane1.add(kuncun_TextField);

		JButton button = new JButton("录入");
		button.setFont(new Font("宋体", Font.PLAIN, 16));
		button.setBounds(65, 260, 93, 23);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name = name_TextField.getText();
				String id = kind_TextField.getText();
				String sex = com_TextField.getText();
				String dep=price_JTextField.getText();
				String phone=kuncun_TextField.getText();

				ServerSQL.insert_SQL(name,id,sex,dep,phone);
				

			}
		});
		pane1.add(button);


		JSplitPane sp = new JSplitPane();
		sp.setOrientation(JSplitPane.HORIZONTAL_SPLIT);

		sp.setLeftComponent(pane1);
		sp.setDividerLocation(200);
		
		JScrollPane js = new JScrollPane();
		JTable table =new JTable();
		DefaultTableModel tablemode = new DefaultTableModel();
		tablemode.addColumn("姓名");
		tablemode.addColumn("学号");
		tablemode.addColumn("性别");
		tablemode.addColumn("专业");
		tablemode.addColumn("电话");
		table.setModel(tablemode);
		js.setViewportView(table);
		
		
		JButton scanbutton = new JButton("浏览");
		scanbutton.setFont(new Font("宋体", Font.PLAIN, 16));
		scanbutton.setBounds(65, 310, 93, 23);
		scanbutton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ServerSQL.scan_SQL(tablemode);
			}
		});
		pane1.add(scanbutton);
		
		JButton deletebutton = new JButton("删除");
		deletebutton.setFont(new Font("宋体", Font.PLAIN, 16));
		deletebutton.setBounds(65, 360, 93, 23);
		pane1.add(deletebutton);
		deletebutton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int row=table.getSelectedRow();
				
				String id = (String)table.getValueAt(row, 1);
				tablemode.removeRow(row);
				ServerSQL.delete_SQL(id);
			}
		});
		
		JButton upbutton = new JButton("更新");
		upbutton.setFont(new Font("宋体", Font.PLAIN, 16));
		upbutton.setBounds(65, 410, 93, 23);
		pane1.add(upbutton);
		upbutton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name = name_TextField.getText();
				String id = kind_TextField.getText();
				String sex = com_TextField.getText();
				String dep=price_JTextField.getText();
				String phone=kuncun_TextField.getText();
				
				ServerSQL.update_SQL(name,id,sex,dep,phone);
			}
		});
		
		
		sp.setRightComponent(js);
		pane.setLayout(new BorderLayout());
		pane.setBounds(100,100,800,500);
		pane.setContentPane(sp);
		pane.setTitle("xxx&&xxx");
		pane.setDefaultCloseOperation(EXIT_ON_CLOSE);
		pane.setVisible(true);
	}
	
}





class ServerThread extends Thread{  //接收线程
	Socket sock;
	OutputStream os;
	DataOutputStream dos;
	ServerThread(Socket sock){  //构造方法
		this.sock=sock;
		
		
		try {
			os = sock.getOutputStream();
			dos = new DataOutputStream(os);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public void run() {   //重写Thread类里的run方法
		try {
			InputStream is=sock.getInputStream();          
			DataInputStream dis = new DataInputStream(is); 
			while(true) {
				String mess=dis.readUTF();   //字符集
				String message=sock.getInetAddress().toString();
				System.out.println(message+":"+mess);

				String[] clientMess = mess.split("##");//以##号切分字符串
				System.out.println(clientMess[0]);

				if(clientMess[0].equals("查询")) {   //插入操作

					String smess=ServerSQL.select_SQL(clientMess);    //调用静态方法  插入
					OutputStream os = sock.getOutputStream();
					DataOutputStream ds = new DataOutputStream(os);
					ds.writeUTF(smess);

				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("一个客户端下线了!");
			ServerMain.socks.remove(sock);
		}
	}
}

ServerSQL.java


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

import javax.swing.table.DefaultTableModel;

public class ServerSQL {
	static Connection con;
	public static void connectSQL() {
		try {
			Class.forName("com.mysql.jdbc.Driver");  //加载数据库的驱动程序
			String url="jdbc:mysql://localhost:3306/wlbc_ks?autoReconnect=true&useSSL=false";  //连接字符串
			String usename="root"; //用户名
			String psw="root";   //密码
			con = DriverManager.getConnection(url,usename,psw);  //和数据库建立连接
			System.out.println("数据库连接成功!");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//			e.printStackTrace();
			System.out.println("数据库连接成功");
		}
	}


	public static void insert_SQL(String one,String two,String three,String four,String five) {
		String insert = "insert into t_xu values (?,?,?,?,?)";

		try {
			PreparedStatement stat = con.prepareStatement(insert);  //预编译
			stat.setString(1,one); //对通配符进行设置
			stat.setString(2,two);
			stat.setString(3,three);
			stat.setString(4,four);
			stat.setString(5,five);
			stat.executeUpdate();  //执行SQL语句
			System.out.println(con);


		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String select_SQL(String mess[]) {

		try {
			String select="select * from t_xu where id=\'"+mess[1]+"\'";
			System.out.println("执行"+select);
			PreparedStatement stat = con.prepareStatement(select);
			ResultSet res=stat.executeQuery();
			String messs ="查询信息##";

			while(res.next()) {
				messs += res.getString(1)+"##"+res.getString(2)+"##"+res.getString(3)+"##"
						+res.getString(4)+"##"+res.getString(5)+"##";

			}
			System.out.println(messs+"--查询成功");

			return messs;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void delete_SQL(String id) {
		try {
			String delete="delete from t_xu where id=\'"+id+"\'";
			System.out.println("执行SQL语句"+delete);
			PreparedStatement stat=con.prepareStatement(delete);
			stat.executeUpdate();
			System.out.println("删除成功!");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static String scan_SQL(DefaultTableModel tb) {
		try {
			String select="select * from t_xu";
			PreparedStatement stat=con.prepareStatement(select);
			ResultSet res=stat.executeQuery();
			String messs="浏览信息##";
			tb.getDataVector().clear();
			while(res.next()) {
				
				ServerStudent ss = new ServerStudent(res.getString(1),res.getString(2), res.getString(3), res.getString(4),res.getString(5));

				tb.addRow(ss.toVector());
			}

			System.out.println("浏览成功!");

			return messs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public static void update_SQL(String one,String two,String three,String four,String five) {

		try {

			String update="update t_xu set name=\'"+one+"\',id=\'"
					+two+"\', sex=\'"+three+"\',dep=\'"+four+"\',phone=\'"+five
					+"\' where id=\'"+two+"\'";
		
			PreparedStatement stat = con.prepareStatement(update);
			int c=stat.executeUpdate();
			System.out.println(update);
			System.out.println("数据库修改成功"+c);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

ServerStudent.java

import java.util.Vector;

public class ServerStudent {

	private String name;
	private String id;
	private String sex;
	private String dep;
	private String phone;

	public ServerStudent(String name, String id, String sex, String dep, String phone) {
		super();
		this.name = name;
		this.id = id;
		this.sex = sex;
		this.dep = dep;
		this.phone = phone;
	}

	public Vector<Object> toVector(){
		Vector<Object> vect = new Vector();
		vect.add(name);
		vect.add(id);
		vect.add(sex);
		vect.add(dep);
		vect.add(phone);
		return vect;
	}

}

  • 7
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值