首页 > 代码库 > 模拟电话簿查询系统的设计

模拟电话簿查询系统的设计

该项目是用于日常生活中记录联系人信息的一款小工具。

实现了对联系人的姓名、年龄、性别、电话号码、住址的添加及修改、查找、删除、排序等功能。该项目是以windows控制台为运行平台,所有的操作都应在控制台上进行。

技术分享

类名:

App  对应  Application(程序入口函数)

Menu 对应  Menu      (菜单类)

Operate 对应  Operate    (业务处理类)

Person 对应  Person (实体类)

 

3.2方法名:

App类中方法(无成员变量):

main() 程序入口函数

start() 主菜单控制

 

Operate类中方法及成员变量

private List<Person> list 集合

 

addLogic() 用户添加信息业务逻辑控制

searchLogic() 用户查询信息业务逻辑控制

modifyLogicLogic() 修改信息业务逻辑控制

deleteLogic() 删除信息业务逻辑控制

orderLogic() 排序信息业务逻辑控制

addOperation () 添加新用户信息

showAll() 查询全部用户信息

searchByName() 按姓名查询用户信息

searchByAge() 按年龄查询用户信息

searchBySex() 按性别查询用户信息

searchByTelNum() 按电话号码查询用户信息

searchByAdd() 按地址查询用户信息

modify() 修改指定记录信息

delete() 删除指定用户信息

deleteAll() 删除全部用户信息

orderName() 按用户姓名排序信息

orderAge() 按用户年龄排序信息

orderSex() 按用户性别排序信息

 

TelNoteRegex类中方法(无成员变量)

menuRegex (int min, int max ) 对菜单输入选项的验证

nameRegex ( ) 对用户输入姓名的验证

ageRegex ( ) 对用户输入年龄的验证

sexRegex ( ) 对用户输入性别的验证

telNumRegex ( ) 对用户输入电话号码的验证

addressegex ( ) 对用户输入地址的验证

 

Menu类中的方法(无成员变量)

mainMenu() 主菜单

addMenu () 添加用户菜单

searchMenu () 查找用户菜单

modifyMenu () 修改用户信息主菜单

subModifyMenu () 修改用户信息子菜单

deleteMenu () 删除用户信息菜单

orderMenu () 排序用户信息菜单

 

Person类中的方法及成员变量

private int id; 用户ID 属性

private String name; 用户姓名属性

private String age; 用户年龄属性

private String sex; 用户性别属性

private String telNum; 用户电话号码属性

private String address; 用户地址属性

Person() 无参数构造方法

Person(String name, String age, String sex, String telNum, String address) 有参数构造方法

getName() 读取用户名

setName(String name) 设置用户名

getAge() 读取用户年龄

setAge(String age) 设置用户年龄

getSex() 读取用户性别

setSex(String sex) 设置用户性别

getTelNum() 读取用户电话号码

setTelNum (String telNum) 设置用户电话号码

getAddress() 读取用户地址

setAddress(String address) 设置用户地址

getID () 读取用户ID

setID (int ID) 设置用户ID

toString() 连接字符串方法

 

下面是各个类的具体代码:

 

package 电话簿设计;
//测试类
public class App 
{
    public static void main(String[] args)
    {
        
        App app = new App();
        
        //实例化Menu类
        Menu me = new Menu();
            
        for(int i = 0;i>=0;i++)
        {
            switch(app.start())
            {
            case 1:
                me.addLogic();                
                break;
            case 2:
                me.searchLogic();
                break;
            case 3:
                me.modifyLogicLogic();        
                break;
            case 4:
                me.deleteLogic();        
                break;
            case 5:
                me.orderLogic();    
                break;
            case 6:
                System.out.println("谢谢使用,再见...");
                i = -2;
                break;
            }
        }               
    }
    
    public int start()
    {
        Menu menu = new Menu();
        return menu.mainMenu();
        
    }

}

 

package 电话簿设计;

import java.util.Scanner;

