Java网络编程实践课程设计.doc_第1页
Java网络编程实践课程设计.doc_第2页
Java网络编程实践课程设计.doc_第3页
Java网络编程实践课程设计.doc_第4页
Java网络编程实践课程设计.doc_第5页
已阅读5页,还剩31页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java网络编程实践课程设计说明书课程名称:Java网络编程课程设计 题目: 基于C/S的学生信息管理 姓名: 学号: 教学班号: 指导教师: 王 小 刚 兰州交通大学软件科学与工程系二 13 年 12 月 27 日任务书实现应用,可以通过网络在客户端对存放在服务器端的学生信息表(至少包含学号、姓名、性别、年龄、籍贯等字段)学生纪录增、删、改、查,并返回结果。要能通过菜单选择功能。基于Socket,多客户端使用多线程,以能同时从多个客户端执行功能。必须提供友好直观、布局合理的图形界面选择功能、显示信息和填写修改信息。成员及分工列表图形界面设计者:数据库设计者: Socket设计者:多线程设计者:项目简述和目的熟悉JAVA 语法,掌握图形化界面、多线程、网络、数据库等综合编程,掌握用编程语言开发由若干功能组成的小型项目的基本流程,增强动手实践能力,巩固和加强书本知识的学习,体会JAVA编程的特点。解题基本思路设计的信息管理系统中的窗体部分,没有使用菜单,而是按钮。菜单虽然简单,方便,但我认为在窗体里运用按钮是有必要的,这样可以方便添加、删除,修改等动作,通过一步步地做,慢慢的集合,比较的清晰,还显得比较的有层次感. 利用Swing设计图形界面利用Access表设计数据库,利用socket设计网络信息管理模块 分析和设计学生管理系统要实现查询,添加,删除,显示,修改等功能。查询不存在要报错,添加相同也要报错,删除不存在的号也要报错,修改不成功也要报错。如果成功的话也要提示。而且每一步都要密码才能进行操作,否则报错提醒!主要程序源代码Client源代码package socket;import java.io.*;import .*;import java.awt.*;import javax.swing.*;import java.awt.event.*;class Client extends JFrame implements ActionListener/登入界面功能是输入正确的账号和密码才能进入,错误的话返回错误提示!private JLabel usernameLabel;/帐号private JLabel a;/标题private JTextField usernameTextField;/帐号文本框private JLabel passwordLabel;/密码private JPasswordField passwordField;/密码文本框private JButton button1;/确定按钮private JButton button2;/取消按钮public Client()super.setTitle(登录界面);Container c=getContentPane();c.setLayout(null);a=new JLabel(欢迎进入学生登入系统!);a.setBounds(50,30,300,30);a.setFont(new Font(黑色,Font.BOLD,25);usernameLabel=new JLabel(账号);usernameLabel.setBounds(50,90,170,30);usernameLabel.setFont(new Font(黑色,Font.BOLD,30);usernameTextField=new JTextField();usernameTextField.setBounds(150,90,170,30);usernameTextField.setFont(new Font(宋体,Font.BOLD,20);passwordLabel=new JLabel(密码:);passwordLabel.setBounds(50,120,170,30);passwordLabel.setFont(new Font(黑色,Font.BOLD,30);passwordField=new JPasswordField();passwordField.setBounds(150,120,170,30);passwordField.setFont(new Font(黑色,Font.BOLD,30);passwordField.setEchoChar(*);button1=new JButton();button1.setText(进入);button1.setFont(new Font(黑色,Font.BOLD,20);button1.setBounds(50,160,100,30);button2=new JButton();button2.setText(退出);button2.setFont(new Font(黑色,Font.BOLD,20);button2.setBounds(220,160,100,30);button1.addActionListener(this);/按钮监听button2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)System.exit(0););c.add(passwordLabel);c.add(passwordField);c.add(usernameLabel);c.add(button1);c.add(button2);c.add(usernameTextField);c.add(a);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(400,300);setLocation(322,30);setVisible(true); private void showText()/登入失败消息框 String xie; xie=usernameTextField.getText(); JOptionPane.showMessageDialog(this.getParent(),登入失败!+xie); private void showText1()/登入成功消息框 String xie; xie=usernameTextField.getText(); JOptionPane.showMessageDialog(this.getParent(),登入成功!+xie); public void actionPerformed(ActionEvent e)/按钮监听事件 Socket ssocket = null; String st1,st2,st3; final PrintWriter writer1; st1=usernameTextField.getText(); st2=passwordField.getText(); st3=进入+,+st1+,+st2;/把帐号和密码打包成字符串 try ssocket=new Socket(localHost,10000);/创建socketwriter1=new PrintWriter(ssocket.getOutputStream();/发送给服务器!if(!(st1.equals(null) & !(st2.equals(null)/判断是否为空writer1.println(st3);writer1.flush();else if(st1.equals(null) | st2.equals(null)/判断是否为空writer1.println(error1);writer1.flush();BufferedReader read=new BufferedReader(new InputStreamReader(ssocket.getInputStream();String s_red=null;System.out.println(please wait.);while(true) s_red=read.readLine(); if(s_red!=null) break;if(s_red.equals(yes)/读取服务端的内容,如果是yes执行Client1的类showText1();dispose();new Client1();else/读取服务端的内容,如果不是yes执行showText()报错提醒!showText();usernameTextField.setText(null); passwordField.setText(null); catch (Exception e1) e1.printStackTrace(); public static void main(String args)throws IOExceptionClient s=new Client(); class Client1 extends JFrame /登录界面的类包含查询,删除,增添,修改,返回等按钮!共4个按钮监听对象!private JButton button1,button2,button3,button4,button5,button6;public Client1()super.setTitle(登录界面);Container c=getContentPane();setLayout(null);button1=new JButton();button1.setText(查询);button1.setFont(new Font(黑色,Font.BOLD,20);button1.setBounds(50,50,120,50);button2=new JButton();button2.setText(删除);button2.setFont(new Font(黑色,Font.BOLD,20);button2.setBounds(220,50,120,50);button3=new JButton();button3.setText(增添);button3.setFont(new Font(黑色,Font.BOLD,20);button3.setBounds(50,150,120,50);button4=new JButton();button4.setText(修改);button4.setFont(new Font(黑色,Font.BOLD,20);button4.setBounds(220,150,120,50);button5=new JButton();button5.setText(显示);button5.setFont(new Font(黑色,Font.BOLD,20);button5.setBounds(50,250,120,50);button6=new JButton();button6.setText(返回);button6.setFont(new Font(黑色,Font.BOLD,20);button6.setBounds(220,250,120,50);button1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)dispose();new Client2(););button2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)dispose();new Client4();); button3.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)dispose();new Client3();); button4.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)dispose();new Client5();); button5.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)dispose();new Client6();); button6.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)dispose();new Client(););c.add(button1);c.add(button2);c.add(button3);c.add(button4);c.add(button5);c.add(button6);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(400,450);setLocation(322,30);setVisible(true); public static void main(String args)/throws IOException new Client1(); class Client2 extends JFrame implements ActionListener/查询类的功能是输入学号和密码,如果正确则返回成功的提醒,如果失败则返回失败的提醒,如果该学好存在,则返回存在的提醒!private JLabel usernameLabel1;private JLabel usernameLabel2;private JLabel usernameLabel3;private JLabel usernameLabel4;private JLabel usernameLabel5;private JLabel usernameLabel6;private JLabel usernameLabel7;private JLabel usernameLabel8;private JTextField usernameTextField1;private JTextField usernameTextField2;private JTextField usernameTextField3;private JTextField usernameTextField4;private JTextField usernameTextField5;private JTextField usernameTextField6;private JTextField usernameTextField7;private JTextField usernameTextField8;private JButton button1;private JButton button2;private JLabel label1; public Client2()super.setTitle(学生操作界面);Container c=getContentPane();c.setLayout(null); label1=new JLabel(欢迎进入学生查询系统!); label1.setBounds(40,20,400,30); label1.setFont(new Font(黑色,Font.BOLD,35); usernameLabel1=new JLabel(查询号);usernameLabel1.setBounds(70,110,200,30);usernameLabel1.setFont(new Font(黑色,Font.BOLD,30);usernameTextField1=new JTextField();usernameTextField1.setBounds(170,110,200,30);usernameTextField1.setFont(new Font(宋体,Font.BOLD,30);usernameLabel2=new JLabel(密码:);usernameLabel2.setBounds(70,150,200,30);usernameLabel2.setFont(new Font(黑色,Font.BOLD,32);usernameTextField2=new JTextField();usernameTextField2.setBounds(170,150,200,30);usernameTextField2.setFont(new Font(黑色,Font.BOLD,30);usernameLabel3=new JLabel(性别:);usernameLabel3.setFont(new Font(黑色,Font.BOLD,32);usernameLabel3.setBounds(70,190,200,30);usernameTextField3=new JTextField();usernameTextField3.setBounds(170,190,200,30);usernameTextField3.setFont(new Font(黑色,Font.BOLD,30);usernameLabel4=new JLabel(籍贯:);usernameLabel4.setBounds(70,230,200,30);usernameLabel4.setFont(new Font(黑色,Font.BOLD,30);usernameTextField4=new JTextField();usernameTextField4.setBounds(170,230,200,30);usernameTextField4.setFont(new Font(黑色,Font.BOLD,30);usernameLabel5=new JLabel(专业 :);usernameLabel5.setBounds(70,270,200,30);usernameLabel5.setFont(new Font(黑色,Font.BOLD,30);usernameTextField5=new JTextField();usernameTextField5.setBounds(170,270,200,30);usernameTextField5.setFont(new Font(黑色,Font.BOLD,30);usernameLabel6=new JLabel(姓名 :);usernameLabel6.setBounds(70,310,200,30);usernameLabel6.setFont(new Font(黑色,Font.BOLD,30);usernameTextField6=new JTextField();usernameTextField6.setBounds(170,310,200,30);usernameTextField6.setFont(new Font(黑色,Font.BOLD,30);usernameLabel7=new JLabel(电话:);usernameLabel7.setBounds(70,350,200,30);usernameLabel7.setFont(new Font(黑色,Font.BOLD,30);usernameTextField7=new JTextField();usernameTextField7.setBounds(170,350,200,30);usernameTextField7.setFont(new Font(宋体,Font.BOLD,30);usernameLabel8=new JLabel(QQ:);usernameLabel8.setBounds(70,390,200,30);usernameLabel8.setFont(new Font(黑色,Font.BOLD,30);usernameTextField8=new JTextField();usernameTextField8.setBounds(170,390,200,30);usernameTextField8.setFont(new Font(宋体,Font.BOLD,30);button1=new JButton();button1.setText(查询);button1.setFont(new Font(黑色,Font.BOLD,30);button1.setBounds(60,470,130,40);button2=new JButton();button2.setText(返回);button2.setFont(new Font(黑色,Font.BOLD,30);button2.setBounds(270,470,130,40);c.add(usernameLabel1);c.add(usernameLabel2);c.add(usernameLabel3);c.add(usernameLabel4);c.add(usernameLabel5);c.add(usernameLabel6);c.add(usernameLabel7);c.add(usernameLabel8);c.add(button1);c.add(button2);c.add(usernameTextField1);c.add(usernameTextField2);c.add(usernameTextField3);c.add(usernameTextField4); c.add(usernameTextField5);c.add(usernameTextField6);c.add(usernameTextField7);c.add(usernameTextField8);c.add(label1);button1.addActionListener(this);button2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)dispose();new Client1(););setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(470,580);setLocation(322,30);setVisible(true); public static void main(String args) Client2 s=new Client2(); private void showText() String xie; xie=usernameTextField1.getText(); JOptionPane.showMessageDialog(this.getParent(),查询成功!+xie); private void showText2() String xie; xie=usernameTextField1.getText(); JOptionPane.showMessageDialog(this.getParent(),该号不存在!+xie); public void actionPerformed(ActionEvent e) Socket ssocket = null; String str, st1,st2; final PrintWriter writer1; st1=usernameTextField1.getText(); st2=usernameTextField2.getText(); str=查询+,+st1+,+st2; try ssocket=new Socket(localHost,10000);writer1=new PrintWriter(ssocket.getOutputStream();if(!(st1.equals(null) & !(st2.equals(null)writer1.println(str);writer1.flush();else if(st1.equals(null) | st2.equals(null)writer1.println(error1);writer1.flush();BufferedReader read=new BufferedReader(new InputStreamReader(ssocket.getInputStream();String line=null;System.out.println(please wait.);line=read.readLine();if(line.equals(Not1)showText2();usernameTextField1.setText(null);usernameTextField2.setText(null);usernameTextField3.setText(null);usernameTextField4.setText(null);usernameTextField5.setText(null);usernameTextField6.setText(null);usernameTextField7.setText(null);usernameTextField8.setText(null);else String sarry=line.split(,);/ usernameTextField1.setText(sarry1); usernameTextField2.setText(sarry2); usernameTextField3.setText(sarry3); usernameTextField4.setText(sarry4); usernameTextField5.setText(sarry5); usernameTextField6.setText(sarry6); usernameTextField7.setText(sarry7); usernameTextField8.setText(sarry8); showText(); catch (Exception e1) e1.printStackTrace(); Server的源代码package socket;import java.io.*;import .*;import java.sql.*; public class Server /Server类通过引用来实现多线程public static void main(String args) Thread1 s=new Thread1();s.start();class Thread1 extends Thread/多线程类static ServerSocket server; static Socket ssocket; static BufferedReader reader1;static BufferedReader reader2;static String sql, databack;static String id,password,address,myname,class1,QQ,phone,sex,school; static ResultSet rs; static Statement stmt; static Connection conn; public void run()/多线程的run()方法 try final String db_driver=sun.jdbc.odbc.JdbcOdbcDriver; /连接多线程! final String db_url=jdbc:odbc:Driver=Microsoft Access Driver (*.mdb, *.accdb);DBQ=d:/my1.mdb;try catch(Exception e) e.printStackTrace(); try server=new ServerSocket(10000);/建立端口 System.out.println(服务器已经创建!等待客户机的链接.); int flag=0; int flag1=0; while(true) ssocket=server.accept();/连接客户端 System.out.println(完成链接!); reader1=new BufferedReader(new InputStreamReader(ssocket.getInputStream(); PrintWriter writer2=new PrintWriter(ssocket.getOutputStream(); String s1=reader1.readLine(); String sarry=s1.split(,); sql=select*from one;/连接数据库中的表名 Class.forName(db_driver); conn=DriverManager.getConnection(db_url,); if(conn!=null) System.out.println(数据库已连接.); else System.out.println(连接失败!); stmt=conn.createStatement(); rs=stmt.executeQuery(sql); if(sarry0.equals(进入)/获取客户端的进入功能 while(rs.next() id=rs.getString(ID); password=rs.getString(password); if(sarry1.equals(id) & sarry2.equals(password) int number1=Integer.parseInt(id); int number2=Integer.parseInt(password); if(number1=201211016 & number2=16) flag=1; break; if(flag=1) writer2.println(yes); writer2.flush(); flag=0; else writer2.println(no); writer2.flush(); stmt.close(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论