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();
        }
    }
}

 

转载于:https://www.cnblogs.com/gc56-db/p/9876856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值