A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘一锋 黑马帝   /  2011-10-7 23:39  /  2687 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. class Employee
  3.         { private String name;
  4.         private char sex;
  5.         private int birthday;
  6.         private String degree;
  7.         private String title;
  8.         private String address;
  9.         private String telephone;

  10.        
  11.         public Employee(String name , char sex, int birthday, String degree, String title, String telephone)
  12.                 {        this.name =name;
  13.                         this.sex =sex;
  14.                         this.birthday =birthday;
  15.                         this.degree =degree;
  16.                         this.title =title;
  17.                         this.title =title;
  18.                         this.telephone=telephone;
  19.                         }
  20.         public Employee(String name)
  21.         {
  22.                 this.name =name;
  23.                
  24.         }
  25. public void setName(String name)
  26.                 {        this.name =name; }
  27.         public void setSex(char sex)
  28.                 {        this.sex =sex; }
  29.         public void setBirthday(int birthday)
  30.                 {this.birthday =birthday;        }
  31.         public void setDegree(String degree)
  32.                 {        this.degree =degree;}
  33.         public void setTitle(String title)
  34.                 {        this.title =title;}
  35.         public void setAddress(String address, String title)
  36.                 {        this.title =title;}
  37.         public void setTelephone(String telephone)
  38.                 {        this.telephone=telephone;}

  39.                          
  40.         public String getName()
  41.         {
  42.                 return name;
  43.         }
  44.                            
  45.         public char getSex()
  46.         {
  47.                 return sex;
  48.         }
  49.                          
  50.         public int getBirthday()
  51.         {
  52.                 return birthday;
  53.         }
  54.         public String getDegree()
  55.         {
  56.                 return degree;
  57.         }
  58.                         
  59.         public String getTitle()
  60.         {
  61.                 return title;
  62.         }
  63.                         
  64.         public String getAddress()
  65.         {
  66.                 return address;
  67.         }
  68.                         
  69.         public String getTelephone()
  70.         {
  71.                 return telephone;
  72.         }
  73.             }

  74. class EmployManager     
  75.         {
  76.         private static HashMap<String, Object> EmployeeListHT = new HashMap<String, Object>();

  77.         public static boolean putEmployee(String employee,Object obj)
  78.                 {
  79.                         EmployeeListHT.put( employee,obj);
  80.                                 return true;       
  81.                 }

  82.         public void putEmployee(Employee employee, Object object, int i, String string2, String string3, String string4, String string5, String string6) {
  83.                 // TODO Auto-generated method stub
  84.                
  85.         }

  86.         public Set<?> keySet() {
  87.                 // TODO Auto-generated method stub
  88.                 return null;
  89.         }


  90. /*        public static boolean removeEmployee(String name)
  91.                 {
  92.                         EmployeeListHT.remove(name);
  93.                                 return true;
  94.                 }
  95.         public static Employee getEmployee(String name )
  96.                 {       
  97.                         EmployeeListHT.get(name)        ;               
  98.                         return Employee;
  99.                 }*/

  100.         }       
  101. class CommandStage
  102. {       
  103.         public static void sop(Object obj)
  104.         {System.out.println(obj);}

  105.         @SuppressWarnings("unchecked")
  106.         public static void main(String[] srgs)
  107.         {       
  108.                 EmployManager em = new EmployManager();
  109.        
  110.                 em.putEmployee(new Employee("刘一锋"),1,22,"22","2","22","22","22");
  111.                  Set<Employee> keySet = (Set<Employee>) em.keySet();
  112.                
  113.                 Iterator<Employee> it = keySet.iterator();
  114.                        
  115.                         while(it.hasNext())
  116.                         {
  117.                                 Employee emp =(Employee) it.next();
  118.                                 sop(emp);
  119.                         }
  120.         }

  121.        
  122.         }       
