周末作业:利用数据库存储账户信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了周末作业:利用数据库存储账户信息相关的知识,希望对你有一定的参考价值。

本次作业我自己设计了一个与电脑对战的猜拳游戏,玩家第一次玩必须首先开通游戏账户,玩家信息(账户id,密码,游戏积分)

存储在数据库的中的player表中,如果猜拳胜利,积分会相应的增加,数据库中的数据也会随之更新。

下面是详细的代码和图示:

数据库部分:

-- Create table
create table PLAYER
(
  playerid VARCHAR2(12) not null,
  playerpw VARCHAR2(6) not null,
  score    NUMBER not null
)
tablespace TEXT_001
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the columns 
comment on column PLAYER.playerid
  is 玩家账户;
comment on column PLAYER.playerpw
  is 账户密码;
comment on column PLAYER.score
  is 积分;
-- Create/Recreate primary, unique and foreign key constraints 
alter table PLAYER
  add constraint PR_PLAYERID primary key (PLAYERID)
  using index 
  tablespace TEXT_001
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

技术分享

 

 

游戏代码部分:

技术分享

package game;

public class Players {

    // 定义成员表变量
    private String playerID;// 玩家ID

    private String password;// 登录密码

    private int score; // 玩家积分
    // getter和setter方法

    public String getPlayerID() {
        return playerID;
    }