//菜单类
public class Menu
{ 
    //排序信息业务逻辑控制
    public void orderLogic() 
    {
       
        //实例化Menu类
        Menu me = new Menu();
        
        //实例化Operate
        Operate ope = new Operate();
        
        for(int i5 = 0;i5>=0;i5++)
        {
            switch(me.orderMenu())
            {
            case 1: ope.orderName();i5 = -2;break;
            case 2: ope.orderAge();i5 = -2;break;
            case 3: ope.orderSex();i5 = -2;break;
            case 4: ope.showAll();;i5 = -2;break;
            case 5: i5 = -2;break;
            }
        }            
    }
    //删除信息业务逻辑控制
    public void deleteLogic() 
    {
        
        //实例化Menu类
        Menu me = new Menu();
        
        //实例化Operate
        Operate ope = new Operate();
        
        for(int i4 =0;i4>=0;i4++)
        {
            switch(me.deleteMenu())
            {
            case 1: ope.showAll();;i4 = -2;break;
            case 2: ope.delete();i4 = -2;break;
            case 3: ope.deleteAll();i4 = -2;break;
            case 4: i4 = -2;break;
            }
        }        
    }
    //修改信息业务逻辑控制
    public void modifyLogicLogic() 
    {            
            //实例化Menu类
            Menu me = new Menu();
            
            //实例化Operate
            Operate ope = new Operate();
            
            for(int i3 = 0;i3>=0;i3++)
            {
                switch(me.modifyMenu())
                {
                case 1:  ope.showAll();i3 = -2;break;
                case 2:
                    for(int i31 = 0;i31>=0;i31++)
                    {
                        switch(me.subModifyMenu())
                        {
                        case 1: ope.modifyname();i31 = -2;i3 = -2;break;
                        case 2: ope.modifyage();i31 = -2;i3 = -2;break;
                        case 3: ope.modifysex();i31 = -2;i3 = -2;break;
                        case 4: ope.modifytelnum();i31 = -2;i3 = -2;break;
                        case 5: ope.modifyadd();i31 = -2;i3 = -2;break;
                        case 6: i31 = -2;break;
                        }

                    }                        
                    break;
                case 3: i3 = -2; break;    
                }            
            }        
    }
    // 用户查询信息业务逻辑控制
    public void searchLogic()
    {        
        //实例化Menu类
        Menu me = new Menu();
        
        //实例化Operate
        Operate ope = new Operate();
        
        for(int i2 = 0;i2>=0;i2++)
        {
            switch(me.searchMenu())
            {
            case 1: ope.searchByName();i2 = -2;break;
            case 2: ope.searchByAge(); i2 = -2;break;
            case 3: ope.searchBySex(); i2 = -2;break;
            case 4: ope.searchByTelNum();i2 = -2;break;
            case 5: ope.searchByAdd();i2 = -2;break;
            case 6: ope.showAll();i2 = -2;break;
            case 7: i2 = -2;break;
            }
        }                
    }
    //用户添加信息业务逻辑控制
    public void addLogic()     
    {        
        //实例化Menu类
        Menu me = new Menu();
        
        //实例化Operate
        Operate ope = new Operate();
        
        for(int i1 =0;i1>=0;i1++)
        {
            switch(me.addMenu())
            {
            case 1: ope.addLogic();i1= -2;break;
            case 2: ope.showAll(); i1= -2;break;
            case 3: i1= -2;break;
            }                    
        }
    }
    //主菜单
    public int mainMenu()
    {
        System.out.println("**********************");
        System.out.println("***  1 添加纪录    ***\n***  2 查找记录    ***");
        System.out.println("***  3 修改记录    ***\n***  4 删除记录    ***");
        System.out.println("***  5 排序纪录    ***\n***  6 退出系统    ***");
        System.out.println("**********************");
        
        System.out.print("欢迎使用,请您选择正确的菜单:");
        
        Scanner sc = new Scanner(System.in);
        
        int result = 0;
        
        while(true)
        {
            result = sc.nextInt();
            
            if(result >=1 && result <=6)
            {
                break;
            }
            else
            {
                System.out.print("菜单输入错误,请重新选择:");
            }
        }
                
        return result;
    }
    //添加用户菜单
    public int addMenu()
    {
        int result = 0;
        
        System.out.println("**********************");
        System.out.println("***  1添加新纪录   ***\n***  2查看全记录   ***\n***  3返回上一级   ***");
        System.out.println("**********************");
        
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择您的菜单:");
        
        while(true)
        {
            result = sc.nextInt();
            
            if(result >=1 && result <=3)
            {
                break;
            }
            else
            {
                System.out.print("菜单选择错误,请重新选择:");
            }
        }
        
        return result;
    }
    //查找用户菜单
    public int searchMenu()
    {
        int result = 0;
        
        System.out.println("**********************");
        System.out.println("***  1按姓名查找   ***\n***  2按年龄查找   ***\n***  3按性别查找   ***");
        System.out.println("***  4按号码查找   ***\n***  5按住址查找   ***\n***  6查看全记录   ***\n***  7返回上一级   ***");
        System.out.println("**********************");
        
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择您的菜单:");
        
        while(true)
        {
            result = sc.nextInt();
            
            if(result >=1 && result <=7)
            {
                break;
            }
            else
            {
                System.out.print("菜单选择错误,请重新选择:");
            }
        }
        
        return result;
    }
    //修改用户信息主菜单
    public int modifyMenu()
    {
        int result = 0;
        
        System.out.println("**********************");
        System.out.println("***  1查看全纪录   ***\n***  2修改指定记录 ***\n***  3返回上一级   ***");
        System.out.println("**********************");
        
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择您的菜单:");
        
        while(true)
        {
            result = sc.nextInt();
            
            if(result >=1 && result <=3)
            {
                break;
            }
            else
            {
                System.out.print("菜单选择错误,请重新选择:");
            }
        }
        
        return result;
    }
    //修改用户信息子菜单
    public int subModifyMenu()
    {
        int result = 0;
        
        System.out.println("********************");
        System.out.println("***  1修改姓名   ***\n***  2修改年龄   ***\n***  3修改性别   ***");
        System.out.println("***  4修改号码   ***\n***  5修改住址   ***\n***  6返回上一级 ***");
        System.out.println("********************");
        
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择您的菜单:");
        
        while(true)
        {
            result = sc.nextInt();
            
            if(result >=1 && result <=6)
            {
                break;
            }
            else
            {
                System.out.print("菜单选择错误,请重新选择:");
            }
        }
        
        return result;
    }
    //删除用户信息菜单
    public int deleteMenu()
    {
        int result = 0;
        
        System.out.println("**********************");
        System.out.println("***  1查看全记录   ***\n***  2删除指定记录 ***\n***  3删除全部记录 ***\n***  4返回上一级   ***");
        System.out.println("**********************");
        
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择您的菜单:");
        
        while(true)
        {
            result = sc.nextInt();
            
            if(result >=1 && result <=4)
            {
                break;
            }
            else
            {
                System.out.print("菜单选择错误,请重新选择:");
            }
        }
        
        return result;
    }
    //排序用户信息菜单
    public int orderMenu()
    {
        int result = 0;
        
        System.out.println("**********************");
        System.out.println("***  1按姓名排序   ***\n***  2按年龄排序   ***\n***  3按性别排序   ***");
        System.out.println("***  4查看全记录   ***\n***  5返回上一级   ***");
        System.out.println("**********************");
        
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择您的菜单:");
        
        while(true)
        {
            result = sc.nextInt();
            
            if(result >=1 && result <=5)
            {
                break;
            }
            else
            {
                System.out.print("菜单选择错误,请重新选择:");
            }
        }
        
        return result;
    }

}
package 电话簿设计;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