复制代码
回复 使用道具 举报
/*
职工管理系统
需求,实现对某单位职工信息的管理,包括职工信息的增加,删除,查询功能,
1,职工信息包括
private String name;姓名
        private char sex;性别
        private int birthday;出生年月
        private String degree;学历
        private String title;职务
        private String address;地址
        private String telephone;联系电话
2新增职工功能,键盘录入新职工信息
3,删除职工,输入职工姓名,将职工从列表中删除
4查询,输入姓名,显示该员工的信息       
(菜鸟写了点,可是总报错,所以向大家求助,不用GUI,)
Y应该是创建三个类,一个是职工类,一个是职工管理类,另一个是控制台类包含主函数的,
职工信息储存室用哈希表的*/
import java.util.*;
class Employee {
        private String name;
        private char sex;
        private int birthday;
        private String degree;
        private String title;
        private String address;
        private String telephone;
       
       
        public void setName(String name)
                {        this.name =name;}
        public void setSex(char sex)
                {        this.sex =sex; }
        public void setBirthday(int birthday)
                {this.birthday =birthday;        }
        public void setDegree(String degree)
                {        this.degree =degree;}
        public void setTitle(String title)
                {        this.title =title;}
        public void setAddress(String address)
                {        this.address =address;}
        public void setTelephone(String telephone)
                {        this.telephone=telephone;}

                         
        public String getName()
        {
                return name;
        }
                           
        public char getSex()
        {
                return sex;
        }
                         
        public int getBirthday()
        {
                return birthday;
        }
        public String getDegree()
        {
                return degree;
        }
                        
        public String getTitle()
        {
                return title;
        }
                        
        public String getAddress()
        {
                return address;
        }
                        
        public String getTelephone()
        {
                return telephone;
        }
            }

class EmployManager   //extends Employee      
        {
        private static Hashtable EmployeeListHT = new Hashtable();

        public static boolean addEmployee(Employee )
                {
                        EmployeeListHT.add(Employee);
                                return true;       
                }


        public static boolean addEmployee(String name)
                {
                        EmployeeListHT.remove(neme);
                                return true;
                }
        public static Employee getEmployee(String name )
                {       
                        EmployeeListHT.get(neme)        ;               
                        return Employee;
                }

        }       
public class CommandStage// extends EmployManager
{       
        public static void sop(Object obj)
        {System.out.println(obj);}
        public static void main(String[] srgs)
        {       
                EmployManager em= new EmployManager();
                em.addEmployee(Employee("刘一锋"));
                sop(em);
        }       
}
//错误太多了,又不会做,愁思我了,

评分

参与人数 1技术分 +2 黑马币 +10 收起 理由
李印东老师 + 2 + 10 继续,加油!

查看全部评分

9 个回复

倒序浏览
Student.java
  1. package com.linguofeng.model;

  2. import java.io.Serializable;

  3. /**
  4. * 学生类
  5. *
  6. * @author 林国锋
  7. * @since 2011-9-23 {@link http://www.linguofeng.com}
  8. *
  9. */
  10. public class Student implements Serializable {
  11.         private static final long serialVersionUID = 1L;

  12.         private int id;// 学生编号
  13.         private String name;// 学生姓名
  14.         private String num;// 学生学号
  15.         private int sex;// 学生性别 1为男 0为女

  16.         public Student() {
  17.         }

  18.         public Student(int id, String name, String num, int sex) {
  19.                 this.id = id;
  20.                 this.name = name;
  21.                 this.num = num;
  22.                 this.sex = sex;
  23.         }

  24.         public int getId() {
  25.                 return id;
  26.         }

  27.         public void setId(int id) {
  28.                 this.id = id;
  29.         }

  30.         public String getName() {
  31.                 return name;
  32.         }

  33.         public void setName(String name) {
  34.                 this.name = name;
  35.         }

  36.         public String getNum() {
  37.                 return num;
  38.         }

  39.         public void setNum(String num) {
  40.                 this.num = num;
  41.         }

  42.         public int getSex() {
  43.                 return sex;
  44.         }

  45.         public void setSex(int sex) {
  46.                 this.sex = sex;
  47.         }

  48.         @Override
  49.         public String toString() {
  50.                 return "Student:@id:" + this.id + "\t@name:" + this.name + "\t@num:"
  51.                                 + this.num + "\t@sex:" + ((this.sex == 0) ? "女" : "男");
  52.         }
  53. }
复制代码
StudentDao.java
  1. package com.linguofeng.dao;

  2. import java.util.TreeMap;

  3. import com.linguofeng.model.Student;

  4. /**
  5. * 学生对象操作接口类
  6. *
  7. * @author 林国锋
  8. * @since 2011-9-24 {@link http://www.linguofeng.com}
  9. *
  10. */
  11. public interface StudentDao {
  12.         /**
  13.          * 添加学生对象
  14.          *
  15.          * @param stuMap
  16.          *            学生对象集合
  17.          */
  18.         public void addStu(TreeMap<String, Student> stuMap);
  19.         /**
  20.          * 更新学生对象
  21.          *
  22.          * @param stuMap
  23.          *            新的学生对象集合
  24.          */
  25.         public void updateStu(TreeMap<String, Student> stuMap);
  26.         /**
  27.          * 查找所有学生对象
  28.          *
  29.          * @return 所有学生对象集合
  30.          */
  31.         public TreeMap<String, Student> findAll();
  32.         /**
  33.          * 删除所有学生对象
  34.          */
  35.         public void delAll();
  36.         /**
  37.          * 根据学生学号删除学生对象
  38.          *
  39.          * @param num
  40.          */
  41.         public boolean delByNum(String num);
  42.         /**
  43.          * 根据学生学号查询学生对象信息
  44.          *
  45.          * @param num
  46.          * @return
  47.          */
  48.         public Student findByNum(String num);
  49.         
  50.         public void updateByNum(String num,Student student);
  51. }
