java小程序电话簿

实现功能增删改查,需要连接sql server

package com_wy;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Findphone {
    public  static Connection con;
    Statement sta;
    ResultSet res;
    String name;
    String number;
    public void  connectsql(){
        String URL = "jdbc:sqlserver:// localhost: 1433; DatabaseName = Phone;";
        String USER = "admin";
        String PASSWORD = "admin";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = (Connection) DriverManager.getConnection(URL,USER,PASSWORD);
            System.out.println("连接成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void inserts(){
        try {
            sta = con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入新增name:");
        name = sc.nextLine();
        System.out.println("请输入新增number:");
        number = sc.nextLine();
        String str = "insert into phonebook(name,number) values(‘"+name+"‘,"+number+")";

        try {
            sta.executeUpdate(str);
            System.out.println("添加成功");
        } catch (SQLException e) {

            System.out.println(e.getMessage());
            System.out.println("添加失败");
        }
    }
    public void finds(){
        List<PhoneInformation> list = new ArrayList<>();

        try {
            sta = con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入查找name:");
        name = sc.nextLine();
        String str = "select number from phonebook where name=‘"+name+"‘";
        try {
            res = sta.executeQuery(str);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            while (res.next()){

                System.out.println(res.getString("number"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void updates(){
        try {
            sta = con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入修改name:");
        name = sc.nextLine();
        System.out.println("请输入修改number:");
        number = sc.nextLine();
        String str = "update phonebook set number=‘"+number+"‘ where name=‘"+name+"‘";
        try {
            sta.executeUpdate(str);
            System.out.println("修改成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void deletes(){
        try {
            sta = con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入删除修改name:");
        name = sc.nextLine();
        String str = "delete from phonebook where name = ‘"+name+"‘";
        try {
            sta.executeUpdate(str);
            System.out.println("删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        int num=0;
        Findphone findphone = new Findphone();
        findphone.connectsql();
        System.out.println("1.新增");
        System.out.println("2.查找");
        System.out.println("3.修改");
        System.out.println("4.删除");
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择:");
        num = sc.nextInt();

        switch (num){
            case 1: findphone.inserts();  break;
            case 2: findphone.finds();  break;
            case 3: findphone.updates();  break;
            case 4: findphone.deletes();  break;
        }
        try {
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

java小程序电话簿

上一篇:Exchange与ADFS单点登录 PART 6:Exchange声明规则配置


下一篇:廖师兄springboot微信点餐虚拟机说明文档(二)