//验证方法的类
public class TelNoteRegex {
    
    //验证用户输入姓名是否合法
    public String nameRegex ()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("请输入姓名:");
        String na = "";
        
        while(true)
        {
            String name = sc.nextLine();
            
            String regex = "[\\u4E00-\\u9FA5]+";
            
            if(name.matches(regex) && name.length() >= 2 && name.length() <= 4)
            {
                na = name;
                break;
            }
            else
            {
                System.out.print("姓名输入不合法,请重新输入:");
            }
        }
        return na;
            
    }
    
    //验证用户输入年龄是否合法
    public int ageRegex ()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("请输入年龄:");
        
        int ag = 0;
        
        while(true)
        {
            int age = sc.nextInt();            
            
            if(age>=5&&age<=130)
            {
                ag = age;
                break;
            }
            else
            {
                System.out.print("年龄输入不合法,请重新输入:");
            }
        }
        return ag;
    }
    
    //验证用户输入性别是否合法
    public String sexRegex ()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("请输入性别(M/F):");
        
        String se = "";
        while(true)
        {
            String sex = sc.nextLine();
                        
            if(sex.equals("M")||sex.equals("F"))
            {
                se = sex;
                break;
            }
            else
            {
                System.out.print("性别输入不合法,请重新输入:");
            }
        }
        return se;
    }
    
    //验证用户输入的电话号码是否合法
    public String telNumRegex()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("请输入电话号码:");
        
        String te = "";
        
        while(true)
        {
            String tel = sc.nextLine();
            
            String regex = "[0-9]{7,11}";
            
            if(tel.matches(regex))
            {
                te = tel;
                break;
            }
            else
            {
                System.out.print("电话号码输入不合法,请重新输入:");
            }
        }
        return te;
    }
    
    //验证输入的ID是否合法
    public int idRegex()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("请输入您要操作数据的ID号:");
        int na = 0;
        
        while(true)
        {
            int id = sc.nextInt();
            int max = 0;
            try
            {
                //加载数据库驱动
                Class.forName("oracle.jdbc.driver.OracleDriver");
                //获取与数据库的连接
                Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
                //获取操作数据库的声明
                Statement st = conn.createStatement();
                //操作数据库的语句
                String sql = "select * from person";
                max = st.executeUpdate(sql);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
            if(id>=1&&id<=max)
            {
                na = id;
                break;
            }
            else
            {
                System.out.print("ID输入不合法,请重新输入:");
            }
        }
        return na;
    }
        
    //验证用户输入的地址是否合法
    public String addressRegex()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("请输入地址:");
        
        String add = "";
        
        while(true)
        {
            String address = sc.nextLine();
            
            String regex = "(^[a-zA-Z0-9]{1,50}$)";
            
            if(address.matches(regex))
            {
                add = address;
                break;
            }
            else
            {
                System.out.print("地址输入不合法,请重新输入:");
            }
        }
        return add;
    }

}
package 电话簿设计;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