复制代码
StudentDao.java
  1. package com.linguofeng.dao.impl;

  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.util.Iterator;
  9. import java.util.TreeMap;

  10. import com.linguofeng.dao.StudentDao;
  11. import com.linguofeng.model.Student;

  12. /**
  13. * 学生对象操作实现类
  14. *
  15. * @author 林国锋
  16. * @since 2011-9-24 {@link http://www.linguofeng.com}
  17. *
  18. */
  19. public class StudentDaoImpl implements StudentDao {
  20.         private static final String file = "student.dat";

  21.         /**
  22.          * 添加学生
  23.          */
  24.         @Override
  25.         public void addStu(TreeMap<String, Student> stuMap) {
  26.                 /* 把原来的数据也加入去 */
  27.                 Iterator<Student> iterator = findAll().values().iterator();
  28.                 while (iterator.hasNext()) {
  29.                         Student student = iterator.next();
  30.                         stuMap.put(student.getNum(), student);
  31.                 }

  32.                 FileOutputStream fos = null;
  33.                 ObjectOutputStream out = null;
  34.                 try {
  35.                         fos = new FileOutputStream(file);
  36.                         out = new ObjectOutputStream(fos);
  37.                         Iterator<Student> it = stuMap.values().iterator();
  38.                         while (it.hasNext()) {
  39.                                 out.writeObject(it.next());
  40.                         }
  41.                         out.writeObject(null);
  42.                         out.flush();
  43.                 } catch (FileNotFoundException e) {
  44.                         e.printStackTrace();
  45.                 } catch (IOException e) {
  46.                         e.printStackTrace();
  47.                 } finally {
  48.                         try {
  49.                                 out.close();
  50.                         } catch (IOException e) {
  51.                                 e.printStackTrace();
  52.                         }
  53.                         try {
  54.                                 fos.close();
  55.                         } catch (IOException e) {
  56.                                 e.printStackTrace();
  57.                         }
  58.                 }
  59.         }

  60.         /**
  61.          * 查找所有学生对象
  62.          */
  63.         @Override
  64.         public TreeMap<String, Student> findAll() {
  65.                 FileInputStream fis = null;
  66.                 ObjectInputStream in = null;
  67.                 TreeMap<String, Student> stuMap = null;
  68.                 try {
  69.                         fis = new FileInputStream(file);
  70.                         in = new ObjectInputStream(fis);
  71.                         Student student = null;
  72.                         stuMap = new TreeMap<String, Student>();
  73.                         while ((student = (Student) (in.readObject())) != null) {
  74.                                 stuMap.put(student.getNum(), student);
  75.                         }
  76.                 } catch (FileNotFoundException e) {
  77.                         e.printStackTrace();
  78.                 } catch (IOException e) {
  79.                         e.printStackTrace();
  80.                 } catch (ClassNotFoundException e) {
  81.                         e.printStackTrace();
  82.                 }
  83.                 return stuMap;
  84.         }
  85.         /**
  86.          * 删除所有学生对象
  87.          */
  88.         @Override
  89.         public void delAll() {
  90.                 FileOutputStream fos = null;
  91.                 ObjectOutputStream out = null;
  92.                 try {
  93.                         fos = new FileOutputStream(file);
  94.                         out = new ObjectOutputStream(fos);
  95.                         out.writeObject(null);
  96.                         out.flush();
  97.                 } catch (FileNotFoundException e) {
  98.                         e.printStackTrace();
  99.                 } catch (IOException e) {
  100.                         e.printStackTrace();
  101.                 } finally {
  102.                         try {
  103.                                 out.close();
  104.                         } catch (IOException e) {
  105.                                 e.printStackTrace();
  106.                         }
  107.                 }
  108.         }

  109.         /**
  110.          * 更新学生对象
  111.          */
  112.         @Override
  113.         public void updateStu(TreeMap<String, Student> stuMap) {
  114.                 FileOutputStream fos = null;
  115.                 ObjectOutputStream out = null;
  116.                 try {
  117.                         fos = new FileOutputStream(file);
  118.                         out = new ObjectOutputStream(fos);
  119.                         Iterator<Student> it = stuMap.values().iterator();
  120.                         while (it.hasNext()) {
  121.                                 out.writeObject(it.next());
  122.                         }
  123.                         out.writeObject(null);
  124.                         out.flush();
  125.                 } catch (FileNotFoundException e) {
  126.                         e.printStackTrace();
  127.                 } catch (IOException e) {
  128.                         e.printStackTrace();
  129.                 } finally {
  130.                         try {
  131.                                 out.close();
  132.                         } catch (IOException e) {
  133.                                 e.printStackTrace();
  134.                         }
  135.                         try {
  136.                                 fos.close();
  137.                         } catch (IOException e) {
  138.                                 e.printStackTrace();
  139.                         }
  140.                 }
  141.         }

  142.         @Override
  143.         public boolean delByNum(String num) {
  144.                 boolean flag = false;
  145.                 TreeMap<String, Student> stuMap = findAll();
  146.                 if (stuMap.remove(num) != null) {
  147.                         updateStu(stuMap);
  148.                         flag = true;
  149.                 }
  150.                 return flag;
  151.         }

  152.         @Override
  153.         public Student findByNum(String num) {
  154.                 return findAll().get(num);
  155.         }

  156.         @Override
  157.         public void updateByNum(String num, Student student) {
  158.                 TreeMap<String, Student> stuMap = findAll();
  159.                 stuMap.get(num).setId(student.getId());
  160.                 stuMap.get(num).setName(student.getName());
  161.                 stuMap.get(num).setNum(student.getNum());
  162.                 stuMap.get(num).setSex(student.getSex());
  163.                 updateStu(stuMap);
  164.         }

  165. }
