preparatory workWe have installed the Ethernet square client or the etheric square cluster installation JDK8 development environment, WEB3J needs in the Java 8 environment to run
Introducing Jar Packs
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>2.2.1</version>
</dependency>
1 2 3 4 5 1 2 3 4 5
Connect the Ethernet square client
Using the WEB3J API connection
public class Web3jclient {
private static String IP = "HTTP://IP address: 8545/";
Private Web3jclient () {}
private volatile static web3j web3j;
public static web3j Getclient () {
if (web3j==null) {
synchronized (web3jclient.class) {
if (web3j==null) {
web3j = web3j.build (new Httpservice (IP));
}} Return web3j
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3-4-5 6 7 8 9 10 11 12 13 14 15 16-17
Using the Parity API connection
public class Parityclient {
private static String IP = "HTTP://IP address: 8545/";
Private Parityclient () {}
private static class clientholder{
private static final Parity Parity = Parity.build ( New Httpservice (IP));
public static final Parity getparity () {return
clientholder.parity
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13-14
Account Operation
1. Create Account Pojo
public class AccountInfo {private String userName;
Private String phone;
Private String address;
Private String School;
Public String GetUserName () {return userName;
} public void Setusername (String userName) {this.username = UserName;
Public String Getphone () {return phone;
public void Setphone (String phone) {this.phone = phone;
Public String getaddress () {return address;
public void setaddress (String address) {this.address = address;
Public String Getschool () {return school;
} public void Setschool (String school) {This.school = school; }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 2-3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41-42
2. Account related operation
public class Account {private static Parity Parity = Parityclient.getparity ();
private static web3j web3j = Web3jclient.getclient ();
/** * "Life *" like this * "like" * Also * It's not the "Same" with you @Author LZH * */Public list<string> getaccountlist () {try{return Parity.personallistaccount
S (). Send (). Getaccountids ();
}catch (Exception e) {e.printstacktrace ();
return null;
public string CreateAccount (string accountname,string password,accountinfo accountinfo) {try {
Newaccountidentifier newaccountidentifier = parity.personalnewaccount (password). Send ();
if (newaccountidentifier!=null) {String AccountId = Newaccountidentifier.getaccountid ();
Parity.personalsetaccountname (Accountid,accountname); map<string,object> account = new hashmap<string,object>();
Account.put (Accountid,accountinfo);
Parity.personalsetaccountmeta (Accountid,account);
return accountid;
} catch (Exception e) {e.printstacktrace ();
return null; Public Personalaccountsinfo.accountsinfo Getaccountinfo (String accountid) {try{Personalaccoun
Tsinfo personalaccountsinfo = Parity.personalaccountsinfo (). Send ();
Return Personalaccountsinfo.getaccountsinfo (). get (AccountId);
}catch (Exception e) {e.printstacktrace ();
return null; Public BigInteger getbalance (String accountid) {try {defaultblockparameter Defaultblockparamet
ER = new Defaultblockparameternumber (58);
Ethgetbalance ethgetbalance = parity.ethgetbalance (accountid,defaultblockparameter). Send ();
if (ethgetbalance!=null) {return ethgetbalance.getbalance (); }}catch (Exception e) {e.printstacktrace ();
return null; }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 1-2--3 4---5 6--7 8 9 10 11 12 13-14 15 16 17 9 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 60 61 62 63 64 65 66 67 68 69
Account Test
public class Accounttest {public static void main (String args[]) {getbalance ();
public static void GetBalance () {Account account = new account ();
BigInteger ba = account.getbalance ("0xcee1086eabd4cac10f6658eeffcdc66ad7565450");
System.out.print (BA);
public static void Queryaccount () {Account account = new account ();
list<string> accounts = account.getaccountlist ();
for (String accountid:accounts) {System.out.println (accountid); } public void CreateAccount () {Account account =