创建RPG游戏角色(jdbc实现)

本文通过jdbc实现RPG游戏角色的创建,包括题目分析、UML类图设计、代码实现(Data_opreation, roleAttribute, Info_update, Test类)及总结。在实现过程中强调了sql语句正确性的重要性,并指出jdbc对于解决此类问题的易读性优势。" 132286570,19671414,使用PCL库提取特定高程点云数据,"['点云处理', 'PCL']

一、题目分析

以我思考的方式使用数据库存储角色属性,利用jdbc完成角色的创建。在实现该方法中,
采用随机数来得到该角色的各个属性,并利用jdbc将得到的结果存储进数据库。在我的
程序中,我将创建角色的过程进行了简单的优化,比如在选择性别或者种族,职业时会
判断输入是否正确。如若输入错误,会提示重新输入。并且完成了角色的属性默认选择。

二、UML类图设计

在这里插入图片描述

三、代码实现

Data_opreation.java:

package per.bhj.homework.rpg;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Data_operation {
   
   
    File raceInformation = new File("raceInformation.txt");
    private  Connection connection = null;
    protected String occupation = null;
    protected String attributeString = null;

    public void insertAttribute(String insert) {
   
   
        try {
   
   
//            connection = DriverManager.getConnection("jdbc:sqlite:attribute.db");
            connection = DriverManager.getConnection("jdbc:sqlite:attribute.db");
            Statement statement = connection.createStatement();
//            statement.setQueryTimeout(100);
            statement.executeUpdate(insert);
        } catch (Exception e) {
   
   
            System.out.println(e);
            System.out.println("增加属性出错!");
        }
    }

    public void insertInfo(String insert) {
   
   
        try {
   
   
            connection = DriverManager.getConnection("jdbc:sqlite:info.db");
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(100);
            statement.executeUpdate(insert);
        } catch (Exception e) {
   
   
            System.out.println(e);
            System.out.println("增加角色所有信息出错!");
        }
    }

    public void wipeAttribute() {
   
   
        try {
   
   
            connection = DriverManager.getConnection("jdbc:sqlite:attribute.db");
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(100);
            statement.executeUpdate("delete from attribute");
            statement.executeUpdate("update sqlite_sequence set seq=0 where name='attribute';");
        } catch (Exception e) {
   
   
            System.out.println("清除数据库attribute.db出错!");
        }
    }

    public void wipeInfo() {
   
   
        try {
   
   
            connection = DriverManager.getConnection("jdbc:sqlite:info.db");
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(100);
            statement.executeUpdate("delete from info");
        } catch (Exception e) {
   
   
            System.out.println("清除数据库info.db出错!");
        }
    }

    public void printInfo() {
   
   
        try {
   
   
            connection = DriverManager.getConnection("jdbc:sqlite:info.db");
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(100);
            ResultSet resultSet = statement.executeQuery("select * from info");
            while(resultSet.next()) {
   
   
                occupation = resultSet.getString("职业");
                System.out.println("======================================");
                System.out.println("姓名" + "                 " + resultSet.getString("姓名"));
                System.out.println("======================================");
                System.out.println("性别" + "                " + resultSet.getString("性别"));
                System.out.println("======================================");
                System.out.println("种族" + "                " + resultSet.getString("种族"));
                System.out.println("======================================");
                System.out.println("职业" + "                " + resultSet.getString("职业"));
                System.out.println("======================================");
                System.out.println("力量" + "                 " + resultSet.getInt("力量"));
                System.out.println("======================================");
                System.out.println("敏捷" + "                 " + resultSet.getInt("敏捷"));
                System.out.println("======================================");
                System.out.println("体力" + "                 " + resultSet.getInt("体力"));
                System.out.println("======================================");
                System.out.println("智力" + "                  " + resultSet.getInt("智力"));
                System.out.println("======================================");
                System.out.println("智慧" + "                  " + resultSet.getInt("智慧"));
                System.out.println("======================================");
                System.out.println("生命值" + "                " + resultSet.getInt("生命值"));
                System.out.println("======================================");
                System.out.println("魔法值" + "                " + resultSet.getInt("魔法值"));
            }
        } catch (Exception e) {
   
   
            System.out.println("查看信息出错!");
        }
    }

    public void writeFile() {
   
   
        try {
   
   
            connection = DriverManager.getConnection("jdbc:sqlite:info.db");
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(100);
            ResultSet resultSet = statement.executeQuery("select * from info");
            if(!raceInformation.exists()) {
   
   
                raceInformation.createNewFile();
            }
            FileWriter fileWriter = new FileWriter</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值