复制代码
回复 使用道具 举报
StudentMain.java
  1. package com.linguofeng.main;

  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Iterator;
  7. import java.util.TreeMap;

  8. import com.linguofeng.dao.StudentDao;
  9. import com.linguofeng.dao.impl.StudentDaoImpl;
  10. import com.linguofeng.model.Student;

  11. public class StudentMain {
  12.         static StudentDao dao;
  13.         static BufferedReader br;
  14.         public static void main(String[] args) {
  15.                 /* 启动时判断数据文件是否存在,如不存在则新建. */
  16.                 File file = new File("student.dat");
  17.                 if (!file.exists()) {
  18.                         try {
  19.                                 file.createNewFile();
  20.                         } catch (IOException e) {
  21.                                 e.printStackTrace();
  22.                         }
  23.                 }

  24.                 dao = new StudentDaoImpl();

  25.                 /* 获得控制台输入流 */
  26.                 br = new BufferedReader(new InputStreamReader(System.in));

  27.                 /* 进行死循环 */
  28.                 while (true) {
  29.                         System.out.println(getLine());
  30.                         System.out.println("=[0]主菜单\t\t\t\t\t\t=");
  31.                         System.out.println("=[1]查找学生\t\t\t\t\t\t=");
  32.                         System.out.println("=[2]增加学生\t\t\t\t\t\t=");
  33.                         System.out.println("=[3]删除一个学生\t\t\t\t\t=");
  34.                         System.out.println("=[4]删除所有学生\t\t\t\t\t=");
  35.                         System.out.println("=[5]学生列表\t\t\t\t\t\t=");
  36.                         System.out.println("=[6]更新学生\t\t\t\t\t\t=");
  37.                         System.out.println("=[x]退出\t\t\t\t\t\t=");
  38.                         System.out.println(getLine());
  39.                         System.out.print("请选择操作:");

  40.                         /* 获得控制台输入的内容 */
  41.                         String input = getInput();
  42.                         if (input.equals("")) {
  43.                                 input = "x";
  44.                         }
  45.                         switch (input.charAt(0)) {
  46.                                 case '2' :
  47.                                         addStu();
  48.                                         break;
  49.                                 case 'x' :
  50.                                         System.out.println("退出成功,欢迎再次使用!");
  51.                                         System.exit(0);
  52.                                         break;
  53.                                 case '5' :
  54.                                         findAll();
  55.                                         break;
  56.                                 case '4' :
  57.                                         delAll();
  58.                                         break;
  59.                                 case '3' :
  60.                                         delByNum();
  61.                                         break;
  62.                                 case '1' :
  63.                                         findByNum();
  64.                                         break;
  65.                                 case '6' :
  66.                                         updateByNum();
  67.                                         break;
  68.                                 default :
  69.                                         System.out.println("不支持的系统指令,请重新输入!");
  70.                                         break;
  71.                         }
  72.                 }
  73.         }

  74.         /**
  75.          * 根据学生学号查询学生对象信息
  76.          */
  77.         public static void findByNum() {
  78.                 System.out.print("请输入要查询的学生学号:");
  79.                 while (true) {
  80.                         String input = getInput();
  81.                         if (dao.findByNum(input) != null) {
  82.                                 System.out.println(dao.findByNum(input));
  83.                                 return;
  84.                         } else if (input.equals("")) {
  85.                                 return;
  86.                         } else {
  87.                                 System.out.print("查询失败,不存在该学生,请重新输入要删除的学生学号:");
  88.                         }
  89.                 }
  90.         }

  91.         /**
  92.          * 根据学生学号删除学生对象
  93.          */
  94.         public static void delByNum() {
  95.                 System.out.println(getLine());
  96.                 findAll();/* 显示所有学生信息方便进行删除操作 */
  97.                 System.out.println(getLine());
  98.                 System.out.println("如需取消留空直接按回车.");
  99.                 System.out.print("请输入要删除的学生学号:");
  100.                 while (true) {
  101.                         String input = getInput();
  102.                         if (dao.delByNum(input)) {
  103.                                 System.out.println("删除成功!");
  104.                                 findAll();
  105.                                 return;
  106.                         } else if (input.equals("")) {
  107.                                 return;
  108.                         } else {
  109.                                 System.out.print("删除失败,不存在该学生,请重新输入要删除的学生学号:");
  110.                         }
  111.                 }
  112.         }
  113.         /**
  114.          * 学生列表
  115.          */
  116.         public static void findAll() {
  117.                 TreeMap<String, Student> stuMap = dao.findAll();
  118.                 Iterator<Student> iterator = stuMap.values().iterator();
  119.                 if (!iterator.hasNext()) {
  120.                         System.out.println("数据库为空");
  121.                 }
  122.                 while (iterator.hasNext()) {
  123.                         System.out.println(iterator.next());
  124.                 }
  125.         }

  126.         /**
  127.          * 删除所有学生对象
  128.          */
  129.         public static void delAll() {
  130.                 while (true) {
  131.                         System.out.print("是否要删除所有学生信息,确定请输入YES,否请输入NO:");
  132.                         String input = getInput();
  133.                         if (input.equals("YES") || input.equals("yes")) {
  134.                                 dao.delAll();
  135.                                 System.out.println("清空成功");
  136.                                 return;
  137.                         } else if (input.equals("NO") || input.equals("no")) {
  138.                                 return;
  139.                         } else {
  140.                                 System.out.println("输入有误,请重新输入!");
  141.                         }
  142.                 }
  143.         }

  144.         /**
  145.          * 添加学生
  146.          */
  147.         public static void addStu() {
  148.                 while (true) {
  149.                         System.out.print("请输入要添加学生的个数:");
  150.                         String ch = null;
  151.                         try {
  152.                                 ch = br.readLine();
  153.                         } catch (IOException e) {
  154.                                 e.printStackTrace();
  155.                         }
  156.                         if (ch.equals("")) {
  157.                                 System.out.println("输入有误,请重新输入!");
  158.                         } else {
  159.                                 int size = Integer.parseInt(ch);
  160.                                 TreeMap<String, Student> stuMap = new TreeMap<String, Student>();
  161.                                 for (int i = 0; i < size; i++) {
  162.                                         Student student = new Student();
  163.                                         try {
  164.                                                 System.out.print("请输入第 " + (i + 1) + " 个学生的姓名:");
  165.                                                 student.setName(br.readLine());
  166.                                                 System.out.print("请输入第 " + (i + 1) + " 个学生的学号:");
  167.                                                 student.setNum(br.readLine());
  168.                                                 System.out.print("请输入第 " + (i + 1)
  169.                                                                 + " 个学生的性别(男输:1;女输:0):");
  170.                                                 student.setSex(Integer.parseInt(br.readLine()));
  171.                                         } catch (IOException e) {
  172.                                                 e.printStackTrace();
  173.                                         }
  174.                                         stuMap.put(student.getNum(), student);
  175.                                 }
  176.                                 dao.addStu(stuMap);
  177.                                 findAll();
  178.                                 return;
  179.                         }
  180.                 }
  181.         }
复制代码
由于字数限制,请接下面的代码。
回复 使用道具 举报

  1.         public static void updateByNum() {
  2.                 findAll();
  3.                 System.out.print("请输入要修改的学生学号:");
  4.                 while (true) {
  5.                         String input = getInput();
  6.                         String num = input;
  7.                         Student student = dao.findByNum(num);
  8.                         if (student != null) {
  9.                                 System.out.println(student);
  10.                                 System.out.println("如不修改直接留空按回车!");
  11.                                 System.out.print("请输入学生的新姓名:");
  12.                                 input = getInput();
  13.                                 if (!input.equals(""))
  14.                                         student.setName(input);
  15.                                 System.out.print("请输入学生的新学号:");
  16.                                 input = getInput();
  17.                                 if (!input.equals(""))
  18.                                         student.setNum(input);
  19.                                 System.out.print("请输入学生的新版性别(男输:1;女输:0):");
  20.                                 input = getInput();
  21.                                 if (!input.equals(""))
  22.                                         student.setSex(Integer.parseInt(input));
  23.                                 dao.updateByNum(num, student);
  24.                                 System.out.println("更新成功,更新后的数据:");
  25.                                 System.out.println(dao.findByNum(student.getNum()));
  26.                                 return;
  27.                         } else if (input.equals("")) {
  28.                                 return;
  29.                         } else {
  30.                                 System.out.print("查询失败,不存在该学生,请重新输入要修改的学生学号:");
  31.                         }
  32.                 }
  33.         }

  34.         public static void addTest() {
  35.                 TreeMap<String, Student> stuMap = new TreeMap<String, Student>();
  36.                 for (int i = 0; i < 10000; i++) {
  37.                         Student student = new Student(i, "A" + i, "NUM" + i, 0);
  38.                         stuMap.put(student.getNum(), student);
  39.                 }
  40.                 dao.addStu(stuMap);
  41.                 return;
  42.         }

  43.         /**
  44.          * 获得===========分割符
  45.          *
  46.          * @return
  47.          */
  48.         public static String getLine() {
  49.                 return "=========================================================";
  50.         }

  51.         /**
  52.          * 获得控制台输入
  53.          *
  54.          * @return
  55.          */
  56.         public static String getInput() {
  57.                 String ch = "";
  58.                 try {
  59.                         ch = br.readLine();
  60.                 } catch (IOException e) {
  61.                         e.printStackTrace();
  62.                 }
  63.                 return ch;
  64.         }
  65. }
