模仿10086电话的代码

代码实现了和数据库的链接,查询
代码如下

package com.zhongruan;

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

public class X10086 {
    public  static int selectdb(int number,int l) throws SQLException, ClassNotFoundException {
        DBUtil dbUtil=new DBUtil();
        Connection connection=dbUtil.getConnection();
        String sql="select * from x10086 where phonenumber="+number+"";
        PreparedStatement statement=connection.prepareStatement(sql);
        ResultSet resultSet=statement.executeQuery();
        int i=0;
        while (resultSet.next()){
            i=resultSet.getInt(l);
        }
        return i;
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner input=new Scanner(System.in);
        System.out.println("欢迎致电中国移动");
        Boolean b=true;
        while (b){
            System.out.println("查询话费请按1,查询流量请按2,人工服务请按3,结束通话请按4");
            System.out.println("请输入一个数");
            int n=input.nextInt();
            switch (n){
                case 1:
                    System.out.println("请输入你的手机号:");
                    int phonenumber1=input.nextInt();
                    int result1=X10086.selectdb(phonenumber1,2);
                    System.out.println("您的话费余额为:"+result1+"元");
                    break;
                case 2:
                    System.out.println("请输入你的手机号:");
                    int phonenumber2=input.nextInt();
                    int result2=X10086.selectdb(phonenumber2,3);
                    System.out.println("您的流量余额为:"+result2+"MB");
                    break;
                case 3:
                    System.out.println("嘟~嘟~嘟");
                    System.out.println("客服繁忙,请稍后再拨");
                    break;
                case 4:
                    System.out.println("欢迎下次致电中国移动");
                    b=false;
                    break;
                default:
                    System.out.println("输入的数字有误,请重新输入");
                    break;
            }
        }



    }



}

你可能感兴趣的:(模仿10086电话的代码)