电话簿

Java-电话簿程序

1、实现的功能

(1) 显示所有电话号码

(2) 删除某一个电话

(3) 新增一个电话号码

2、建立数据库表存储电话信息(MySQL)

id姓名电话号码详情
1张三130***手机
2李四187***手机
3王五133**座机

3、连接MySQL

import java.sql.*;

public class ConnectionMYSQL {
    private static String URL = "jdbc:mysql://localhost:3306/PhoneBook?characterEncoding=utf8";
    private static Connection con = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    //连接数据库mysql
    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");  //加载驱动
            con = DriverManager.getConnection(URL, "root", "asdfgh");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //关闭数据库
    public static void closeConnection() {
        try {
            if (con != null) {
                con.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        getConnection();
    }
}

4、创建一个INote接口

public interface INote {
    public void Scanner1();
    public void Scanner2();
}

5、创建一个 实现类Note实现INote接口,并封装

import java.util.Scanner;

public class Note implements INote {
    static private int id;
    static private String username;
    static private String tel;
    static private String desc1;

    public Note() {
    }

    public static int getId() {
        return id;
    }

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

    public static String getUsername() {
        return username;
    }

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

    public static String getTel() {
        return tel;
    }

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

    public static String getDesc1() {
        return desc1;
    }

    public static void setDesc1(String desc1) {
        Note.desc1 = desc1;
    }

    @Override
    public String toString() {
        return "Note{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", tel='" + tel + '\'' +
                ", desc1='" + desc1 + '\'' +
                '}';
    }

    @Override
    public void Scanner1() {
        //输入信息
        java.util.Scanner sc2 = new Scanner(System.in);
        System.out.println("请输入姓名:");
        username = sc2.nextLine();
        Scanner sc3 = new Scanner(System.in);
        System.out.println("请输入电话:");
        tel = sc3.nextLine();
        Scanner sc4 = new Scanner(System.in);
        System.out.println("请输入说明信息:");
        desc1 = sc4.nextLine();
    }

    //id
    public void Scanner2() {
        Scanner sc = new Scanner(System.in);
        id = Integer.parseInt(sc.nextLine());

    }
}

6、创建INoteService接口,包含添加、删除、查询的方法

public interface INoteService {
    //添加
    public abstract void insert();

    //删除
    public abstract void delete();

    //查询
    public abstract void select();
}

7、创建实现类NoteService实现INoteService接口,继承接口的方法

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

public class NoteService implements INoteService {
    static Connection con = null;
    static PreparedStatement ps = null;
    static ResultSet rs = null;


    //添加
    @Override
    public void insert() {
        try {
            System.out.println("请输入您要添加的信息:");
            Note note = new Note();
            note.Scanner1();
            con = ConnectionMYSQL.getConnection();
            ps = con.prepareStatement("insert into note (username,tel,desc1) values (?,?,?)");
            ps.setString(1, note.getUsername());
            ps.setString(2, note.getTel());
            ps.setString(3, note.getDesc1());
            ps.executeUpdate();
            System.out.println("添加成功");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
             ConnectionMYSQL.closeConnection();
        }
    }

    //删除
    @Override
    public void delete() {
        try {
            System.out.println("请输入您要删除信息的序号:");
            con =  ConnectionMYSQL.getConnection();
            Note note = new Note();
            note.Scanner2();
            ps = con.prepareStatement("delete from note where id = ? ");
            ps.setInt(1, Note.getId());
            ps.executeUpdate();
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
             ConnectionMYSQL.closeConnection();
        }
    }

    //查询
    @Override
    public void select() {
        try {
            con =  ConnectionMYSQL.getConnection();
            ps = con.prepareStatement("select * from note");
            rs = ps.executeQuery();
            System.out.println("查询结果如下:");
            System.out.println(" 序 号 " + "  姓名  " + "   电话号码   " + "   说明信息 ");
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String tel = rs.getString("tel");
                String desc1 = rs.getString("desc1");
                System.out.println("  " + id + "     " + username + "      " + tel + "       " + desc1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
             ConnectionMYSQL.closeConnection();
        }
    }
}

8、主界面,可以调用添加、删除、查询的方法

import java.util.Scanner;

public class MainTest {
    public static void main(String[] args) {
        while (true) {
            System.out.println("1.查询    2.添加    3.删除    4.退出");
            System.out.println("请输入你要执行的数字:");
            Scanner sc = new Scanner(System.in);
            int i = Integer.parseInt(sc.nextLine());
            NoteService noteService = new NoteService();
            if (i == 1) {
                noteService.select();
            }
            if (i == 2) {
                noteService.insert();
            }
            if (i == 3) {
                noteService.delete();
            }
            if (i == 4) {
                System.out.println("您以成功退出,欢迎下次使用。");
                break;
            }
        }
    }
}

界面效果:

1.查询    2.添加    3.删除    4.退出  
请输入你要执行的数字:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值