复制代码
这是我写的一个学生信息管理系统,使用的时候注意包路径和文件名,最好是导入到Eclipse工程中运行。
回复 使用道具 举报
时间原因,不能给你写一个,但可以给你个相似的,你可以看下
USER类:
public class User {
        private int  userid;
        private String username;
        private String password;
       
        private String unum;
       
        private String sex;
       
        private String age;
        private String tel;
       
        private String purview;
       
        private String address;

        public int getUserid() {
                return userid;
        }

        public void setUserid(int userid) {
                this.userid = userid;
        }

        public String getUsername() {
                return username;
        }

        public void setUsername(String username) {
                this.username = username;
        }

        public String getPassword() {
                return password;
        }

        public void setPassword(String password) {
                this.password = password;
        }

        public String getUnum() {
                return unum;
        }

        public void setUnum(String unum) {
                this.unum = unum;
        }

        public String getSex() {
                return sex;
        }

        public void setSex(String sex) {
                this.sex = sex;
        }

        public String getAge() {
                return age;
        }

        public void setAge(String age) {
                this.age = age;
        }

        public String getTel() {
                return tel;
        }

        public void setTel(String tel) {
                this.tel = tel;
        }

        public String getPurview() {
                return purview;
        }

        public void setPurview(String purview) {
                this.purview = purview;
        }