//数据库操作类
public class Operate {
    
    //构造连接池
    //ComboPooledDataSource cp  = new ComboPooledDataSource("helloc3p0");
    
    //构造存放person类的集合
    private List<Person> list = new ArrayList();    
    
    //实例化TelNoteRegex对象
    TelNoteRegex tnr = new TelNoteRegex();

    //业务逻辑
    public void addLogic() //添加新用户信息
    {
        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            //获取操作数据库的声明
            Statement st = conn.createStatement();
            
            String sql = "insert into person values(sq_person.nextval,‘"+tnr.nameRegex()+
                    "‘,‘"+tnr.ageRegex()+"‘,‘"+tnr.sexRegex()+"‘,‘"+
                    tnr.telNumRegex()+"‘,‘"+tnr.addressRegex()+"‘)";
            
            st.executeUpdate(sql);
            
            System.out.println("添加纪录成功!");
            
            //释放资源
            st.close();
            conn.close();
            
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
    }

    public void showAll() //查询全部用户信息
    {
        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            //获取操作数据库的声明
            Statement st = conn.createStatement();
            
            String sql = "select * from person";
            
            ResultSet rs = st.executeQuery(sql);
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list.add(per);
            }
            for(Person p:list)
            {
                System.out.println(p);
            }                                
            //释放资源
            rs.close();
            st.close();
            conn.close();
            
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    public void searchByName() //按姓名查询用户信息
    {
        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");            
            
            String sql = "select * from person where name = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            //替换占位符?
            ps.setString(1, tnr.nameRegex());
            
            //执行数据库操作
            ResultSet rs = ps.executeQuery();
            
            if(rs.next())
            {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String sex = rs.getString("sex");
                long telnum = rs.getLong("telnum");
                String address = rs.getString("address");
                
                System.out.println("ID="+id+"  NAME="+name
                        +"   AGE="+age+"   SEX="+sex+
                        "   TELNUM="+telnum+"ADDRESS="+address);
                
            }
            else
            {
                System.out.println("无数据!");
            }
                                                    
            //释放资源
            rs.close();
            ps.close();
            ps.close();
            conn.close();
            
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
    }
    
    public void searchByAge() //按年龄查询用户信息
    {
        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");            
            
            String sql = "select * from person where age = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            //替换占位符?
            ps.setLong(1, tnr.ageRegex());
            
            //执行数据库操作
            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                List<Person> list1 = new ArrayList();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list1.add(per);
                
                for(Person p:list1)
                {
                    System.out.println(p);
                }
            }
            
                                                                
            //释放资源
            rs.close();
            ps.close();
            ps.close();
            conn.close();
            
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    public void searchBySex() //按性别查询用户信息
    {
        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");            
            
            String sql = "select * from person where sex = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            //替换占位符?
            ps.setString(1, tnr.sexRegex());
            
            //执行数据库操作
            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                List<Person> list1 = new ArrayList();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list1.add(per);
                
                for(Person p:list1)
                {
                    System.out.println(p);
                }
            }
                                                                            
            //释放资源
            rs.close();
            ps.close();
            ps.close();
            conn.close();
            
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    public void searchByTelNum() //按电话号码查询用户信息
    {
        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");            
            
            String sql = "select * from person where telnum = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            //替换占位符?
            ps.setNString(1, tnr.telNumRegex());
            
            //执行数据库操作
            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                List<Person> list1 = new ArrayList();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list1.add(per);
                
                for(Person p:list1)
                {
                    System.out.println(p);
                }
            }
            
                                                                
            //释放资源
            rs.close();
            ps.close();
            ps.close();
            conn.close();
            
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    public void searchByAdd() //按地址查询用户信息
    {
        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");            
            
            String sql = "select * from person where address = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            //替换占位符?
            ps.setString(1, tnr.addressRegex());
            
            //执行数据库操作
            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                List<Person> list1 = new ArrayList();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list1.add(per);
                
                for(Person p:list1)
                {
                    System.out.println(p);
                }
            }
            
                                                                
            //释放资源
            rs.close();
            ps.close();
            ps.close();
            conn.close();
            
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
        
