Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A small example of JAVA database application

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Write it casually, take the hearth as the background, write a small example for reference, and first put on the SQL table:

If OBJECT_ID ('card_pool') is not null

Drop table card_pool

Create table card_pool (

Card_id int identity (1 dint 1) not null

Card_name varchar (32)

Card_occupation varchar (32)-- Card class: neutral, druid, hunter, mage, Paladin, priest, lurker, shaman, warrior, warrior

Card_race varchar (16),-- card race: fish, demon, beast, totem, pirate, dragon

Card_type varchar (16)-- Type: weapons, entourage, heroes, spells, spell effects, heroic abilities

Card_rarity varchar (16)-rarity: ordinary, free, rare, epic, legendary

Card_consumption int,-- Mana cost

Card_blood int,-- initial health

Card_attack int,-- * force

Card_property int,-- card attribute value: 0: normal, 1: charge, 2: dead language, 3: war roar, 10: spell card effect, 11: weapons and equipment

Card_property_desc varchar (128),-- card characteristics description: charge, death language, war roar

Card_desc varchar (128C)-text description on the card

);

Insert into card_pool values ('Arrow Elf', 'Neutral',''attendant', 'ordinary', 1 dagger 1 dagger 3 'War Roar: deals 1 damage', deals 1 damage)

Insert into card_pool values ('hound', 'hunter', 'beast', 'entourage', 'ordinary', 1meme1, 1pence0pence0pence`)

Insert into card_pool values ('arcane missiles', 'mages', 'spells', 'ordinary', 1jor0jin3d10 'random 3 damage', 'random 3 damage')

Insert into card_pool values ('Aujin axe', 'warrior', 'weapon', 'ordinary', '5phare2' 5pm 11''the disappeared stalker never showed up again.')

Insert into card_pool values ('disciple of Cosun', 'Neutral', 'attendant', 'ordinary', 3pyrrine 1pyrrine 2pyrrhen3 'War Roar: deals 2 damage. Give your Kersuen + 2 Universe 2 (whether it's in hand, library, or battlefield)', 'War Roar: deals 2 damage. Give your Kersuen + 2 Universe 2 (whether it's in hand, library, or battlefield)

Insert into card_pool values ('call of doom', 'neutral',''entourage', 'rare', 8, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 If it's time to follow death, suck it into your library', 'War Roar: give your Cruen + 2 Universe 2 (whether it's in your hand, library, or battlefield). If it's time to follow death, suck it into your library')

Insert into card_pool values ('Ke Suen', 'Neutral',''entourage', 'Legend', 10meme6pence3 'War Roar: deals damage equal to the power of the follower and randomly distributes it to all enemies.' War Roar: deals damage equal to the power of the follower and randomly distributes it to all enemies.')

Insert into card_pool values ('Anu Barak', 'lurking', 'followers', 'legends','9 miners', 4'8 'and 2' dead words: change all enemy followers into 1 point.' , 'Dead words: change all enemy followers to 1 point.')

Insert into card_pool values ('scorching Sun Giant', 'mage', 'Machine', 'entourage', 'Legend', 7 magicians 7 dagger 7 dagger 7 dagger 'when you draw this card, all characters deal 2 damage.' When you draw the card, all characters deal 2 damage.

Insert into card_pool values ('Secret Challenger', 'Paladin', 'entourage', 'Epic', 6 magicals, 6 memorials 6 dagger 3) War Roar: put all the different mysteries into the battlefield from your library. War Roar: put all the different mysteries into the battlefield from your library.')

Insert into card_pool values ('witchcraft', 'Shaman Priest','', 'attendant', 'ordinary', 3 magicaI '0Magi 10') 'turns a servant into a frog with 0 stroke 1 and endows him with mockery skills', 'turns a servant into a frog with 0 ram 1 and gives mockery skills')

Insert into card_pool values ('Fire element', 'Neutral',''attendant', 'ordinary', 6pyrrine 5dint 6pr 3 'War Roar: deals 3 damage', 'War Roar: deals 3 damage')

Next up is the card class:

Public class Cards {

Name of private String cardName;// card

Private String cardOccupation;// card class: neutral, druid, hunter, mage, Paladin, priest, lurker, shaman, warrior, warrior

Private String cardRace;// card race: fish man, demon, beast, totem, pirate, dragon

Private String cardType;// type: weapon, entourage, hero, spell, spell effect, heroic ability

Private String cardRarity;// rarity: ordinary, free, rare, epic, legendary

Private Integer cardConsumption;// mana cost

Private Integer cardBlood;// initial health value

Private Integer cardAttack;//*** force

Private Integer cardProperty;// card attribute value: 0: normal, 1: charge, 2: dead language, 3: war roar, 10: spell card effect, 11: weapons and equipment

Private String cardPropertyDesc;// card characteristics description: charge, death language, war roar

/ * *

* the effect of cards as soon as they are applied to the battlefield

* @ param obj

* @ param bloodAdd

* @ param effect 1: freeze, 2: destroy, 3: life increase

, /

Public void cardBeginApply (String obj,Integer bloodAdd,Integer effect) {

}

Public void printCard () {

String cardInfo= "card name:" + this.cardName

+ "|" + "occupation:" + this.cardOccupation

+ "|" + "race:" + this.cardRace

+ "|" + "type:" + this.cardType

+ "|" + "rarity:" + this.cardRarity

+ "|" + "Mana cost value:" + this.cardConsumption

+ "|" + "initial health:" + this.cardBlood

+ "|" + "* * Force:" + this.cardAttack

+ "|" + "card property value:" + this.cardProperty

+ "|" + "description:" + 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

}

}

Finally, call the database and a test class of this card class:

Import java.sql.Connection

Import java.sql.DriverManager

Import java.sql.ResultSet

Import java.sql.SQLException

Import java.sql.Statement

Public class TestData {

/ * *

* @ param args

* @ throws ClassNotFoundException

* @ throws SQLException

, /

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 ()"; / / extract the id value of the last row in the table to avoid duplicate key values when writing

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 ()

/ / get the actual number of rows obtained by this SQL statement

Int rsCount = rs2.getInt ("cnt")

ResultSet rs = stmt.executeQuery (sql1)

/ / declare an array of objects that corresponds each row of values fetched from the database to an attribute of the card class

Cards [] card = new Cards [5]

For (int iTunes 0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report