        public String getAddress() {
                return address;
        }

        public void setAddress(String address) {
                this.address = address;
        }
userdao接口:
                public int updateUserByUserID(User user);
        public int addUser(User user);
        public User queryUserById(int  userid);
        public int deleteUser(int id );
        public List<User> queryAllUser(String username, int pageNumber, int pageSize);
        public int queryAllUserCount(String username);
        public int updatePassword(User user);实现类:public User loginUser(User user) {
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        String sql="select * from shop_user where username=? and password=?";
        User users=new User();

        try {
                pstmt=ConnectionManager.getConnection().prepareStatement(sql);
                pstmt.setString(1,user.getUsername());
                pstmt.setString(2, user.getPassword());
                rs=pstmt.executeQuery();
                if(rs.next()){
                               
                        users.setUserid(rs.getInt("userid"));
                        users.setUsername(rs.getString("username"));
                        users.setPassword(rs.getString("password"));
                        users.setUnum(rs.getString("unum"));
                        users.setSex(rs.getString("sex"));
                        users.setAge(rs.getString("age"));
                        users.setTel(rs.getString("tel"));
                        users.setPurview(rs.getString("purview"));
                        users.setAddress(rs.getString("address"));
                }else{
                        users = null;
                }

               
        } catch (SQLException e) {
       
                e.printStackTrace();
        }finally{
                try {
                        pstmt.close();
                        rs.close();
                       
                } catch (SQLException e) {
                       
                        e.printStackTrace();
                }
        }
       
       
       
                return users;
        }
public int updateUserByUserID(User user) {
int rows=0;
PreparedStatement pstmt=null;
int i=0;
String sql="update shop_user set username=?,password=?,unum=?,sex=?,age=?,tel=?,purview=?,address=? where userid=?";
try {
        pstmt=ConnectionManager.getConnection().prepareStatement(sql);
        pstmt.setString(++i, user.getUsername());
        pstmt.setString(++i, user.getPassword());
        pstmt.setString(++i, user.getUnum());
        pstmt.setString(++i, user.getSex());
        pstmt.setString(++i, user.getAge());
        pstmt.setString(++i, user.getTel());
        pstmt.setString(++i, user.getPurview());
        pstmt.setString(++i, user.getAddress());
        pstmt.setInt(++i, user.getUserid());
     rows=pstmt.executeUpdate();

       
} catch (SQLException e) {

        e.printStackTrace();
}finally{
        try {
                pstmt.close();
        } catch (SQLException e) {
       
                e.printStackTrace();
        }
}

                return rows;
        }