    public void setPlayerID(String playerID) {
        this.playerID = playerID;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    // 构造方法
    public Players()// 不带参数构造方法
    {

    }

    public Players(String playerID, String password, int score)// 带参构造方法
    {
        super();
        this.playerID = playerID;
        this.password = password;
        this.score = score;

    }

    // to string
    @Override
    public String toString() {
        return "Players [playerID=" + playerID + ", password=" + password + ", score=" + score + "]";
    }

}
package game;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Startgame {
    String s = ""; // 用来存放登陆中用户的ID

    String p = ""; // 用来存放登陆中用户的密码

    int c = 0; // 用来存放登陆中用户的积分

    public int game()

    {
        // 加载数据库驱动

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            // 获取数据库连接 通过java的驱动管理器

            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816",
                    "666666");

            // 获得操作数据库的声明

            Statement st = conn.createStatement();

            System.out.print("请输入您的游戏ID:");// 输入id
            Scanner sc1 = new Scanner(System.in);
            int result = 0;
            String plid = "";
            String plpw = "";
            int plsc = 0;
            for (int j = 0; j >= 0; j++) {
                String id = sc1.nextLine();

                // 查询数据库,判断输入的ID是否已经存在

                ResultSet rs = st.executeQuery("select * from player where playerid = ‘" + id + "‘");

                while (rs.next()) {
                    // 读取数据
                    String playerid = rs.getString("playerid");
                    String playerpw = rs.getString("playerpw");
                    int score = rs.getInt("score");

                    plid = playerid;
                    plpw = playerpw;
                    plsc = score;

                }
                if (plid == "") {
                    System.out.print("您输入的ID不存在,请重新输入:");
                } else {
                    j = -2;
                    break;
                }

            }
            System.out.print("请输入您的密码:"); // 输入密码
            Scanner sc2 = new Scanner(System.in);
            for (int i = 0; i < 3; i++) {
                String password = sc2.nextLine();
                if (password.equals(plpw)) {
                    System.out.println("loading......");
                    result = 1;
                    break;
                } else if (i < 2) {
                    System.out.print("密码错误,请重新输入:");
                } else {
                    System.out.println("对不起,密码输入错误次数过多,系统将强制关闭...");
                    result = -1;
                }
            }
            p = plpw;
            c = plsc;
            s = plid;
            return result;
        } catch (ClassNotFoundException | SQLException e) {

            e.printStackTrace();
            return 1;
        }
    }

    public void gamebegain() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 获取数据库连接 通过java的驱动管理器

            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816",
                    "666666");

            // 获得操作数据库的声明

            Statement st = conn.createStatement();
            System.out.println("欢迎回来   " + s);
            System.out.println("您当前的积分为: " + c + "\\n游戏即将开始...");
            int score = 0;
            for (int i = 0; i >= 0; i++) {

                Random ran = new Random();
                int computer = ran.nextInt(3);
                int cha = 0; // 通过差值判断胜负
                String cp = ""; // 存放电脑的输出结果
                String pl = ""; // 存放玩家的输出结果
                switch (computer) {
                case 0:
                    cp = "剪刀";
                    break;
                case 1:
                    cp = "锤头";
                    break;
                case 2:
                    cp = "包袱";
                    break;
                }

                Scanner sc2 = new Scanner(System.in);
                System.out.print("您将要出的是(0.剪刀/1.锤头/2.包袱):");
                for (int j = 0; j >= 0; j++) {
                    int player = sc2.nextInt();
                    switch (player) {
                    case 0:
                        pl = "剪刀";
                        j = -2;
                        break;
                    case 1:
                        pl = "锤头";
                        j = -2;
                        break;
                    case 2:
                        pl = "包袱";
                        j = -2;
                        break;
                    default:
                        System.out.print("输入结果无效,请重新输入:");
                        break;
                    }

                    cha = computer - player; // 计算差值
                }
                switch (cha) {
                case (0):
                    System.out.println("电脑出的是:" + cp + "   平局");
                    score += 0;
                    break;
                case (-1):
                    System.out.println("电脑出的是:" + cp + "   你赢啦,积分+100");
                    score += 100;
                    break;
                case (2):
                    System.out.println("电脑出的是:" + cp + "   你赢啦,积分+100");
                    score += 100;
                    break;
                case (-2):
                    System.out.println("电脑出的是:" + cp + "   你输啦,积分-100");
                    score -= 100;
                    break;
                case (1):
                    System.out.println("电脑出的是:" + cp + "   你输啦,积分-100");
                    score -= 100;
                    break;
                }

                System.out.print("您要继续游戏吗?0.是    1.否");

                Scanner sc3 = new Scanner(System.in);

                int jieguo = sc3.nextInt();

                switch (jieguo) {
                case 0:
                    System.out.println("游戏继续...");
                    break;
                case 1:
                    System.out.println("游戏已经关闭,再见...");
                    i = -2;
                    c += score;
                    st.executeUpdate("update player set score = ‘" + c + "‘ where playerid = ‘" + s + "‘");
                    break;
                default:
                    System.out.println("输入结果无效,游戏强行退出...");
                    i = -2;
                    c += score;
                    st.executeUpdate("update player set score = ‘" + c + "‘ where playerid = ‘" + s + "‘");
                    break;
                }
            }

        } catch (ClassNotFoundException | SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }

    }

    public void oppenaccount() {
        // 加载数据库驱动

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            // 获取数据库连接 通过java的驱动管理器

            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "text0816",
                    "666666");

            // 获得操作数据库的声明

            Statement st = conn.createStatement();

            String newID = "";
            String newpassword = "";
            int newscore = 1000;
            // List<Players> list = Playerlist.getlist().getList();
            System.out.print("请输入一个新的ID:");

            Scanner sc = new Scanner(System.in);

            for (int i = 0; i >= 0; i++) {
                newID = sc.nextLine();

                // 查询数据库,判断输入的ID是否已经存在

                ResultSet rs = st.executeQuery("select * from player where playerid = ‘" + newID + "‘");
                String plid = "";
                while (rs.next()) {
                    // 读取数据
                    String playerid = rs.getString("playerid");
                    String playerpw = rs.getString("playerpw");
                    String score = rs.getString("score");

                    plid = playerid;

                }
                if (plid == "") {
                    i = -2;
                    break;
                } else {
                    System.out.print("该ID已被注册,请重新输入:");
                }

            }
            System.out.print("请创建您的密码:");
            Scanner sc1 = new Scanner(System.in);

            newpassword = sc1.nextLine();

            System.out.print("请再次输入密码以验证:");

            Scanner sc2 = new Scanner(System.in);

            String newpassword2;
            while (true) {
                newpassword2 = sc2.nextLine();

                if (newpassword.equals(newpassword2)) {
                    System.out.println("验证通过,创建用户成功!");
                    break;
                } else {
                    System.out.println("验证失败,请重新输入");
                }
            }

            st.executeUpdate("insert into player (playerid,playerpw,score) values (" + "‘" + newID + "‘" + "," + "‘"
                    + newpassword + "‘" + "," + "‘1000‘)");

        } catch (ClassNotFoundException | SQLException e) {

            e.printStackTrace();
        }

    }

}
package game;

import java.util.Scanner;

public class Text {

    public static void main(String[] args) {

        Startgame st = new Startgame();

        System.out.println("欢迎进入猜拳游戏...\\n1.创建账户\\n2.开始游戏");

        Scanner sc = new Scanner(System.in);

        System.out.print("请选择您的菜单:");
        for (int i = 0; i >= 0; i++) {
            int resout = sc.nextInt();

            switch (resout) {
            case 1:
                st.oppenaccount();
                i = -2;
                break;
            case 2:
                if (st.game() >= 0) {
                    st.gamebegain();
                } else {
                    System.out.println("系统正在关闭,再见...");
                }
                i = -2;
                break;
            default:
                System.out.print("输入错误,请重新输入:");
            }
        }

    }

}

 

以下是测试结果截图:

技术分享

技术分享

 

以上是关于周末作业:利用数据库存储账户信息的主要内容,如果未能解决你的问题,请参考以下文章

周末大作业

挖洞经验 | 利用XSS和CSRF漏洞远程实现PayPal合作方网站未授权账户访问

python基础之员工信息表作业

如何在数据库中表示银行账户信息?

数据预处理

2017-2018网络攻防第四周作业