简单的通讯录小程序

实现通讯录的添加,查找,修改,排序,

package com.TongXunLu.services;

import java.util.Scanner;

import com.TongXunLu.Add.AddMenu;
import com.TongXunLu.Delete.DeleteMenu;
import com.TongXunLu.Select.SelectMenu;
import com.TongXunLu.Update.UpdateMenu;

public class App {
    public static void main(String[] args) {
        //欢迎进入系统
        Welcome();
    }

    public static void Welcome(){
        System.out.println("*********************");
        System.out.println("**    1 添加纪录        **");
        System.out.println("**    2 查找记录        **");
        System.out.println("**    3 修改记录        **");
        System.out.println("**    4 删除记录        **");
        System.out.println("**    5 排序记录        **");
        System.out.println("**    6 退出系统        **");
        System.out.println("*********************");
        System.out.println("请输入正确的数字,最小是:1,最大是:6");
        Scanner input=new Scanner(System.in);
        int sel=input.nextInt();
        switch (sel) {
        case 1:
            //添加纪录
            AddMenu.Add();
            break;
        case 2:
            //查找记录
            SelectMenu.Select();
            break;
        case 3:
            //修改记录
            UpdateMenu.menu    ();
            break;
        case 4:
            DeleteMenu.menu();
            break;
        case 5:
            //排序记录
            CompartorMenu.menu();
            break;
        case 6:
            System.out.println("系统退出!");
            System.exit(0);
            break;
        }
    }
}
package com.TongXunLu.Add;

import java.util.List;
import java.util.Scanner;

import com.TongXunLu.Select.SelectAll;
import com.TongXunLu.services.App;
import com.TongXunLu.services.Person;

public class AddMenu {
    //添加纪录菜单
    public static void Add(){
        System.out.println("*********************");
        System.out.println("**    1 添加新纪录     **");
        System.out.println("**    2 查看全纪录     **");
        System.out.println("**    3 返回上一级    **");
        System.out.println("请输入正确的数字,最小是:1,最大是:3");
        Scanner input=new Scanner(System.in);
        int AddSel=input.nextInt();
        switch (AddSel) {
        case 1:
            //添加新纪录
            AddServices addPerson=new AddImplement();
            addPerson.AddPerson();
            break;
        case 2:
            //查看全记录
            List<Person> list=SelectAll.SelectAllPerson();
            for (Person p : list) {
                System.out.println(p.toString());
            }
            Add();
            break;
        case 3:
            //返回上一级
            App.Welcome();
            break;

        }
    }

}
package com.TongXunLu.Add;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

import com.TongXunLu.tools.TelNoteRegex;
import com.TongXunLu.tools.Tools;

public class AddImplement implements AddServices{

