自己随便写的,以炉石为背景,写一个参考用的小例子,先上SQL表:
if OBJECT_ID('card_pool') is not null
drop table card_pool;
create table card_pool(
card_id int identity(1,1) not null,
card_name varchar(32),
card_occupation varchar(32), --卡牌职业:中立、德鲁伊、猎人、法师、圣骑士、牧师、潜伏者、萨满祭司、术士、战士
card_race varchar(16), --卡牌种族:鱼人、恶魔、野兽、图腾、海盗、龙
card_type varchar(16), --类型:武器、随从、英雄、法术、法术效果、英雄能力
card_rarity varchar(16), --稀有度:普通、免费、稀有、史诗、传说
card_consumption int, --法力耗费值
card_blood int, --初始生命值
card_attack int, --***力
card_property int, --卡牌特性值:0:普通,1:冲锋,2:亡语, 3:战吼,10:法术牌效果,11:武器装备
card_property_desc varchar(128), --卡牌特性说明:冲锋、亡语、战吼
card_desc varchar(128) --卡牌上的文字描述
);
insert into card_pool values('弓箭精灵','中立','','随从','普通',1,1,1,3,'战吼:造成1点伤害','造成1点伤害');
insert into card_pool values('猎犬','猎人','野兽','随从','普通',1,1,1,0,'','');
insert into card_pool values('奥术飞弹','法师','','法术','普通',1,0,3,10,'随机3点伤害','随机3点伤害');
insert into card_pool values('奥金斧','战士','','武器','普通',5,2,5,11,'','那个消失了的潜行者再也不曾出现过...');
insert into card_pool values('克苏恩的信徒','中立','','随从','普通',3,1,2,3,'战吼:造成2点伤害。使你的克苏恩获得+2/+2(无论它在手牌、牌库或是战场)','战吼:造成2点伤害。使你的克苏恩获得+2/+2(无论它在手牌、牌库或是战场)');
insert into card_pool values('厄运召唤者','中立','','随从','稀有',8,9,7,3,'战吼:使你的克苏恩获得+2/+2(无论它在你的手牌、牌库或是战场)。如果该随从死亡,将其吸入你的牌库','战吼:使你的克苏恩获得+2/+2(无论它在你的手牌、牌库或是战场)。如果该随从死亡,将其吸入你的牌库');
insert into card_pool values('克苏恩','中立','','随从','传说',10,6,6,3,'战吼:造成等同于该随从***力的伤害,随机分配到所有敌人身上。','战吼:造成等同于该随从***力的伤害,随机分配到所有敌人身上。');
insert into card_pool values('阿努巴拉克','潜伏者','','随从','传说',9,4,8,2,'亡语:将所有敌方随从***变为1点。','亡语:将所有敌方随从***变为1点。');
insert into card_pool values('烈焰巨兽','法师','机械','随从','传说',7,7,7,0,'当你抽到该牌时,使所有角色造成2点伤害。','当你抽到该牌时,使所有角色造成2点伤害。');
insert into card_pool values('神秘挑战者','圣骑士','','随从','史诗',6,6,6,3,'战吼:将所有不同奥秘从你的牌库中置入战场。','战吼:将所有不同奥秘从你的牌库中置入战场。');
insert into card_pool values('妖术','萨满祭司','','随从','普通',3,1,0,10,'把一名仆从变为0/1的青蛙,并赋予嘲讽技能','把一名仆从变为0/1的青蛙,并赋予嘲讽技能');
insert into card_pool values('火元素','中立','','随从','普通',6,5,6,3,'战吼:造成3点伤害','战吼:造成3点伤害');
接下来是卡牌类:
public class Cards {
private String cardName;//卡牌名称
private String cardOccupation;//卡牌职业:中立、德鲁伊、猎人、法师、圣骑士、牧师、潜伏者、萨满祭司、术士、战士
private String cardRace;//卡牌种族:鱼人、恶魔、野兽、图腾、海盗、龙
private String cardType;//类型:武器、随从、英雄、法术、法术效果、英雄能力
private String cardRarity;//稀有度:普通、免费、稀有、史诗、传说
private Integer cardConsumption;//法力耗费值
private Integer cardBlood;//初始生命值
private Integer cardAttack;/
public void cardBeginApply(String obj,Integer bloodAdd,Integer effect){
}
public void printCard(){
String cardInfo="卡牌名称:"+this.cardName
+"|"+"职业:"+this.cardOccupation
+"|"+"种族:"+this.cardRace
+"|"+"类型:"+this.cardType
+"|"+"稀有度:"+this.cardRarity
+"|"+"法力耗费值:"+this.cardConsumption
+"|"+"初始生命值:"+this.cardBlood
+"|"+"***力:"+this.cardAttack
+"|"+"卡牌特性值:"+this.cardProperty
+"|"+"说明:"+this.cardPropertyDesc;
System.out.println(cardInfo);
}
public void cardBloodAdd(Cards obj,Integer bloodAdd){
obj.cardBlood += bloodAdd;
}
public void cardConsumptionAdd(Cards obj,Integer consumptionAdd){
obj.cardConsumption += consumptionAdd;
}
public void cardAttackAdd(Cards obj,Integer attackAdd){
obj.cardAttack += attackAdd;
}
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
public String getCardOccupation() {
return cardOccupation;
}
public void setCardOccupation(String cardOccupation) {
this.cardOccupation = cardOccupation;
}
public String getCardRace() {
return cardRace;
}
public void setCardRace(String cardRace) {
this.cardRace = cardRace;
}
public String getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getCardRarity() {
return cardRarity;
}
public void setCardRarity(String cardRarity) {
this.cardRarity = cardRarity;
}
public Integer getCardConsumption() {
return cardConsumption;
}
public void setCardConsumption(Integer cardConsumption) {
this.cardConsumption = cardConsumption;
}
public Integer getCardBlood() {
return cardBlood;
}
public void setCardBlood(Integer cardBlood) {
this.cardBlood = cardBlood;
}
public Integer getCardAttack() {
return cardAttack;
}
public void setCardAttack(Integer cardAttack) {
this.cardAttack = cardAttack;
}
public Integer getCardProperty() {
return cardProperty;
}
public void setCardProperty(Integer cardProperty) {
this.cardProperty = cardProperty;
}
public String getCardPropertyDesc() {
return cardPropertyDesc;
}
public void setCardPropertyDesc(String cardPropertyDesc) {
this.cardPropertyDesc = cardPropertyDesc;
}
}
最后是调用数据库和这个卡牌类的一个测试类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestData {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
final String className="com.microsoft.jdbc.sqlserver.SQLServerDriver";
final String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=lushi";
final String NAME="wjz";
final String PASSWORD="wjz";
String sql1 = "select top 5 * from card_pool order by newid()";//取出表中最后一行的id值,避免写入时出现重复键值
String sql2 ="select count(1) cnt from (select top 5 * from card_pool order by newid()) as a";
Class.forName(className);
Connection conn = null ;
Statement stmt = null;
try {
conn = DriverManager.getConnection(URL, NAME, PASSWORD);
stmt = conn.createStatement();
ResultSet rs2 = stmt.executeQuery(sql2);
rs2.next();
//得到本次SQL语句实际得到的行数
int rsCount = rs2.getInt("cnt");
ResultSet rs = stmt.executeQuery(sql1);
//声明一个对象数组,用以将从数据库中取出的每一行值对应到card类的一个属性
Cards[] card = new Cards[5];
for(int i=0 ; i<rsCount ; i++){
rs.next();
card[i] = new Cards();//数据对象赋值前必须初始化
card[i].setCardName(rs.getString("card_name"));
card[i].setCardOccupation(rs.getString("card_occupation"));
card[i].setCardRace(rs.getString("card_race"));
card[i].setCardType(rs.getString("card_type"));
card[i].setCardRarity(rs.getString("card_rarity"));
card[i].setCardConsumption(rs.getInt("card_consumption"));
card[i].setCardBlood(rs.getInt("card_blood"));
card[i].setCardAttack(rs.getInt("card_attack"));
card[i].setCardProperty(rs.getInt("card_property"));
card[i].setCardPropertyDesc(rs.getString("card_property_desc"));
card[i].printCard();
}
} catch (Exception e) {
e.printStackTrace();
} finally{
stmt.close();
conn.close();
}
}
}
-------------------------------------------------
执行结果:
卡牌名称:神秘挑战者|职业:圣骑士|种族:|类型:随从|稀有度:史诗|法力耗费值:6|初始生命值:6|***力:6|卡牌特性值:3|说明:战吼:将所有不同奥秘从你的牌库中置入战场。
卡牌名称:猎犬|职业:猎人|种族:野兽|类型:随从|稀有度:普通|法力耗费值:1|初始生命值:1|***力:1|卡牌特性值:0|说明:
卡牌名称:妖术|职业:萨满祭司|种族:|类型:随从|稀有度:普通|法力耗费值:3|初始生命值:1|***力:0|卡牌特性值:10|说明:把一名仆从变为0/1的青蛙,并赋予嘲讽技能
卡牌名称:烈焰巨兽|职业:法师|种族:机械|类型:随从|稀有度:传说|法力耗费值:7|初始生命值:7|***力:7|卡牌特性值:0|说明:当你抽到该牌时,使所有角色造成2点伤害。
卡牌名称:奥术飞弹|职业:法师|种族:|类型:法术|稀有度:普通|法力耗费值:1|初始生命值:0|***力:3|卡牌特性值:10|说明:随机3点伤害