    public void modifyname() //修改姓名信息
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "update person set name = ? where id = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ps.setLong(2,tnr.idRegex() );
            System.out.println("下面输入新的姓名");
            ps.setString(1, tnr.nameRegex());
            ps.executeUpdate();
            
            System.out.println("修改信息成功!");
            
            //释放资源
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void modifyage()  //修改年龄
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "update person set age = ? where id = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ps.setLong(2,tnr.idRegex() );
            System.out.println("下面输入新的年龄");
            ps.setLong(1, tnr.ageRegex());
            ps.executeUpdate();
            
            System.out.println("修改信息成功!");
            
            //释放资源
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void modifysex()  //修改性别
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "update person set sex = ? where id = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ps.setLong(2,tnr.idRegex() );
            System.out.println("下面输入新的性别");
            ps.setString(1, tnr.sexRegex());
            ps.executeUpdate();
            
            System.out.println("修改信息成功!");
            
            //释放资源
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void modifytelnum()  //修改号码
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "update person set telnum = ? where id = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ps.setLong(2,tnr.idRegex() );
            System.out.println("下面输入新的号码");
            ps.setString(1, tnr.telNumRegex());
            ps.executeUpdate();
            
            System.out.println("修改信息成功!");
            
            //释放资源
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
        
    public void modifyadd()  //修改住址
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "update person set address = ? where id = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ps.setLong(2,tnr.idRegex() );
            System.out.println("下面输入新的住址");
            ps.setString(1, tnr.addressRegex());
            ps.executeUpdate();
            
            System.out.println("修改信息成功!");
            
            //释放资源
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void delete() //删除指定用户信息
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "delete person where id = ?";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setLong(1, tnr.idRegex());
            ps.executeUpdate();
            
            System.out.println("删除信息成功!");
            
            //释放资源
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void deleteAll() //删除全部用户信息
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "truncate table person";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ps.executeUpdate();
            
            System.out.println("数据已全部清空!");
            
            //释放资源
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void orderName() //按用户姓名排序信息
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "select * from person order by name";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                List<Person> list1 = new ArrayList();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list1.add(per);
                
                for(Person p:list1)
                {
                    System.out.println(p);
                }
            }
            
            //释放资源
            rs.close();
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void orderAge() //按用户年龄排序信息
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "select * from person order by age";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                List<Person> list1 = new ArrayList();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list1.add(per);
                
                for(Person p:list1)
                {
                    System.out.println(p);
                }
            }
            
            //释放资源
            rs.close();
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }
    
    public void orderSex() //按用户性别排序信息
    {
        try
        {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取与数据库的连接
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816", "666666");
            
            String sql = "select * from person order by sex";
            
            //获取操作数据库的声明
            PreparedStatement ps = conn.prepareStatement(sql);
            
            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                //实例化Person类
                Person per = new Person();
                
                List<Person> list1 = new ArrayList();
                
                //添加信息到per对象中
                per.setId(rs.getInt("id"));
                per.setName(rs.getString("name"));
                per.setAge(rs.getInt("age"));
                per.setSex(rs.getString("sex"));
                per.setTelNum(rs.getLong("telnum"));
                per.setAddress(rs.getString("address"));
                
                //添加per对象放到list集合中
                list1.add(per);
                
                for(Person p:list1)
                {
                    System.out.println(p);
                }
            }
            
            //释放资源
            rs.close();
            ps.close();
            conn.close();
        }
                
     catch (Exception e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    }

}
package 电话簿设计;
//实体类
public class Person {
    
    private int id;         //用户ID
    private String name;    //用户姓名属性
    private int age;        //用户年龄属性
    private String sex;     //用户性别属性
    private long telNum;  //用户电话号码属性
    private String address; //用户地址属性
    
    //无参构造方法
    public Person()
    {
        super();
    }
        
    //有参构造方法
    public Person(int id, String name, int age, String sex, int telNum, String address)
    {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.telNum = telNum;
        this.address = address;
    }
    
    //getter和setter方法
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

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

    public long getTelNum() {
        return telNum;
    }

    public void setTelNum(long telNum) {
        this.telNum = telNum;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    
    //tostring
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", telNum=" + telNum
                + ", address=" + address + "]";
    }
    
    

}

 

模拟电话簿查询系统的设计