    @Override
    public void AddPerson() {
        Scanner input=new Scanner(System.in);
        String name=null;
        int age=0;
        String sex=null;
        String TelNum=null;
        String address=null;
        boolean b=false;
        while(b==false){
        System.out.println("输入姓名,1-10位字母");
        name=input.next();
        b=TelNoteRegex.CheckName(name,b);
        }
        b=false;
        while(b==false){
        System.out.println("输入年龄,1-200");
        age=input.nextInt();
        b=TelNoteRegex.CheckAge(age, b);
        }
        b=false;
        while(b==false){
        System.out.println("输入性别,(男 m or M)(女 f or F)");
        sex=input.next();
        b=TelNoteRegex.CheckSex(sex, b);
        }
        b=false;
        while(b==false){
        System.out.println("输入电话号码,6-10位数字");
        TelNum=input.next();
        b=TelNoteRegex.CheckTel(TelNum, b);
        }
        b=false;
        while(b==false){
            System.out.println("输入地址,1-50位字母或数字");
            address=input.next();
            b=TelNoteRegex.CheckAddress(address, b);
        }

        //获取连接 驱动
        Connection conn=null;
        PreparedStatement pstm=null;
        conn=Tools.getConnection();
        try {
            pstm=conn.prepareStatement("insert into person(id,name,age,sex,telnum,address)values (person_sequence.nextval,?,?,?,?,?)");
            pstm.setString(1, name);
            pstm.setInt(2, age);
            pstm.setString(3, sex);
            pstm.setString(4, TelNum);
            pstm.setString(5, address);
            if(pstm.executeUpdate()>0){
                System.out.println("添加成功!");
            }
            AddMenu.Add();
        } catch (SQLException e) {
//            e.printStackTrace();
            System.out.println("输入错误!请重新输入");
            AddPerson();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

}
package com.TongXunLu.Add;

public interface AddServices {
    //添加新纪录
    public void AddPerson();

}
package com.TongXunLu.Delete;

public interface DeleteServices {
    public abstract void deleteByID(int id);
    public abstract void deleteAll();
}
package com.TongXunLu.Delete;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.TongXunLu.tools.Tools;

public class DeleteImplement implements DeleteServices{
    Connection conn=null;
    PreparedStatement pstm=null;

    @Override
    public void deleteByID(int id) {
        // 删除指定记录
        conn=Tools.getConnection();
        try {
            pstm=conn.prepareStatement("delete from person where id=?");
            pstm.setInt(1, id);
            if(pstm.executeUpdate()>0){
                System.out.println("删除成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }
    }

    @Override
    public void deleteAll() {
        // 删除所有记录
        conn=Tools.getConnection();
        try {
            pstm=conn.prepareStatement("delete from person ");
            if(pstm.executeUpdate()>0){
                System.out.println("删除成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }
    }

}
package com.TongXunLu.Delete;

import java.util.List;
import java.util.Scanner;

import com.TongXunLu.Select.SelectAll;
import com.TongXunLu.services.App;
import com.TongXunLu.services.Person;
import com.TongXunLu.tools.Count;

public class DeleteMenu {
    public static void menu(){
        DeleteServices del=new DeleteImplement();
        System.out.println("*********************");
        System.out.println("**    1 查看全记录     **");
        System.out.println("**    2 删除指定记录  **");
        System.out.println("**    3 删除全部记录  **");
        System.out.println("**    4 返回上一级      **");
        System.out.println("*********************");
        System.out.println("请输入正确的数字,最小是:1,最大是:4");
        Scanner input=new Scanner(System.in);
        int sel=input.nextInt();
        switch (sel) {
        case 1:
            List<Person> list=SelectAll.SelectAllPerson();
            for (Person p : list) {
                System.out.println(p.toString());
            }
            DeleteMenu.menu();
            break;
        case 2:
            int id=Count.InputXuHao();
            del.deleteByID(id);
            DeleteMenu.menu();
            break;
        case 3:
            del.deleteAll();
            DeleteMenu.menu();
            break;
        case 4:
            App.Welcome();
            break;

        }
    }

}
package com.TongXunLu.Select;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.TongXunLu.services.Person;
import com.TongXunLu.tools.Tools;

public class SelectAll {
    /*
     * 查询所有记录
     */
    public static List<Person> SelectAllPerson(){
        List <Person>list=new ArrayList();
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        conn=Tools.getConnection();
        try {
            pstm=conn.prepareStatement("select *from person");
            rs=pstm.executeQuery();
            while(rs.next()){
                Person p=new Person();
                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setAge(rs.getInt(3));
                p.setSex(rs.getString(4));
                p.setTelNum(rs.getString(5));
                p.setAddress(rs.getString(6));
//                System.out.println(p.toString());
                list.add(p);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(rs);
            Tools.Close(pstm);
            Tools.Close(conn);
        }
        return list;
    }
}
package com.TongXunLu.Select;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.TongXunLu.services.Person;
import com.TongXunLu.tools.Tools;

public class SelectImplement implements SelectServices{
    Scanner input=new Scanner(System.in);

    Connection conn=null;
    PreparedStatement pstm=null;
    ResultSet rs=null;

    Person p=new Person();

    @Override
    public void findByName() {
        //按姓名查找
        System.out.println("输入姓名,1-10为字母");
        try {
            String name=input.next();
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("select *from person where name=?");
            pstm.setString(1, name);
            rs=pstm.executeQuery();
            if(rs.next()){
                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setAge(rs.getInt(3));
                p.setSex(rs.getString(4));
                p.setTelNum(rs.getString(5));
                p.setAddress(rs.getString(6));
                System.out.println(p.toString());
            }else{
                System.out.println("无此联系人!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(rs);
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

    @Override
    public void findByAge() {

        System.out.println("输入年龄,1-100+");
        int age=input.nextInt();
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("select *from person where age=?");
            pstm.setInt(1, age);
            rs=pstm.executeQuery();
            while(rs.next()){

                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setAge(rs.getInt(3));
                p.setSex(rs.getString(4));
                p.setTelNum(rs.getString(5));
                p.setAddress(rs.getString(6));
                System.out.println(p.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(rs);
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

    @Override
    public void findBySex() {

        System.out.println("输入性别,(男 m or M)(女 f or F)");
        String sex=input.next();
        String sex_Up=sex.toUpperCase();
        String sex_Low=sex.toLowerCase();
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("select *from person where  sex=? or sex=?");
            pstm.setString(1, sex_Up);
            pstm.setString(2, sex_Low);
            rs=pstm.executeQuery();
            while(rs.next()){

                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setAge(rs.getInt(3));
                p.setSex(rs.getString(4));
                p.setTelNum(rs.getString(5));
                p.setAddress(rs.getString(6));
                System.out.println(p.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(rs);
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

    @Override
    public void findByTel() {
        System.out.println("输入电话号码,6-10位数字");
        String TelNum=input.next();
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("select *from person where TelNum=?");
            pstm.setString(1, TelNum);
            rs=pstm.executeQuery();
            while(rs.next()){

                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setAge(rs.getInt(3));
                p.setSex(rs.getString(4));
                p.setTelNum(rs.getString(5));
                p.setAddress(rs.getString(6));
                System.out.println(p.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(rs);
            Tools.Close(pstm);
            Tools.Close(conn);
        }
    }

    @Override
    public void findByAddress() {
        System.out.println("输入地址,1-50位字母或数字");
        String address=input.next();
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("select *from person where address=?");
            pstm.setString(1, address);
            rs=pstm.executeQuery();
            while(rs.next()){

                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setAge(rs.getInt(3));
                p.setSex(rs.getString(4));
                p.setTelNum(rs.getString(5));
                p.setAddress(rs.getString(6));
                System.out.println(p.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(rs);
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

}
package com.TongXunLu.Select;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import com.TongXunLu.services.App;
import com.TongXunLu.services.Person;
import com.TongXunLu.tools.Tools;

public class SelectMenu {
    //查找记录菜单
        public static void Select(){
            System.out.println("*********************");
            System.out.println("**    1 按姓名查找     **");
            System.out.println("**    2 按年龄查找     **");
            System.out.println("**    3 按性别查找     **");
            System.out.println("**    4 按号码查找     **");
            System.out.println("**    5 按住址查找     **");
            System.out.println("**    6 查看全记录     **");
            System.out.println("**    7 返回上一级     **");
            System.out.println("*********************");
            System.out.println("请输入正确的数字,最小是:1 最大是:7");

            SelectServices select=new SelectImplement();

            Scanner input=new Scanner(System.in);
            int sel=input.nextInt();
            switch (sel) {
            case 1:
                //按姓名查找

                select.findByName();
                SelectMenu.Select();
                break;
            case 2:
                //按年龄查找
                select.findByAge();
                SelectMenu.Select();
                break;
            case 3:
                //按性别查找
                select.findBySex();
                SelectMenu.Select();
                break;
            case 4:
                //按号码查找
                select.findByTel();
                SelectMenu.Select();
                break;
            case 5:
                //按住址查找
                select.findByAddress();
                SelectMenu.Select();
                break;
            case 6:
                //查看全记录
                List<Person> list=SelectAll.SelectAllPerson();
                for (Person p : list) {
                    System.out.println(p.toString());
                }
                SelectMenu.Select();
                break;
            case 7:
                App.Welcome();
                break;
            }

        }
}
package com.TongXunLu.Select;

public interface SelectServices {

    public abstract void findByName();
    public abstract void findByAge();
    public abstract void findBySex();
    public abstract void findByTel();
    public abstract void findByAddress();
}
package com.TongXunLu.services;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

import com.TongXunLu.Select.SelectAll;
import com.TongXunLu.tools.AgeComparator;
import com.TongXunLu.tools.NameComparator;
import com.TongXunLu.tools.SexComparator;

public class CompartorMenu {
    public static void menu(){
        System.out.println("*********************");
        System.out.println("**    1 按姓名排序     **");
        System.out.println("**    2 按年龄排序     **");
        System.out.println("**    3 按性别排序     **");
        System.out.println("**    4 查看全部记录  **");
        System.out.println("**    5 返回上一级     **");
        System.out.println("*********************");
        System.out.println("请输入正确的数字,最小是:1,最大是:4");
        Scanner input=new Scanner(System.in);

        int sel=input.nextInt();
        List<Person> list=SelectAll.SelectAllPerson();
        switch (sel) {
        case 1:
            //按姓名排序
            Collections.sort(list, new NameComparator());
            for (Person p : list) {
                System.out.println(p.toString());

            }
            menu();
            break;
        case 2:
            Collections.sort(list, new AgeComparator());
            for (Person p : list) {
                System.out.println(p.toString());
            }
            menu();
            break;
        case 3:
            Collections.sort(list, new SexComparator());
            for (Person p : list) {
                System.out.println(p.toString());
            }
            menu();
            break;
        case 4:
            for (Person p : list) {
                System.out.println(p.toString());
            }
            menu();
            break;
        case 5:
            App.Welcome();
            break;

        }
    }
}
package com.TongXunLu.services;

public class Person{
    private int id;
    private String name;
    private int age;
    private String sex;
    private String TelNum;
    private String Address;

    public Person(){}
    public Person(int id,String name,int age,String sex,String TelNum,String Address){
        this.id=id;
        this.name=name;
        this.age=age;
        this.sex=sex;
        this.TelNum=TelNum;
        this.Address=Address;
    }
    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 String getTelNum() {
        return TelNum;
    }
    public void setTelNum(String telNum) {
        TelNum = telNum;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }

    @Override
    public String toString() {
        return "序号\t" + id + "\t姓名\t" + name + "\t年龄\t" + age
                + "\t性别\t" + sex + "\t电话号码\t" + TelNum + "\t住址\t"
                + Address ;
    }

    public int OrderByName(Object o) {
        //姓名排序
        Person input=(Person)o;
        return name.compareTo(input.name);
    }

}
package com.TongXunLu.tools;

import java.util.Comparator;

import com.TongXunLu.services.Person;

public class AgeComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        // 年龄排序
        Person p1=(Person)o1;
        Person p2=(Person)o2;
        return p1.getAge()-p2.getAge();
    }

}
package com.TongXunLu.tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Count {
    public static int InputXuHao(){
        Scanner input=new Scanner(System.in);
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;

        conn=Tools.getConnection();
        int count=0;
        try {
            pstm=conn.prepareStatement("select count(*)from person");
            rs=pstm.executeQuery();
            while(rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(rs);
            Tools.Close(pstm);
            Tools.Close(conn);
        }
        System.out.println("请输入记录序号");
        System.out.println("请输入正确的数字,最小是:1,最大是:"+count);
        int id=input.nextInt();
        return id;
    }
}
package com.TongXunLu.tools;

import java.util.Comparator;

import com.TongXunLu.services.Person;

public class NameComparator implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        // 姓名排序
        Person p1=(Person)o1;
        Person p2=(Person)o2;

        return p1.getName().compareTo(p2.getName());
    }

}
package com.TongXunLu.tools;

import java.util.Comparator;

import com.TongXunLu.services.Person;

public class SexComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        // 性别排序
        Person p1=(Person)o1;
        Person p2=(Person)o2;
        return p1.getSex().compareTo(p2.getSex());
    }

}
package com.TongXunLu.tools;

public class TelNoteRegex {
    public static boolean CheckName(String name,boolean b){
        if(name.length()>10){
            try {
                throw new Exception("输入姓名错误,请检查!");
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }else{
            b=true;
        }
        return b;
    }
    public static boolean CheckAge(int age,boolean b){
        if(age<0||age>200){
            try {
                throw new Exception("输入年龄错误,请检查!");
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }else{
            b=true;
        }
        return b;
    }
    public static boolean CheckSex(String sex,boolean b){
        if(!(sex.equals("F")||sex.equals("f")||sex.equals("M")||sex.equals("m"))){
            try {
                throw new Exception("输入性别错误,请检查!");
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }else{
            b=true;
        }
        return b;
    }
    public static boolean CheckTel(String Tel,boolean b){
        if(Tel.length()<6||Tel.length()>10){
            try {
                throw new Exception("电话输入错误,请检查!");
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }else{
            b=true;
        }
        return b;
    }
    public static boolean CheckAddress(String Address,boolean b){
        if(Address.length()<0||Address.length()>50){
            try {
                throw new Exception("地址输入错误,请检查!");
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }else{
            b=true;
        }
        return b;
    }

}
package com.TongXunLu.tools;

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

public class Tools {
    static{
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn=null;

        try {
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl11","scott","0000");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    /**
     * 关闭资源
     * @param conn
     */
    public static void Close(Connection conn){
        try {
            if(conn!=null &&!conn.isClosed()){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void Close(PreparedStatement pstm){
        try {
            if(pstm!=null &&!pstm.isClosed()){
                pstm.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void Close(ResultSet rs){
        try {
            if(rs!=null &&!rs.isClosed()){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
package com.TongXunLu.Update;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.TongXunLu.tools.Tools;

public class UpdateImplement implements UpdateServices{

    Connection conn=null;
    PreparedStatement pstm=null;

    @Override
    public void UpdateName(String name,int id) {
        // 修改姓名

        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("Update person set name=? where id=?");
            pstm.setString(1, name);
            pstm.setInt(2, id);
            if(pstm.executeUpdate()>0){
                System.out.println("修改成功!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

    @Override
    public void UpdateAge(int age,int id) {
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("Update person set age=? where id=?");
            pstm.setInt(1, age);
            pstm.setInt(2, id);
            if(pstm.executeUpdate()>0){
                System.out.println("修改成功!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

    @Override
    public void UpdateSex(String sex,int id) {
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("Update person set sex=? where id=?");
            pstm.setString(1, sex);
            pstm.setInt(2, id);
            if(pstm.executeUpdate()>0){
                System.out.println("修改成功!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

    @Override
    public void UpdateTel(String TelNum,int id) {
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("Update person set TelNum=? where id=?");
            pstm.setString(1, TelNum);
            pstm.setInt(2, id);
            if(pstm.executeUpdate()>0){
                System.out.println("修改成功!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

    @Override
    public void UpdateAddress(String address,int id) {
        try {
            conn=Tools.getConnection();
            pstm=conn.prepareStatement("Update person set address=? where id=?");
            pstm.setString(1, address);
            pstm.setInt(2, id);
            if(pstm.executeUpdate()>0){
                System.out.println("修改成功!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Tools.Close(pstm);
            Tools.Close(conn);
        }

    }

}
package com.TongXunLu.Update;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import com.TongXunLu.Select.SelectAll;
import com.TongXunLu.services.App;
import com.TongXunLu.services.Person;
import com.TongXunLu.tools.Tools;

public class UpdateMenu {
    public static void menu(){
        System.out.println("*********************");
        System.out.println("**   1 查看全记录       **");
        System.out.println("**   2 修改指定记录    **");
        System.out.println("**   3 返回上一级    **");
        System.out.println("*********************");
        System.out.println("请输入正确的数字,最小是:1,最大是:3");
        Scanner input=new Scanner(System.in);
        int sel=input.nextInt();
        switch (sel) {
        case 1:
            List<Person> list=SelectAll.SelectAllPerson();
            for (Person p : list) {
                System.out.println(p.toString());
            }
            UpdateMenu.menu();
            break;
        case 2:
            ZhiDingXiuGai.menu();
            UpdateMenu.menu();

            break;
        case 3:
            App.Welcome();
            break;
        }
    }
}
package com.TongXunLu.Update;

public interface UpdateServices {

    //修改姓名
    public abstract void UpdateName(String name,int id);
    //修改年龄
    public abstract void UpdateAge(int age,int id);
    //修改性别
    public abstract void UpdateSex(String sex,int id);
    //修改号码
    public abstract void UpdateTel(String TelNum,int id);
    //修改住址
    public abstract void UpdateAddress(String address,int id);

}
package com.TongXunLu.Update;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.TongXunLu.tools.Count;
import com.TongXunLu.tools.TelNoteRegex;
import com.TongXunLu.tools.Tools;

public class ZhiDingXiuGai {

    public static void menu(){
        UpdateServices up=new UpdateImplement();
        int id=Count.InputXuHao();
        System.out.println("*********************");
        System.out.println("**    1 修改姓名        **");
        System.out.println("**    2 修改年龄        **");
        System.out.println("**    3 修改性别        **");
        System.out.println("**    4 修改号码        **");
        System.out.println("**    5 修改住址        **");
        System.out.println("**    6 返回上一级        **");
        System.out.println("*********************");
        System.out.println("请输入正确的数字,最小是:1,最大是:6");
        Scanner input= new Scanner (System.in);
        int sel=input.nextInt();
        switch (sel) {
        case 1:
            //修改姓名
            String name=null;
            boolean b=false;
            while(b==false){
            System.out.println("输入姓名,1-10位字母");
            name=input.next();
            b=TelNoteRegex.CheckName(name,b);
            }
            up.UpdateName(name, id);
            menu();
            break;
        case 2:
            //2 修改年龄
            int age=0;
            b=false;
            while(b==false){
            System.out.println("输入年龄,1-200");
            age=input.nextInt();
            b=TelNoteRegex.CheckAge(age, b);
            }
            up.UpdateAge(age,id);
            menu();
            break;
        case 3:
//            System.out.println("输入性别,(男 m or M)(女 f or F)");
            String sex=null;
            b=false;
            while(b==false){
            System.out.println("输入性别,(男 m or M)(女 f or F)");
            sex=input.next();
            b=TelNoteRegex.CheckSex(sex, b);
            }
            up.UpdateSex(sex,id);
            menu();
            break;
        case 4:
//            System.out.println("输入电话号码,6-10位数字");
            String TelNum=null;
            b=false;
            while(b==false){
            System.out.println("输入电话号码,6-10位数字");
            TelNum=input.next();
            b=TelNoteRegex.CheckTel(TelNum, b);
            }
            up.UpdateTel(TelNum, id);
            menu();
            break;
        case 5:
//            System.out.println("输入地址,1-50位字母或数字");
            String address=null;
            b=false;
            while(b==false){
                System.out.println("输入地址,1-50位字母或数字");
                address=input.next();
                b=TelNoteRegex.CheckAddress(address, b);
            }
            up.UpdateAddress(address, id);
            menu();
            break;
        case 6:
            UpdateMenu.menu();
            break;

        }
    }

}

初学java,程序写的不好

时间: 2024-10-06 09:14:00

简单的通讯录小程序的相关文章

简单的购物车小程序

1 # -*- coding:utf-8 -*- #简单的购物车小程序 author:李学松 2 shopping_cart =[] 3 product_list_tatol = "---product list----" 4 welcome = "-----------welcome to shopping marketi----------" 5 product_list = [ 6 ('iphone',5800), 7 ('lenovo',900), 8 ('

java学习的一些预备知识和一些简单的java小程序

1先了解一些基本的dos命令 dir     ·列出当前目录下的文件和文件夹 md      创建文件夹 rd        删除文件夹(文件下没有子文件) del "文件夹名"    可以删除带子文件的文件夹 cd..    使文件夹目录一层一层后退 cd\    回到根目录 haha>1.txt    创建文档,并将haha写入到1.txt文档中 del 1.txt    删除文件(不走回收站) del  *.txt    只删除txt文件 exit    退出命令行 set

在VR中模拟用鼠标操作电脑并实现简单画图的小程序

(图没有录好,明天换一下) 一.概述 1.实现的基本操作是: 1)用手柄抓住黄色的方块代表手抓住鼠标. 2)通过移动手柄模拟鼠标移动,电脑屏幕上的光标跟着移动. 3)当光标移动到一个Button上时,Button高亮,离开时Button取消高亮,点击Button触发点击事件. 4)当点击Button之后,打开一个画图程序,可以用光标在颜色选择区选择一种颜色,然后在画图区根据光标的移动轨迹,画出选择颜色的光标移动路径的曲线: 2.脚本 1)ComputerController挂在代表电脑的Canv

python实现简单的数学小程序

冒泡法排列list利用打标记来优化 避免重复循环杨辉三角形process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=" alt="python实现简单的数学小程序" />建立二维列表,if判定打印出第一个与最后一个1,其余利用列表索引计算与上图类似将每一行列表尾部补0,利用负索引 直接

2015.11.27初识java一集简单的java小程序

今天是来到汉企第二天,昨天主要学习了JAVA配置环境变量方法:1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME变量值:D:\Java\jdk1.7.0_60_x64(变量值必须为全英文路径,不能有中文) 变量名:CLASSPATH变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;(完全复制即可)3. 选择“系统变量”中变量名为“Path”的环境变量,双击该

Linux下简单C语言小程序的反汇编分析

韩洋原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 写在开始,本文为因为参加MOOC相关课程而写的作业,如有疏漏,还请指出. 选了一门Linux内核分析课程,因为阅读内核代码中或多或少要涉及到At&T汇编代码的阅读,所以这里写下一个对一个简单C命令行程序的反汇编分析过程,一方面完成作业,另一方面当作练手.下面开始: 1.编写我们的C语言小程序 这里我们使用简单的例子,代码如下: 1

python网络编程基础--socket的简介,以及使用socket来搭建一个简单的udp小程序

socket介绍: socket(简称套接字),是进程间通讯的一个工具,他能实现把数据从一方传输到另一方,完成不同电脑上进程之间的通讯,它好比数据的搬运工.socket应用:不夸张来说,只要跟网络相关的应用程序或者软件都使用到了socket, 比如:微信,qq等使用socket创建udp网络程序的流程:1,创建客户端套接字2,发送/接收数据3,关闭套接字使用程序展现流程 :(使用的 ide为pycharm):1,首先创建socket,在 Python 中 使用socket 模块的函数 socke

MongoDB API - 简单的示例小程序

阅读群体:对MOngoDB 尚且不够熟悉的初学者. Plus: 由于本ID是第一次使用MongoDB,如出现理解的偏差,还请指出. 1 些概念: 一个MongoDB服务可以建立多个数据库,每一个数据库可以有多张表,通常而言,我们的数据库的表的名字叫 Collection,每一个Collection可以存放多个Document,每一个文档都以BSON(binary)的形式存放在硬盘之中, 因此可以存放比较复杂的形式存放在硬盘之中,因此可以存放比较复杂的对象,它是以文档的形式进行存储的.你可以给 任

用java实现简单的聊天小程序

该小程序目前还不具有对话功能,只有最基本的登录,显示聊天记录的功能 //登录界面import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.JTextField;import javax.swing.JPasswordField;import javax.swing.JPanel;import java.awt.GridLayout;import java.aw