        public int addUser(User user) {
PreparedStatement pstmt=null;
String sql="insert into shop_user values(SEQ_SHOP_USER_USERID.Nextval,?,?,?,?,?,?,?,?)";
int row=0;
int i=0;

try {
        pstmt=ConnectionManager.getConnection().prepareStatement(sql);
        pstmt.setString(++i, user.getUsername());
        pstmt.setString(++i, user.getPassword());
        pstmt.setString(++i, user.getUnum());
        pstmt.setString(++i, user.getSex());
        pstmt.setString(++i, user.getAge());
        pstmt.setString(++i, user.getTel());
        pstmt.setString(++i, user.getPurview());
        pstmt.setString(++i, user.getAddress());
        row=pstmt.executeUpdate();
       
       
       
} catch (SQLException e) {

        e.printStackTrace();
}finally{
        try {
                pstmt.close();
        } catch (SQLException e) {
       
                e.printStackTrace();
        }
}

                return row;
        }

        public User queryUserById(int  userid) {
                PreparedStatement pstmt=null;
                ResultSet rs=null;
                String sql="select * from shop_user where userid=?";
                User users=null;
                int index=0;
                users=new User();

                try {
                        pstmt=ConnectionManager.getConnection().prepareStatement(sql);
                        pstmt.setInt(++index,userid);
                        rs=pstmt.executeQuery();
                        while(rs.next()){
                                       
                                users.setUserid(rs.getInt("userid"));
                                users.setUsername(rs.getString("username"));
                                users.setPassword(rs.getString("password"));
                                users.setUnum(rs.getString("unum"));
                                users.setSex(rs.getString("sex"));
                                users.setAge(rs.getString("age"));
                                users.setTel(rs.getString("tel"));
                                users.setPurview(rs.getString("purview"));
                                users.setAddress(rs.getString("address"));
                        }

                       
                } catch (SQLException e) {
               
                        e.printStackTrace();
                }finally{
                        try {
                                pstmt.close();
                                rs.close();
                               
                        } catch (SQLException e) {
                               
                                e.printStackTrace();
                        }
                }
               
               
               
                        return users;
                }

        public int deleteUser(int id) {
                int row=0;
                PreparedStatement pstmt=null;
                int i=0;
                String sql="delete shop_user where userid=?";
                try {
                        pstmt=ConnectionManager.getConnection().prepareStatement(sql);
                        pstmt.setInt(++i, id);
                        row=pstmt.executeUpdate();
                       
                } catch (SQLException e) {
                       
                        e.printStackTrace();
                }finally{
                        try {
                                pstmt.close();
                        } catch (SQLException e) {
               
                                e.printStackTrace();
                        }
                }
               
               
               
                return row;
        }

