The Java client of Ethernet square uses __java

Source: Internet
Author: User
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>
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
    }
}

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 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;
    }
}

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; }
}
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 ("0xad7bbca86e02e503076b06931e05938e51e4
9fb9 ");
    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 tr
        Ansaction = 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: [{}]Trading Failure! ", 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);
    }
other

There is a problem, or want to know more information on the block chain, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.