java小程序电话簿

Posted hsdv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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小程序电话簿的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

微信小程序代码片段分享

小程序各种功能代码片段整理---持续更新

Android获取各个应用程序的缓存文件代码小片段(使用AIDL)

提效小技巧——记录那些不常用的代码片段

Android小部件,启动一个片段?