        public List<User> queryAllUser(String username, int pageNumber, int pageSize) {
                List<User> list=new ArrayList<User>();
                PreparedStatement pstmt=null;
                ResultSet rs=null;
                String sql="select t.* from(select a.*,rownum r from(select * from shop_user order by userid desc ) a where 1=1";
                StringBuffer sb=new StringBuffer();
                sb.append(sql);
               
                        if(username!=null){
                                sb.append(" and  username like '%");
                                sb.append(username);
                                sb.append("%')");
                                }else{
                                sb.append(")");       
                                }
                                sb.append(" t where  r>=");
                                sb.append(pageSize*(pageNumber-1)+1);
                                sb.append(" and r<=");
                                sb.append(pageSize*pageNumber);       

                                sql=sb.toString();


       

                        try {
                                pstmt=ConnectionManager.getConnection().prepareStatement(sql);
                                rs=pstmt.executeQuery();
                                while(rs.next()){
                     User user=new User();
                     user.setUserid(rs.getInt("userid"));
                                 user.setUsername(rs.getString("username"));
                                 user.setPassword(rs.getString("password"));
                                 user.setUnum(rs.getString("unum"));
                                 user.setSex(rs.getString("sex"));
                                 user.setAge(rs.getString("age"));
                                 user.setTel(rs.getString("tel"));
                                 user.setPurview(rs.getString("purview"));
                                 user.setAddress(rs.getString("address"));
                                 list.add(user);
                                 }
                        } catch (SQLException e) {
                       
                                e.printStackTrace();
                        }finally{
                                try {
                                        pstmt.close();
                                } catch (SQLException e) {
                       
                                        e.printStackTrace();
                                }
                        }
                       
                       
                return list;
        }

        public int queryAllUserCount(String username) {
                PreparedStatement pstmt=null;
                ResultSet rs=null;
                String sql="select count(userid) from shop_user where 1=1 ";
        StringBuffer sb=new StringBuffer();
                sb.append(sql);
                if(username!=null){
                        sb.append("and username like '%");
                    sb.append(username);
                        sb.append("%'");
                }
                sql=sb.toString();
               
               
               
                int count=0;
                try {
                        pstmt=ConnectionManager.getConnection().prepareStatement(sql);
//                        if(username!=null){
//                        pstmt.setString(1,username)        ;
//                        }else{
//                        pstmt.setString(1,null);       
//                        System.out.println(sql);       
//                        }
               
                        rs=pstmt.executeQuery();
                        while(rs.next()){
                                count=rs.getInt(1);
                        }
                } catch (SQLException e) {
               
                        e.printStackTrace();
                }finally{
                        try {
                                pstmt.close();
                        } catch (SQLException e) {
                               
                                e.printStackTrace();
                        }
                }
               
               
                return count;
        }

        public int updatePassword(User user) {
        int row=0;
        String sql="update shop_user set password=? where username=?";
        PreparedStatement pstmt=null;
        int i=0;
       
        try {
                pstmt=ConnectionManager.getConnection().prepareStatement(sql);
                pstmt.setString(++i,user.getPassword());
                pstmt.setString(++i,user.getUsername());
                row=pstmt.executeUpdate();
        } catch (SQLException e) {
       
                e.printStackTrace();
        }finally{
                try {
                        pstmt.close();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
               
        }
       
                return row;
        }

我是从库里查的信息,库里只有一个USER表
回复 使用道具 举报
刘一锋 黑马帝 2011-10-8 15:12:28
7#
感觉改代码真的好废时间啊,今天视频都还没看呢,愁
回复 使用道具 举报
刘一锋 黑马帝 2011-10-9 15:26:23
8#
Exception in thread "main" java.lang.NullPointerException
        at CommandStage.main(CommandStage.java:123)
编译没错误,运行出了NullPointerException
什么原因啊,搞不懂,
回复 使用道具 举报
程钧杰 黑马帝 2011-10-9 15:36:41
9#
这个真不难,学了JDBC就可以做了,出现NullPointerException,我一般是再从数据库取东西的时候没放到变量里,好几次这种错误了
回复 使用道具 举报
我知道这个是最基本的,但是对于初学者来说还是有点困难的,感觉,刚才看视频,发现不应该用HashMap应该用LIst,因为是一对多映射,一个名字对应着很多个信息
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马