The preparation has been installed the Ethernet square client or the etheric square cluster installation JDK8 development environment, WEB3J need in the Java 8 environment to run the introduction jar package
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>2.2.1</version>
</dependency>
Connect the Ethernet square clientUsing 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
}
}
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
}
}
Account OperationCreate 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;
}
}
Account related Operations
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.personallistaccounts
(). Send (). Getaccountids ();
}catch (Exception e) {e.printstacktrace ();
return null; public string CreateAccount (string accountname,string password,accountinfo accountinfo) {try {N
Ewaccountidentifier 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{personalaccounts
Info personalaccountsinfo = Parity.personalaccountsinfo (). Send ();
Return Personalaccountsinfo.getaccountsinfo (). get (AccountId);
}catch (Exception e) {e.printstacktrace ();
return null; Public BigInteger getbalance (String accountid) {try {defaultblockparameter Defaultblockparamete
R = new Defaultblockparameternumber (58);
Ethgetbalance ethgetbalance = parity.ethgetbalance (accountid,defaultblockparameter). Send ();
if (ethgetbalance!=null) {return ethgetbalance.getbalance ();
} }catch (Exception e) {e.printstacktrace ();
return null; }
}
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 = new account ();
AccountInfo accountinfo = new AccountInfo ();
Accountinfo.setphone ("229787499");
Accountinfo.setaddress ("World Ning Mansion");
Accountinfo.setschool ("Buaa");
Accountinfo.setusername ("Lzh");
String AccountId = Account.createaccount ("Lzh", "123456", AccountInfo);
SYSTEM.OUT.PRINTLN ("Registered account success:" +accountid); // Personalaccountsinfo.accountsinfo accountsinfo = Account.getaccountinfo ("
0xad7bbca86e02e503076b06931e05938e51e49fb9 ");
System.out.println (Accountsinfo.tostring ()); }
}
Transaction Operations
public class Trade {private static final Logger Logger = Loggerfactory.getlogger (Trade.class);
private static BigInteger nonce = new BigInteger ("0");
private static BigInteger Gasprice = new BigInteger ("1");
private static BigInteger Gaslimit = new BigInteger ("50");
Private Parity Parity = Parityclient.getparity (); public boolean trasfer (String accountid,string passsword,string toaccountid, BigDecimal amount) {Transaction tra
Nsaction = Transaction.createethertransaction (Accountid,null,null,null,toaccountid,amount.tobiginteger ()); try{ethsendtransaction ethsendtransaction =parity.personalsignandsendtransaction (Transaction,passsword). Send (
);
if (ethsendtransaction!=null) {String Tradehash = Ethsendtransaction.gettransactionhash ();
Logger.info ("account: [{}] Transfer to account: [{}], transaction hash:[{}]", Accountid,toaccountid,tradehash); }}catch (Exception e) {logger.error ("account: [{}] transaction failed!",ACCOUNTID,E);
return false; }
}
Trading Test
public class Tradetest {public
static void Main (String args[]) {
Trade Trade = new Trade ();
Trade.trasfer ("Hash code for Account A", "abc123", "hash code for Account B", new BigDecimal);
}
reference materialsWEB3J Document WEB3J Code